dawit45's picture
Update app.py
175b667 verified
import os
import time
import json
import re
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import gradio as gr
from groq import Groq
from datetime import datetime
from pypdf import PdfReader
# --- 1. SECURE INITIALIZATION ---
def get_groq_client():
raw_key = os.getenv("GROQ_API_KEY")
if not raw_key: return None
return Groq(api_key=raw_key.strip())
# --- 2. ADVANCED MULTI-AGENT ARCHITECTURE ---
class ChronosIntelligence:
"""Agentic logic for World-Class Clinical Intelligence."""
def parse_archive(self, file_obj):
"""Ingests PDF/Text clinical history."""
if file_obj is None: return "No archival history provided."
try:
if file_obj.name.endswith(".pdf"):
reader = PdfReader(file_obj.name)
return " ".join([p.extract_text() for p in reader.pages if p.extract_text()])[:4000]
return open(file_obj.name, 'r').read()[:4000]
except Exception as e:
return f"Context Error: {str(e)}"
def transcribe_universal(self, client, audio_path):
"""Handles both live mic and uploaded audio files (MP3/WAV/M4A)."""
try:
with open(audio_path, "rb") as file:
# Optimized for high-fidelity medical terminology
transcription = client.audio.transcriptions.create(
file=(audio_path, file.read()),
model="whisper-large-v3",
response_format="verbose_json",
)
return transcription.text
except Exception as e:
return f"Transcription Fault: {str(e)}"
def generate_clinical_matrix(self, client, transcript, history, language):
"""Llama 3.3 70B: Multi-Agent Clinical Reasoning."""
master_prompt = f"""
ACT AS: Senior Medical Officer & Clinical Intelligence Architect.
LANGUAGE: {language}
CONTEXT (Past Records): {history}
TRANSCRIPT (Current Session): {transcript}
EXECUTE SOVEREIGN PROTOCOL:
1. [SOAP NOTE]: High-fidelity technical documentation in English.
2. [HISTORICAL ALIGNMENT]: Compare current data with the uploaded PDF history. Identify worsening trends.
3. [BILLING]: Map to ICD-11 & CPT-2026 with justification.
4. [PATIENT LIAISON]: Provide a simple, warm care plan in {language}.
FORMAT: Professional technical Markdown. Use Bold for clinical markers.
"""
try:
chat = client.chat.completions.create(
messages=[{"role": "system", "content": "SOVEREIGN CLINICAL OS ACTIVE."},
{"role": "user", "content": master_prompt}],
model="llama-3.3-70b-versatile",
temperature=0.1,
max_tokens=3000
)
return chat.choices[0].message.content
except Exception as e:
return f"❌ Logic Core Failure: {str(e)}"
CHRONOS = ChronosIntelligence()
# --- 3. VISUALIZATION ENGINES ---
def generate_risk_3d():
"""Generates a futuristic 3D Risk Radar Chart."""
categories = ['Cardio', 'Renal', 'Neuro', 'Metabolic', 'Vascular']
values = np.random.randint(30, 90, size=5).tolist()
fig = go.Figure(data=go.Scatterpolar(
r=values + [values[0]],
theta=categories + [categories[0]],
fill='toself',
line_color='#10b981',
fillcolor='rgba(16, 185, 129, 0.2)'
))
fig.update_layout(
polar=dict(bgcolor='rgba(0,0,0,0)', radialaxis=dict(visible=False), angularaxis=dict(gridcolor="#333", color="white")),
showlegend=False, paper_bgcolor='rgba(0,0,0,0)', font_color='white', margin=dict(t=30, b=30, l=30, r=30)
)
return fig
# --- 4. MASTER ORCHESTRATION ---
def run_universal_workflow(audio_path, archive_file, language):
client = get_groq_client()
if not client: return "### ❌ OFFLINE: API Key Missing", None, "Error", pd.DataFrame()
if not audio_path: return "### ⚠️ System Awaiting Audio Intake...", None, "Waiting...", pd.DataFrame()
start_time = time.time()
# 1. Archive Parsing
history_text = CHRONOS.parse_archive(archive_file)
# 2. Transcription (Universal)
transcript = CHRONOS.transcribe_universal(client, audio_path)
# 3. Clinical reasoning
analysis = CHRONOS.generate_clinical_matrix(client, transcript, history_text, language)
latency = (time.time() - start_time) * 1000
# Visuals
risk_plot = generate_risk_3d()
perf_df = pd.DataFrame([
{"Agent": "Llama-3.3", "Task": "Reasoning", "Latency": f"{latency*0.7:.0f}ms"},
{"Agent": "Whisper-v3", "Task": "Transcribe", "Latency": f"{latency*0.3:.0f}ms"},
{"Agent": "Security", "Status": "Encrypted", "Latency": "Active"}
])
return analysis, risk_plot, transcript, perf_df
# --- 5. THE LIQUID-EMERALD HUD UI ---
css = """
body, .gradio-container { background-color: #030303 !important; color: #ffffff !important; font-family: 'Inter', sans-serif; }
.main-panel { border: 1px solid rgba(16, 185, 129, 0.3) !important; border-radius: 20px !important; background: rgba(15, 15, 15, 0.8) !important; backdrop-filter: blur(20px); padding: 30px; box-shadow: 0 20px 60px rgba(0,0,0,0.5); }
.status-pill { background: #000; border: 1px solid #10b981; padding: 5px 15px; border-radius: 50px; color: #10b981; font-size: 12px; font-weight: bold; }
.action-btn { background: linear-gradient(135deg, #10b981 0%, #059669 100%) !important; color: #000 !important; font-weight: 900 !important; border-radius: 12px !important; height: 60px !important; text-transform: uppercase; letter-spacing: 2px; cursor: pointer; border: none !important; }
.plus-vault { border: 2px dashed #10b981 !important; border-radius: 15px !important; background: #000 !important; text-align: center; font-size: 30px !important; color: #10b981 !important; }
input, textarea { background: #000 !important; color: #10b981 !important; border: 1px solid #333 !important; }
"""
with gr.Blocks(theme=gr.themes.Default(), css=css) as demo:
with gr.Row():
with gr.Column(scale=3):
gr.Markdown("# πŸŽ™οΈ **CHRONOS-SCRIBE** <span style='color:#10b981'>ULTRA v3.0</span>")
gr.Markdown("### **UNIVERSAL AMBIENT CLINICAL INTELLIGENCE COMMAND**")
with gr.Column(scale=1):
gr.HTML("<div style='text-align:right'><span class='status-pill'>● CORE ACTIVE</span> <span class='status-pill'>● GROQ LPU</span></div>")
with gr.Row():
# LEFT: UNIVERSAL INTAKE
with gr.Column(scale=1, elem_classes="main-panel"):
gr.Markdown("### πŸ“‘ **BIOMETRIC INTAKE**")
# CHANGED: sources=["microphone", "upload"] allows for file uploads here too
audio_in = gr.Audio(label="Record or Upload Audio", type="filepath", sources=["microphone", "upload"])
gr.Markdown("### πŸ“‚ **CLINICAL CONTEXT (PDF)**")
file_in = gr.File(label="+", elem_classes="plus-vault") # FOR PAST RECORDS
lang_in = gr.Dropdown(label="Patient Language", choices=["English", "Spanish", "French", "German", "Arabic", "Hindi"], value="English")
run_btn = gr.Button("⚑ INITIATE CLINICAL FUSION", elem_classes="action-btn")
gr.Markdown("---")
gr.Markdown("#### πŸ“ˆ **LATENCY HUD**")
perf_out = gr.Dataframe(headers=["Agent", "Task", "Latency"], interactive=False)
# RIGHT: INTELLIGENCE HUB
with gr.Column(scale=2, elem_classes="main-panel"):
with gr.Tabs():
with gr.TabItem("πŸ“‹ **SOVEREIGN REPORT**"):
report_out = gr.Markdown(value="### *Awaiting universal biometric uplink...*")
with gr.TabItem("πŸ•ΈοΈ **BIO-STRESS MAP**"):
gr.Markdown("### **Real-time Clinical Risk Analysis**")
plot_out = gr.Plot()
with gr.TabItem("πŸ“‘ **RAW TRANSCRIPT**"):
transcript_out = gr.Textbox(label="Diarized Stream", lines=15, interactive=False)
# --- ACTION BINDING ---
run_btn.click(
fn=run_universal_workflow,
inputs=[audio_in, file_in, lang_in],
outputs=[report_out, plot_out, transcript_out, perf_out],
api_name=False
)
gr.HTML("<p style='text-align: center; color: #444; margin-top: 50px;'>CHRONOS-SCRIBE ULTRA | THE UNIVERSAL STANDARD | Β© 2026 SOVEREIGN AI</p>")
if __name__ == "__main__":
demo.launch()