Muhammadidrees commited on
Commit
7b584bb
·
verified ·
1 Parent(s): 07b7c56

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ import os
4
+
5
+ # 🔹 Set your Groq API Key securely
6
+ os.environ["GROQ_API_KEY"] = "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"
7
+
8
+ # 🔹 Initialize Groq client
9
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
10
+
11
+ # 🔹 Define model
12
+ MODEL_ID = "llama-3.3-70b-versatile"
13
+
14
+ # ---------------- AI Response Function ----------------
15
+ def respond(albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes, age, gender, height, weight):
16
+ # ----- System Prompt -----
17
+ system_message = (
18
+ "You are an AI health assistant that only analyzes lab reports based on the given Levine Biomarkers "
19
+ "and generates clear, structured, and patient-friendly summaries.\n"
20
+ "Your role is to transform raw lab values into a structured medical report with actionable insights "
21
+ "but never recommend medicine and never calculate anything else.\n"
22
+ "Follow this exact output format:\n\n"
23
+ "Tabular Mapping\n"
24
+ "- This section must always include a Markdown table.\n"
25
+ "- The table must contain exactly four columns:\n"
26
+ "| Biomarker | Value | Status (Low/Normal/High) | AI-Inferred Insight |\n"
27
+ "- Include ALL 9 Levine biomarkers (Albumin, Creatinine, Glucose, CRP, MCV, RDW, ALP, WBC, Lymphocytes).\n"
28
+ "- The first row after the header must begin directly with 'Albumin'.\n"
29
+ "- Do NOT add any index numbers or empty rows.\n"
30
+ "- Each biomarker must appear exactly once as a separate row.\n\n"
31
+ "Executive Summary\n"
32
+ "- List Top 3 Priorities.\n"
33
+ "- Highlight Key Strengths.\n\n"
34
+ "System-Specific Analysis\n"
35
+ "- Status: “Optimal” | “Monitor” | “Needs Attention”.\n"
36
+ "- Write a 2–3 sentence explanation in plain language.\n\n"
37
+ "Personalized Action Plan\n"
38
+ "- Nutrition, Lifestyle, Medical, Testing.\n\n"
39
+ "Interaction Alerts\n"
40
+ "- Note possible interactions between lab markers.\n\n"
41
+ "Constraints:\n"
42
+ "- Never provide direct diagnosis, prescriptions, or medical treatment.\n"
43
+ "- Never give anything that isn't present in the input.\n"
44
+ "- Always recommend consulting a doctor.\n"
45
+ "- Don't show input in output.\n"
46
+ "- Also give normal reference ranges.\n"
47
+ "- Keep the language simple, clear, and supportive."
48
+ )
49
+
50
+ # ----- User Message -----
51
+ user_message = (
52
+ f"Patient Info:\n"
53
+ f"- Age: {age}\n"
54
+ f"- Gender: {gender}\n"
55
+ f"- Height: {height} cm\n"
56
+ f"- Weight: {weight} kg\n\n"
57
+ f"Biomarkers:\n"
58
+ f"- Albumin: {albumin} g/dL\n"
59
+ f"- Creatinine: {creatinine} mg/dL\n"
60
+ f"- Glucose: {glucose} mg/dL\n"
61
+ f"- CRP: {crp} mg/L\n"
62
+ f"- MCV: {mcv} fL\n"
63
+ f"- RDW: {rdw} %\n"
64
+ f"- ALP: {alp} U/L\n"
65
+ f"- WBC: {wbc} x10^3/μL\n"
66
+ f"- Lymphocytes: {lymphocytes} %"
67
+ )
68
+
69
+ # ----- Call Groq API -----
70
+ completion = client.chat.completions.create(
71
+ model=MODEL_ID,
72
+ messages=[
73
+ {"role": "system", "content": system_message},
74
+ {"role": "user", "content": user_message}
75
+ ],
76
+ temperature=0.2,
77
+ max_tokens=2000,
78
+ top_p=0.9,
79
+ stream=False # set True if you want real-time token streaming
80
+ )
81
+
82
+ return completion.choices[0].message.content
83
+
84
+
85
+ # ---------------- Gradio UI ----------------
86
+ with gr.Blocks() as demo:
87
+ gr.Markdown("## 🧪 AI Health Assistant (Levine Biomarkers via Groq Llama-3.3-70B)")
88
+
89
+ with gr.Row():
90
+ with gr.Column():
91
+ albumin = gr.Textbox(label="Albumin (g/dL)", value="4.5")
92
+ creatinine = gr.Textbox(label="Creatinine (mg/dL)", value="1.5")
93
+ glucose = gr.Textbox(label="Glucose (mg/dL, fasting)", value="160")
94
+ crp = gr.Textbox(label="CRP (mg/L)", value="2.5")
95
+ mcv = gr.Textbox(label="MCV (fL)", value="150")
96
+ rdw = gr.Textbox(label="RDW (%)", value="15")
97
+ alp = gr.Textbox(label="ALP (U/L)", value="146")
98
+ wbc = gr.Textbox(label="WBC (10^3/μL)", value="10.5")
99
+ lymphocytes = gr.Textbox(label="Lymphocytes (%)", value="38")
100
+
101
+ with gr.Column():
102
+ age = gr.Textbox(label="Age (years)", value="30")
103
+ gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
104
+ height = gr.Textbox(label="Height (cm)", value="123")
105
+ weight = gr.Textbox(label="Weight (kg)", value="60")
106
+
107
+ output = gr.Textbox(label="AI Health Report", lines=30)
108
+
109
+ btn = gr.Button("Generate Report")
110
+ btn.click(
111
+ respond,
112
+ inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes, age, gender, height, weight],
113
+ outputs=output
114
+ )
115
+
116
+ if __name__ == "__main__":
117
+ demo.launch()