Spaces:
Running
Running
| import gradio as gr | |
| import os | |
| from openai import OpenAI | |
| # 🔹 Set your Grok (xAI) API Key securely | |
| os.environ["XAI_API_KEY"] = "xai-2ftlCxHfSh3eCmrzNVWlC8X1r4w06GQTPLK8YFwJdwjgFXK0oW0H6QvR4NN5N0fhkLLEkibGBY7Q1e6m" | |
| # 🔹 Initialize client for Grok | |
| client = OpenAI( | |
| api_key=os.getenv("XAI_API_KEY"), | |
| base_url="https://api.x.ai/v1" # ✅ xAI Grok endpoint | |
| ) | |
| # 🔹 Define model (latest general-purpose) | |
| MODEL_ID = "grok-beta" # or "grok-2", check console for available models | |
| # ---------------- AI Response Function ---------------- | |
| def respond(albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes, | |
| hemoglobin, pv, age, gender, height, weight): | |
| 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" | |
| "| Biomarker | Value | Status (Low/Normal/High) | AI-Inferred Insight |Reference Range|\n" | |
| "Include all available biomarkers: Albumin, Creatinine, Glucose, CRP, MCV, RDW, ALP, WBC, " | |
| "Lymphocytes, Hemoglobin, Plasma Viscosity (PV).\n\n" | |
| "### Executive Summary\n" | |
| "- Top 3 Health Priorities.\n" | |
| "- Key Strengths (normal biomarkers).\n\n" | |
| "### System-Specific Analysis\n" | |
| "- Organ systems: Liver, Kidney, Immune, Blood, etc.\n" | |
| "- Status: “Optimal” | “Monitor” | “Needs Attention”.\n" | |
| "- Write concise, supportive explanations.\n\n" | |
| "### Personalized Action Plan\n" | |
| "- Recommendations: Nutrition, Lifestyle, Testing, Consultation.\n" | |
| "- Never recommend medication.\n\n" | |
| "### Interaction Alerts\n" | |
| "- Highlight potential relationships (e.g., high CRP + low Albumin).\n\n" | |
| "### Constraints\n" | |
| "- No diagnosis or prescriptions.\n" | |
| "- Use only provided data.\n" | |
| "- Always recommend seeing a healthcare professional.\n" | |
| "- Include normal reference ranges for each biomarker.\n" | |
| "- Use patient-friendly language." | |
| ) | |
| 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" | |
| ) | |
| 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 | |
| ) | |
| return completion.choices[0].message.content | |
| # ---------------- Gradio UI ---------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🧪 AI Health Assistant (Extended Biomarkers via Grok API)") | |
| 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") | |
| hemoglobin = gr.Textbox(label="Hemoglobin (g/dL)", value="13.5") | |
| pv = gr.Textbox(label="Plasma Viscosity (mPa·s)", value="1.7") | |
| 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="170") | |
| weight = gr.Textbox(label="Weight (kg)", value="65") | |
| 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, hemoglobin, pv, age, gender, height, weight | |
| ], | |
| outputs=output | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |