Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load MedGemma
|
| 6 |
+
MODEL_NAME = "google/medgemma-4b-it" # choose the model size you want
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, device_map="auto") # GPU if available
|
| 9 |
+
|
| 10 |
+
# Function to generate response
|
| 11 |
+
def medgemma_chat(prompt, max_length=200):
|
| 12 |
+
"""
|
| 13 |
+
Generates medical responses from MedGemma.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
prompt (str): Medical question, lab results, or patient info.
|
| 17 |
+
max_length (int): Max number of tokens to generate.
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
str: AI-generated response.
|
| 21 |
+
"""
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
output = model.generate(
|
| 25 |
+
**inputs,
|
| 26 |
+
max_length=max_length,
|
| 27 |
+
do_sample=True,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
top_p=0.9
|
| 30 |
+
)
|
| 31 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 32 |
+
return response
|
| 33 |
+
|
| 34 |
+
# Gradio UI
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=medgemma_chat,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Textbox(lines=5, placeholder="Enter patient's info, lab results, or medical question here...", label="Input")
|
| 39 |
+
],
|
| 40 |
+
outputs=[
|
| 41 |
+
gr.Textbox(label="MedGemma Response")
|
| 42 |
+
],
|
| 43 |
+
title="MedGemma Medical Assistant",
|
| 44 |
+
description=(
|
| 45 |
+
"Ask questions or provide patient information. "
|
| 46 |
+
"MedGemma generates medical insights, summaries, and guidance. "
|
| 47 |
+
"⚠️ For educational and research purposes only — not a substitute for professional medical advice."
|
| 48 |
+
),
|
| 49 |
+
examples=[
|
| 50 |
+
["Patient: 45-year-old male, BMI 28, blood pressure 140/90, glucose 7.5 mmol/L. Suggest possible conditions."],
|
| 51 |
+
["Summarize the following lab report: Hemoglobin 11 g/dL, WBC 9 x10^9/L, Platelets 200 x10^9/L."]
|
| 52 |
+
],
|
| 53 |
+
allow_flagging="never"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
iface.launch()
|