Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
# --- Initialize Hugging Face router client ---
|
|
@@ -13,9 +14,9 @@ client = OpenAI(
|
|
| 13 |
api_key=HF_TOKEN,
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# ---
|
| 17 |
def generate_report(age, gender, height, weight, albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes, hb, pv):
|
| 18 |
-
# --- System
|
| 19 |
system = """You are an advanced Medical Insight Generation AI trained to analyze clinical biomarkers, urine analysis, and lab test results.
|
| 20 |
Your goal is to generate a medically accurate, empathetic, and client-friendly health report in the following structured format:
|
| 21 |
|
|
@@ -33,7 +34,7 @@ Your goal is to generate a medically accurate, empathetic, and client-friendly h
|
|
| 33 |
Maintain a professional, compassionate tone and explain medical reasoning in accessible language.
|
| 34 |
"""
|
| 35 |
|
| 36 |
-
# ---
|
| 37 |
user_message = (
|
| 38 |
f"Patient Info:\n"
|
| 39 |
f"- Age: {age}\n"
|
|
@@ -55,33 +56,40 @@ Maintain a professional, compassionate tone and explain medical reasoning in acc
|
|
| 55 |
)
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
# ---
|
| 59 |
response = client.chat.completions.create(
|
| 60 |
model="openai/gpt-oss-120b:fireworks-ai",
|
| 61 |
messages=[
|
| 62 |
{"role": "system", "content": system},
|
| 63 |
{"role": "user", "content": user_message},
|
| 64 |
],
|
| 65 |
-
temperature=0.
|
| 66 |
-
max_tokens=2000,
|
| 67 |
)
|
|
|
|
|
|
|
| 68 |
reply = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
-
|
| 71 |
|
| 72 |
-
return
|
| 73 |
|
| 74 |
|
| 75 |
-
# --- Gradio
|
| 76 |
with gr.Blocks(title="🧬 Biomarker Medical Insight Chatbot") as demo:
|
| 77 |
gr.Markdown(
|
| 78 |
"""
|
| 79 |
## 🧠 AI-Powered Biomarker Report Generator
|
| 80 |
Enter the patient details and biomarkers below.
|
| 81 |
-
The AI will generate a **comprehensive medical report** with insights, risk assessment, and
|
| 82 |
"""
|
| 83 |
)
|
| 84 |
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
age = gr.Number(label="Age", value=45)
|
| 87 |
gender = gr.Radio(["Male", "Female"], label="Gender", value="Male")
|
|
@@ -90,8 +98,8 @@ with gr.Blocks(title="🧬 Biomarker Medical Insight Chatbot") as demo:
|
|
| 90 |
height = gr.Number(label="Height (cm)", value=175)
|
| 91 |
weight = gr.Number(label="Weight (kg)", value=72)
|
| 92 |
|
|
|
|
| 93 |
gr.Markdown("### 🧫 Biomarker Inputs (Demo Values Pre-filled)")
|
| 94 |
-
|
| 95 |
with gr.Row():
|
| 96 |
albumin = gr.Number(label="Albumin (g/dL)", value=4.2)
|
| 97 |
creatinine = gr.Number(label="Creatinine (mg/dL)", value=1.1)
|
|
@@ -111,8 +119,9 @@ with gr.Blocks(title="🧬 Biomarker Medical Insight Chatbot") as demo:
|
|
| 111 |
hb = gr.Number(label="Hemoglobin (g/dL)", value=14.5)
|
| 112 |
pv = gr.Number(label="Plasma (PV) (mL)", value=3000)
|
| 113 |
|
|
|
|
| 114 |
submit_btn = gr.Button("📤 Generate Medical Report")
|
| 115 |
-
output_box = gr.
|
| 116 |
|
| 117 |
submit_btn.click(
|
| 118 |
generate_report,
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import markdown
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
# --- Initialize Hugging Face router client ---
|
|
|
|
| 14 |
api_key=HF_TOKEN,
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# --- AI processing function ---
|
| 18 |
def generate_report(age, gender, height, weight, albumin, creatinine, glucose, crp, mcv, rdw, alp, wbc, lymphocytes, hb, pv):
|
| 19 |
+
# --- System prompt ---
|
| 20 |
system = """You are an advanced Medical Insight Generation AI trained to analyze clinical biomarkers, urine analysis, and lab test results.
|
| 21 |
Your goal is to generate a medically accurate, empathetic, and client-friendly health report in the following structured format:
|
| 22 |
|
|
|
|
| 34 |
Maintain a professional, compassionate tone and explain medical reasoning in accessible language.
|
| 35 |
"""
|
| 36 |
|
| 37 |
+
# --- Format user message ---
|
| 38 |
user_message = (
|
| 39 |
f"Patient Info:\n"
|
| 40 |
f"- Age: {age}\n"
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
try:
|
| 59 |
+
# --- Query model ---
|
| 60 |
response = client.chat.completions.create(
|
| 61 |
model="openai/gpt-oss-120b:fireworks-ai",
|
| 62 |
messages=[
|
| 63 |
{"role": "system", "content": system},
|
| 64 |
{"role": "user", "content": user_message},
|
| 65 |
],
|
| 66 |
+
temperature=0.5,
|
|
|
|
| 67 |
)
|
| 68 |
+
|
| 69 |
+
# --- Get model reply and convert Markdown → HTML ---
|
| 70 |
reply = response.choices[0].message.content
|
| 71 |
+
html_output = markdown.markdown(
|
| 72 |
+
reply,
|
| 73 |
+
extensions=["tables", "fenced_code", "nl2br"]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
except Exception as e:
|
| 77 |
+
html_output = f"<p style='color:red;'>⚠️ Error: {str(e)}</p>"
|
| 78 |
|
| 79 |
+
return html_output
|
| 80 |
|
| 81 |
|
| 82 |
+
# --- Gradio Interface ---
|
| 83 |
with gr.Blocks(title="🧬 Biomarker Medical Insight Chatbot") as demo:
|
| 84 |
gr.Markdown(
|
| 85 |
"""
|
| 86 |
## 🧠 AI-Powered Biomarker Report Generator
|
| 87 |
Enter the patient details and biomarkers below.
|
| 88 |
+
The AI will generate a **comprehensive medical report** with structured insights, risk assessment, and recommendations.
|
| 89 |
"""
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# --- Basic Info ---
|
| 93 |
with gr.Row():
|
| 94 |
age = gr.Number(label="Age", value=45)
|
| 95 |
gender = gr.Radio(["Male", "Female"], label="Gender", value="Male")
|
|
|
|
| 98 |
height = gr.Number(label="Height (cm)", value=175)
|
| 99 |
weight = gr.Number(label="Weight (kg)", value=72)
|
| 100 |
|
| 101 |
+
# --- Biomarkers ---
|
| 102 |
gr.Markdown("### 🧫 Biomarker Inputs (Demo Values Pre-filled)")
|
|
|
|
| 103 |
with gr.Row():
|
| 104 |
albumin = gr.Number(label="Albumin (g/dL)", value=4.2)
|
| 105 |
creatinine = gr.Number(label="Creatinine (mg/dL)", value=1.1)
|
|
|
|
| 119 |
hb = gr.Number(label="Hemoglobin (g/dL)", value=14.5)
|
| 120 |
pv = gr.Number(label="Plasma (PV) (mL)", value=3000)
|
| 121 |
|
| 122 |
+
# --- Submit + Output ---
|
| 123 |
submit_btn = gr.Button("📤 Generate Medical Report")
|
| 124 |
+
output_box = gr.HTML(label="🧠 AI-Generated Medical Report (Rendered in Markup)")
|
| 125 |
|
| 126 |
submit_btn.click(
|
| 127 |
generate_report,
|