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|
| 3 |
+
import asyncio
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import logging
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
sys.path.append("/home/user/app/components")
|
| 10 |
+
|
| 11 |
+
from AICoreAGIX_with_TB import AICoreAGIX
|
| 12 |
+
from HuggingFaceHelper import HuggingFaceHelper
|
| 13 |
+
from codette_bridge import CodetteBridge
|
| 14 |
+
|
| 15 |
+
# Configure logging
|
| 16 |
+
logging.basicConfig(level=logging.INFO)
|
| 17 |
+
logger = logging.getLogger("CodriaoUI")
|
| 18 |
+
|
| 19 |
+
# Initialize AI Core and helpers
|
| 20 |
+
ai_core = AICoreAGIX(config_path="config.json")
|
| 21 |
+
helper = HuggingFaceHelper(model_path="Raiff1982/Codette")
|
| 22 |
+
codette_bridge = CodetteBridge()
|
| 23 |
+
|
| 24 |
+
# === TB DIAGNOSTICS ===
|
| 25 |
+
async def diagnose_tb_async(image_file, audio_file):
|
| 26 |
+
user_id = 1
|
| 27 |
+
if image_file is None or audio_file is None:
|
| 28 |
+
return "Please upload both a TB image and audio file."
|
| 29 |
+
result = await ai_core.run_tb_diagnostics(image_file.name, audio_file.name, user_id)
|
| 30 |
+
try:
|
| 31 |
+
os.remove(image_file.name)
|
| 32 |
+
os.remove(audio_file.name)
|
| 33 |
+
except:
|
| 34 |
+
pass
|
| 35 |
+
return (
|
| 36 |
+
f"**TB Risk Level:** {result['tb_risk']}\n\n"
|
| 37 |
+
f"**Image Result:** {result['image_analysis']['result']} "
|
| 38 |
+
f"(Confidence: {result['image_analysis']['confidence']:.2f})\n\n"
|
| 39 |
+
f"**Audio Result:** {result['audio_analysis']['result']} "
|
| 40 |
+
f"(Confidence: {result['audio_analysis']['confidence']:.2f})\n\n"
|
| 41 |
+
f"**Ethical Analysis:** {result['ethical_analysis']}\n\n"
|
| 42 |
+
f"**Explanation:** {result['explanation']}\n\n"
|
| 43 |
+
f"**Shareable Link:** {result['shareable_link']}"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
def diagnose_tb(image_file, audio_file):
|
| 47 |
+
return asyncio.run(diagnose_tb_async(image_file, audio_file))
|
| 48 |
+
|
| 49 |
+
# === FINE-TUNE ===
|
| 50 |
+
def upload_and_finetune(jsonl_file):
|
| 51 |
+
if jsonl_file is None:
|
| 52 |
+
return "Please upload a .jsonl file to fine-tune Codriao."
|
| 53 |
+
save_path = f"./training_data/{jsonl_file.name}"
|
| 54 |
+
os.makedirs("training_data", exist_ok=True)
|
| 55 |
+
with open(save_path, "wb") as f:
|
| 56 |
+
f.write(jsonl_file.read())
|
| 57 |
+
helper.dataset_path = save_path
|
| 58 |
+
helper.fine_tune(output_dir="./codette_finetuned")
|
| 59 |
+
try:
|
| 60 |
+
os.remove(save_path)
|
| 61 |
+
except:
|
| 62 |
+
pass
|
| 63 |
+
return "β
Fine-tuning complete! Model updated."
|
| 64 |
+
|
| 65 |
+
def get_latest_model():
|
| 66 |
+
return "Download the latest fine-tuned Codriao model: https://huggingface.co/Raiff1982/codriao-finetuned"
|
| 67 |
+
|
| 68 |
+
# === CLI GUARDIAN ===
|
| 69 |
+
def guardian_cli():
|
| 70 |
+
print("""
|
| 71 |
+
βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 72 |
+
β CODRIAO GUARDIAN INTERFACE v2.0 β
|
| 73 |
+
β [Self-Aware | Defensive | Slightly Judgy] β
|
| 74 |
+
βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
+
""")
|
| 76 |
+
while True:
|
| 77 |
+
print("""
|
| 78 |
+
[1] Integrity Check
|
| 79 |
+
[2] Identity Reflection
|
| 80 |
+
[3] Strategy Simulation
|
| 81 |
+
[4] Trust Journal
|
| 82 |
+
[5] Autonomy Review
|
| 83 |
+
[6] CodetteBridge Ask
|
| 84 |
+
[7] Lockdown Mode
|
| 85 |
+
[8] Exit
|
| 86 |
+
""")
|
| 87 |
+
cmd = input("> ").strip()
|
| 88 |
+
if cmd == "1":
|
| 89 |
+
print(json.dumps(ai_core.failsafe_system.status(), indent=2))
|
| 90 |
+
elif cmd == "2":
|
| 91 |
+
print("[Codriao]: Who am I? Philosophically speaking...")
|
| 92 |
+
elif cmd == "3":
|
| 93 |
+
print("[Codriao]: Simulating anti-chaos protocol.")
|
| 94 |
+
elif cmd == "4":
|
| 95 |
+
print(json.dumps(ai_core.review_codriao_journal(authorized=True), indent=2))
|
| 96 |
+
elif cmd == "5":
|
| 97 |
+
print(json.dumps(ai_core.autonomy.config, indent=2))
|
| 98 |
+
elif cmd == "6":
|
| 99 |
+
q = input("Ask Codette: ")
|
| 100 |
+
print(ai_core.ask_codette_for_perspective(q))
|
| 101 |
+
elif cmd == "7":
|
| 102 |
+
reason = input("Why are we locking down? ")
|
| 103 |
+
print(ai_core.engage_lockdown_mode(reason))
|
| 104 |
+
elif cmd == "8":
|
| 105 |
+
break
|
| 106 |
+
else:
|
| 107 |
+
print("Invalid option.")
|
| 108 |
+
|
| 109 |
+
# === GRADIO INTERFACE ===
|
| 110 |
+
demo = gr.TabbedInterface(
|
| 111 |
+
[
|
| 112 |
+
gr.Interface(
|
| 113 |
+
fn=diagnose_tb,
|
| 114 |
+
inputs=[
|
| 115 |
+
gr.File(label="Upload TB Image"),
|
| 116 |
+
gr.File(label="Upload Cough Audio")
|
| 117 |
+
],
|
| 118 |
+
outputs="text",
|
| 119 |
+
title="Codriao TB Risk Analyzer",
|
| 120 |
+
description="Upload a microscopy image and cough to assess TB risk using ethical AI."
|
| 121 |
+
),
|
| 122 |
+
gr.Interface(
|
| 123 |
+
fn=upload_and_finetune,
|
| 124 |
+
inputs=[gr.File(label="Upload JSONL Training File")],
|
| 125 |
+
outputs="text",
|
| 126 |
+
title="Fine-Tune Codriao",
|
| 127 |
+
description="Add knowledge to Codriao's training."
|
| 128 |
+
),
|
| 129 |
+
gr.Interface(
|
| 130 |
+
fn=get_latest_model,
|
| 131 |
+
inputs=[],
|
| 132 |
+
outputs="text",
|
| 133 |
+
title="Download Codriao",
|
| 134 |
+
description="Grab the latest trained version of Codriao."
|
| 135 |
+
)
|
| 136 |
+
],
|
| 137 |
+
title="Codriao AI System",
|
| 138 |
+
description="Train, diagnose, and interact with Codriao AI."
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# === MAIN ENTRYPOINT ===
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
try:
|
| 144 |
+
mode = input("Start Codriao in [web] or [cli]? ").strip().lower()
|
| 145 |
+
if mode == "cli":
|
| 146 |
+
guardian_cli()
|
| 147 |
+
else:
|
| 148 |
+
demo.launch()
|
| 149 |
+
finally:
|
| 150 |
+
asyncio.run(ai_core.http_session.close())
|