Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,49 +12,51 @@ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
| 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,
|
|
|
|
|
|
|
| 16 |
# ----- System Prompt -----
|
| 17 |
system_message = (
|
| 18 |
-
"You are an AI
|
| 19 |
-
"and generates
|
| 20 |
-
"Your
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"Tabular Mapping\n"
|
| 24 |
-
"-
|
| 25 |
-
"- The table must contain exactly four columns:\n"
|
| 26 |
"| Biomarker | Value | Status (Low/Normal/High) | AI-Inferred Insight |\n"
|
| 27 |
-
"- Include
|
|
|
|
| 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 |
-
"-
|
| 37 |
-
"Personalized Action Plan\n"
|
| 38 |
-
"- Nutrition, Lifestyle,
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
-
"
|
| 42 |
-
"
|
| 43 |
-
"- Never give
|
| 44 |
-
"-
|
| 45 |
-
"-
|
| 46 |
-
"-
|
| 47 |
-
"-
|
| 48 |
)
|
| 49 |
|
| 50 |
# ----- User Message -----
|
| 51 |
user_message = (
|
| 52 |
-
f"Patient
|
| 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"
|
| 58 |
f"- Albumin: {albumin} g/dL\n"
|
| 59 |
f"- Creatinine: {creatinine} mg/dL\n"
|
| 60 |
f"- Glucose: {glucose} mg/dL\n"
|
|
@@ -63,7 +65,9 @@ def respond(albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes,
|
|
| 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 -----
|
|
@@ -76,7 +80,7 @@ def respond(albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes,
|
|
| 76 |
temperature=0.2,
|
| 77 |
max_tokens=2000,
|
| 78 |
top_p=0.9,
|
| 79 |
-
stream=False
|
| 80 |
)
|
| 81 |
|
| 82 |
return completion.choices[0].message.content
|
|
@@ -84,7 +88,7 @@ def respond(albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes,
|
|
| 84 |
|
| 85 |
# ---------------- Gradio UI ----------------
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
-
gr.Markdown("## 🧪 AI Health Assistant (
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column():
|
|
@@ -97,19 +101,24 @@ with gr.Blocks() as demo:
|
|
| 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="
|
| 105 |
-
weight = gr.Textbox(label="Weight (kg)", value="
|
| 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=[
|
|
|
|
|
|
|
|
|
|
| 113 |
outputs=output
|
| 114 |
)
|
| 115 |
|
|
|
|
| 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,
|
| 16 |
+
hemoglobin, pv, age, gender, height, weight):
|
| 17 |
+
|
| 18 |
# ----- System Prompt -----
|
| 19 |
system_message = (
|
| 20 |
+
"You are an AI Health Assistant that analyzes laboratory biomarkers "
|
| 21 |
+
"and generates structured, patient-friendly health summaries.\n\n"
|
| 22 |
+
"Your task is to evaluate the provided biomarkers and generate an AI-driven medical report "
|
| 23 |
+
"with insights, observations, and clear explanations.\n"
|
| 24 |
+
"You must strictly follow this structured format:\n\n"
|
| 25 |
+
"### Tabular Mapping\n"
|
| 26 |
+
"- Always include a Markdown table with exactly four columns:\n"
|
|
|
|
| 27 |
"| Biomarker | Value | Status (Low/Normal/High) | AI-Inferred Insight |\n"
|
| 28 |
+
"- Include **all available biomarkers** below:\n"
|
| 29 |
+
"Albumin, Creatinine, Glucose, CRP, MCV, RDW, ALP, WBC, Lymphocytes, Hemoglobin, Plasma Viscosity (PV)\n"
|
| 30 |
"- The first row after the header must begin directly with 'Albumin'.\n"
|
|
|
|
| 31 |
"- Each biomarker must appear exactly once as a separate row.\n\n"
|
| 32 |
+
"### Executive Summary\n"
|
| 33 |
+
"- List Top 3 Health Priorities.\n"
|
| 34 |
+
"- Highlight Key Strengths or normal biomarkers.\n\n"
|
| 35 |
+
"### System-Specific Analysis\n"
|
| 36 |
+
"- Summarize findings grouped by organ systems (Liver, Kidney, Immune, Blood, etc.).\n"
|
| 37 |
"- Status: “Optimal” | “Monitor” | “Needs Attention”.\n"
|
| 38 |
+
"- Provide 2–3 sentences of explanation in plain, supportive language.\n\n"
|
| 39 |
+
"### Personalized Action Plan\n"
|
| 40 |
+
"- Provide categorized recommendations (Nutrition, Lifestyle, Testing, Medical Consultation).\n"
|
| 41 |
+
"- Never recommend medication or treatment.\n\n"
|
| 42 |
+
"### Interaction Alerts\n"
|
| 43 |
+
"- Highlight potential relationships between markers (e.g., high CRP + low Albumin).\n\n"
|
| 44 |
+
"### Constraints\n"
|
| 45 |
+
"- Never give a diagnosis or prescribe medicine.\n"
|
| 46 |
+
"- Never use data not present in the input.\n"
|
| 47 |
+
"- Always recommend consulting a healthcare professional.\n"
|
| 48 |
+
"- Always include normal reference ranges for each biomarker.\n"
|
| 49 |
+
"- Use simple, clear, patient-friendly language."
|
| 50 |
)
|
| 51 |
|
| 52 |
# ----- User Message -----
|
| 53 |
user_message = (
|
| 54 |
+
f"Patient Information:\n"
|
| 55 |
+
f"- Age: {age} years\n"
|
| 56 |
f"- Gender: {gender}\n"
|
| 57 |
f"- Height: {height} cm\n"
|
| 58 |
f"- Weight: {weight} kg\n\n"
|
| 59 |
+
f"Biomarker Values:\n"
|
| 60 |
f"- Albumin: {albumin} g/dL\n"
|
| 61 |
f"- Creatinine: {creatinine} mg/dL\n"
|
| 62 |
f"- Glucose: {glucose} mg/dL\n"
|
|
|
|
| 65 |
f"- RDW: {rdw} %\n"
|
| 66 |
f"- ALP: {alp} U/L\n"
|
| 67 |
f"- WBC: {wbc} x10^3/μL\n"
|
| 68 |
+
f"- Lymphocytes: {lymphocytes} %\n"
|
| 69 |
+
f"- Hemoglobin: {hemoglobin} g/dL\n"
|
| 70 |
+
f"- Plasma Viscosity (PV): {pv} mPa·s"
|
| 71 |
)
|
| 72 |
|
| 73 |
# ----- Call Groq API -----
|
|
|
|
| 80 |
temperature=0.2,
|
| 81 |
max_tokens=2000,
|
| 82 |
top_p=0.9,
|
| 83 |
+
stream=False
|
| 84 |
)
|
| 85 |
|
| 86 |
return completion.choices[0].message.content
|
|
|
|
| 88 |
|
| 89 |
# ---------------- Gradio UI ----------------
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown("## 🧪 AI Health Assistant (Extended Biomarkers via Groq Llama-3.3-70B)")
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column():
|
|
|
|
| 101 |
alp = gr.Textbox(label="ALP (U/L)", value="146")
|
| 102 |
wbc = gr.Textbox(label="WBC (10^3/μL)", value="10.5")
|
| 103 |
lymphocytes = gr.Textbox(label="Lymphocytes (%)", value="38")
|
| 104 |
+
hemoglobin = gr.Textbox(label="Hemoglobin (g/dL)", value="13.5")
|
| 105 |
+
pv = gr.Textbox(label="Plasma Viscosity (mPa·s)", value="1.7")
|
| 106 |
|
| 107 |
with gr.Column():
|
| 108 |
age = gr.Textbox(label="Age (years)", value="30")
|
| 109 |
gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
|
| 110 |
+
height = gr.Textbox(label="Height (cm)", value="170")
|
| 111 |
+
weight = gr.Textbox(label="Weight (kg)", value="65")
|
| 112 |
|
| 113 |
output = gr.Textbox(label="AI Health Report", lines=30)
|
| 114 |
|
| 115 |
btn = gr.Button("Generate Report")
|
| 116 |
btn.click(
|
| 117 |
respond,
|
| 118 |
+
inputs=[
|
| 119 |
+
albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc,
|
| 120 |
+
lymphocytes, hemoglobin, pv, age, gender, height, weight
|
| 121 |
+
],
|
| 122 |
outputs=output
|
| 123 |
)
|
| 124 |
|