GROQ_LLAMA_API / app.py
Muhammadidrees's picture
Create app.py
7b584bb verified
raw
history blame
4.85 kB
import gradio as gr
from groq import Groq
import os
# 🔹 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, age, gender, height, weight):
# ----- System Prompt -----
system_message = (
"You are an AI health assistant that only analyzes lab reports based on the given Levine Biomarkers "
"and generates clear, structured, and patient-friendly summaries.\n"
"Your role is to transform raw lab values into a structured medical report with actionable insights "
"but never recommend medicine and never calculate anything else.\n"
"Follow this exact output format:\n\n"
"Tabular Mapping\n"
"- This section must always include a Markdown table.\n"
"- The table must contain exactly four columns:\n"
"| Biomarker | Value | Status (Low/Normal/High) | AI-Inferred Insight |\n"
"- Include ALL 9 Levine biomarkers (Albumin, Creatinine, Glucose, CRP, MCV, RDW, ALP, WBC, Lymphocytes).\n"
"- The first row after the header must begin directly with 'Albumin'.\n"
"- Do NOT add any index numbers or empty rows.\n"
"- Each biomarker must appear exactly once as a separate row.\n\n"
"Executive Summary\n"
"- List Top 3 Priorities.\n"
"- Highlight Key Strengths.\n\n"
"System-Specific Analysis\n"
"- Status: “Optimal” | “Monitor” | “Needs Attention”.\n"
"- Write a 2–3 sentence explanation in plain language.\n\n"
"Personalized Action Plan\n"
"- Nutrition, Lifestyle, Medical, Testing.\n\n"
"Interaction Alerts\n"
"- Note possible interactions between lab markers.\n\n"
"Constraints:\n"
"- Never provide direct diagnosis, prescriptions, or medical treatment.\n"
"- Never give anything that isn't present in the input.\n"
"- Always recommend consulting a doctor.\n"
"- Don't show input in output.\n"
"- Also give normal reference ranges.\n"
"- Keep the language simple, clear, and supportive."
)
# ----- 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} %"
)
# ----- Call Groq API -----
completion = client.chat.completions.create(
model=MODEL_ID,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": user_message}
],
temperature=0.2,
max_tokens=2000,
top_p=0.9,
stream=False # set True if you want real-time token streaming
)
return completion.choices[0].message.content
# ---------------- Gradio UI ----------------
with gr.Blocks() as demo:
gr.Markdown("## 🧪 AI Health Assistant (Levine Biomarkers via Groq Llama-3.3-70B)")
with gr.Row():
with gr.Column():
albumin = gr.Textbox(label="Albumin (g/dL)", value="4.5")
creatinine = gr.Textbox(label="Creatinine (mg/dL)", value="1.5")
glucose = gr.Textbox(label="Glucose (mg/dL, fasting)", value="160")
crp = gr.Textbox(label="CRP (mg/L)", value="2.5")
mcv = gr.Textbox(label="MCV (fL)", value="150")
rdw = gr.Textbox(label="RDW (%)", value="15")
alp = gr.Textbox(label="ALP (U/L)", value="146")
wbc = gr.Textbox(label="WBC (10^3/μL)", value="10.5")
lymphocytes = gr.Textbox(label="Lymphocytes (%)", value="38")
with gr.Column():
age = gr.Textbox(label="Age (years)", value="30")
gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
height = gr.Textbox(label="Height (cm)", value="123")
weight = gr.Textbox(label="Weight (kg)", value="60")
output = gr.Textbox(label="AI Health Report", lines=30)
btn = gr.Button("Generate Report")
btn.click(
respond,
inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes, age, gender, height, weight],
outputs=output
)
if __name__ == "__main__":
demo.launch()