Spaces:
Sleeping
Sleeping
File size: 6,015 Bytes
7b584bb 2a45801 7b584bb 30142d7 7b584bb 30142d7 9efbf8e 30142d7 7b584bb 30142d7 7b584bb 30142d7 7b584bb 30142d7 7b584bb 30142d7 7b584bb 30142d7 7b584bb 0a59616 7b584bb 9ebeb3d 7b584bb 30142d7 0a59616 7b584bb 0a59616 7b584bb 0a59616 7b584bb e58133f 7b584bb e58133f 389e8a8 30142d7 e58133f 30142d7 e58133f 7b584bb e58133f |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
import gradio as gr
from groq import Groq
import os
import markdown
# 🔹 Set your Groq API Key securely
os.environ["GROQ_API_KEY"] = "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"
# 🔹 Initialize Groq client
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
# 🔹 Define model
MODEL_ID = "llama-3.3-70b-versatile"
# ---------------- AI Response Function ----------------
def respond(albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes,
hemoglobin, pv, age, gender, height, weight):
# ----- System Prompt -----
system_message = (
"You are an AI Health Assistant that analyzes laboratory biomarkers "
"and generates structured, patient-friendly health summaries.\n\n"
"Your task is to evaluate the provided biomarkers and generate an AI-driven medical report "
"with insights, observations, and clear explanations.\n"
"You must strictly follow this structured format:\n\n"
"### Tabular Mapping\n"
"- Always include a Markdown table with exactly four columns:\n"
"| Biomarker | Value | Status (Low/Normal/High) | AI-Inferred Insight | Reference Range |\n"
"- Include **all available biomarkers** below:\n"
"Albumin, Creatinine, Glucose, CRP, MCV, RDW, ALP, WBC, Lymphocytes, Hemoglobin, Plasma Viscosity (PV)\n"
"- The first row after the header must begin directly with 'Albumin'.\n"
"- Each biomarker must appear exactly once as a separate row.\n\n"
"### Executive Summary\n"
"- List Top 3 Health Priorities.\n"
"- Highlight Key Strengths or normal biomarkers.\n\n"
"### System-Specific Analysis\n"
"- Summarize findings grouped by organ systems (Liver, Kidney, Immune, Blood, etc.).\n"
"- Status: “Optimal” | “Monitor” | “Needs Attention”.\n"
"- Provide 2–3 sentences of explanation in plain, supportive language.\n\n"
"### Personalized Action Plan\n"
"- Provide categorized recommendations (Nutrition, Lifestyle, Testing, Medical Consultation).\n"
"- Never recommend medication or treatment.\n\n"
"### Interaction Alerts\n"
"- Highlight potential relationships between markers (e.g., high CRP + low Albumin).\n\n"
"### Constraints\n"
"- Never give a diagnosis or prescribe medicine.\n"
"- Never use data not present in the input.\n"
"- Always recommend consulting a healthcare professional.\n"
"- Always include normal reference ranges for each biomarker.\n"
"- Use simple, clear, patient-friendly language."
)
# ----- User Message -----
user_message = (
f"Patient Information:\n"
f"- Age: {age} years\n"
f"- Gender: {gender}\n"
f"- Height: {height} cm\n"
f"- Weight: {weight} kg\n\n"
f"Biomarker Values:\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: {hemoglobin} g/dL\n"
f"- Plasma Viscosity (PV): {pv} mPa·s"
)
try:
# --- Query model ---
response = client.chat.completions.create(
model=MODEL_ID,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": user_message}
],
temperature=0.4,
max_tokens=2500,
top_p=0.9,
stream=False
)
# --- Get model reply and convert Markdown → HTML ---
reply = response.choices[0].message.content
html_output = markdown.markdown(
reply,
extensions=["tables", "fenced_code", "nl2br"]
)
except Exception as e:
html_output = f"<p style='color:red;'>⚠️ Error: {str(e)}</p>"
return html_output
# --- Gradio Interface ---
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 structured insights, risk assessment, and recommendations.
"""
)
# --- Basic Info ---
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)
# --- Biomarkers ---
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 + Output ---
submit_btn = gr.Button("📤 Generate Medical Report")
output_box = gr.HTML(label="🧠 AI-Generated Medical Report (Rendered in Markup)")
submit_btn.click(
respond,
inputs=[
age, gender, height, weight,
albumin, creatinine, glucose, crp, mcv,
rdw, alp, wbc, lymphocytes, hb, pv
],
outputs=output_box
)
demo.launch()
|