File size: 4,195 Bytes
afddc96
6c89169
01d4e47
afddc96
dbaef5a
6c89169
 
01d4e47
 
 
afddc96
 
6c89169
afddc96
 
01d4e47
dbaef5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afddc96
6c89169
dbaef5a
6c89169
 
dbaef5a
 
 
 
6c89169
dbaef5a
afddc96
6c89169
 
 
 
 
 
afddc96
dbaef5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import gradio as gr
from openai import OpenAI

# --- Initialize Hugging Face router client ---
HF_TOKEN = os.getenv("HF_TOKEN")

if not HF_TOKEN:
    raise ValueError("❌ HF_TOKEN not found. Please set it in your Hugging Face Space secrets.")

client = OpenAI(
    base_url="https://router.huggingface.co/v1",
    api_key=HF_TOKEN,
)

# --- Chat handler ---
def generate_report(age, gender, height, weight, albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes, hb, pv):
    # --- System Prompt ---
    system = """You are an advanced Medical Insight Generation AI trained to analyze clinical biomarkers, urine analysis, and lab test results.
Your goal is to generate a medically accurate, empathetic, and client-friendly health report in the following structured format:

1. Executive Summary
2. System-Specific Analysis
3. Personalized Action Plan
4. Interaction Alerts
5. Longevity Metrics
6. Tabular Mapping
7. Enhanced AI Insight
8. AI Insights & Longitudinal Risk Assessment
9. Predictive Longevity Risk Profile
10. Actionable Next Steps

Maintain a professional, compassionate tone and explain medical reasoning in accessible language.
"""

    # --- User message ---
    user_message = (
        f"Patient Info:\n"
        f"- Age: {age}\n"
        f"- Gender: {gender}\n"
        f"- Height: {height} cm\n"
        f"- Weight: {weight} kg\n\n"
        f"Biomarkers:\n"
        f"- Albumin: {albumin} g/dL\n"
        f"- Creatinine: {creatinine} mg/dL\n"
        f"- Glucose: {glucose} mg/dL\n"
        f"- CRP: {crp} mg/L\n"
        f"- MCV: {mcv} fL\n"
        f"- RDW: {rdw} %\n"
        f"- ALP: {alp} U/L\n"
        f"- WBC: {wbc} x10^3/μL\n"
        f"- Lymphocytes: {lymphocytes} %\n"
        f"- Hemoglobin: {hb} g/dL\n"
        f"- Plasma (PV): {pv} mL\n"
    )

    try:
        # --- Send request to model ---
        response = client.chat.completions.create(
            model="openai/gpt-oss-120b:fireworks-ai",
            messages=[
                {"role": "system", "content": system},
                {"role": "user", "content": user_message},
            ],
            temperature=0.7,
            max_tokens=2000,
        )
        reply = response.choices[0].message.content
    except Exception as e:
        reply = f"⚠️ Error: {str(e)}"

    return reply


# --- Gradio UI ---
with gr.Blocks(title="🧬 Biomarker Medical Insight Chatbot") as demo:
    gr.Markdown(
        """
        ## 🧠 AI-Powered Biomarker Report Generator  
        Enter the patient details and biomarkers below.  
        The AI will generate a **comprehensive medical report** with insights, risk assessment, and personalized recommendations.
        """
    )

    with gr.Row():
        age = gr.Number(label="Age", value=45)
        gender = gr.Radio(["Male", "Female"], label="Gender", value="Male")

    with gr.Row():
        height = gr.Number(label="Height (cm)", value=175)
        weight = gr.Number(label="Weight (kg)", value=72)

    gr.Markdown("### 🧫 Biomarker Inputs (Demo Values Pre-filled)")

    with gr.Row():
        albumin = gr.Number(label="Albumin (g/dL)", value=4.2)
        creatinine = gr.Number(label="Creatinine (mg/dL)", value=1.1)
        glucose = gr.Number(label="Glucose (mg/dL)", value=98)

    with gr.Row():
        crp = gr.Number(label="CRP (mg/L)", value=2.5)
        mcv = gr.Number(label="MCV (fL)", value=90.5)
        rdw = gr.Number(label="RDW (%)", value=13.2)

    with gr.Row():
        alp = gr.Number(label="ALP (U/L)", value=110)
        wbc = gr.Number(label="WBC (x10^3/μL)", value=6.8)
        lymphocytes = gr.Number(label="Lymphocytes (%)", value=35)

    with gr.Row():
        hb = gr.Number(label="Hemoglobin (g/dL)", value=14.5)
        pv = gr.Number(label="Plasma (PV) (mL)", value=3000)

    submit_btn = gr.Button("📤 Generate Medical Report")
    output_box = gr.Textbox(label="AI-Generated Medical Report", lines=25)

    submit_btn.click(
        generate_report,
        inputs=[
            age, gender, height, weight,
            albumin, creatinine, glucose, crp, mcv,
            rdw, alp, wbc, lymphocytes, hb, pv
        ],
        outputs=output_box
    )

demo.launch()