rajibbhtto/medical-imaging-ai-agent
import gradio as gr from PIL import Image import torch import torchvision.transforms as T import os
-----------------------------
1️⃣ Setup (model placeholder)
-----------------------------
device = "cuda" if torch.cuda.is_available() else "cpu"
Agar tu chahe to yahan apna trained model load kar sakta hai:
model = torch.load("bestmodel.pth", maplocation=device)
model.eval()
Image transform
transform = T.Compose([ T.Resize((224, 224)), T.ToTensor(), T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ])
-----------------------------
2️⃣ Dummy Prediction Function
-----------------------------
def predictimage(img: Image.Image): # (Yahan tu real model inference lagayega, abhi demo output de rahe hain) x = transform(img).unsqueeze(0).to(device) # with torch.nograd(): # preds = model(x) # probs = torch.softmax(preds, dim=1) # pred_idx = probs.argmax().item() # confidence = probs.max().item()
# Demo output predlabel = "No abnormality detected (Demo)" confidence = 0.88 explanation = f"Model predicts: {predlabel} (confidence {confidence:.2f}).\n⚠️ This is a demo output, not a medical diagnosis." return {"label": pred_label, "confidence": f"{confidence:.2f}", "explanation": explanation}
-----------------------------
3️⃣ Chat Function
-----------------------------
def chatwithagent(img, userquestion): pred = predictimage(img)
# Prompt banate hain jisse LLM ya chatbot explain kare prompt = f""" User Question: {user_question} Model Output: {pred['label']} (confidence {pred['confidence']}) Explain in simple language with disclaimer. """
# Agar OpenAI ya koi LLM API use karni ho to: # import openai # openai.apikey = os.getenv("OPENAIAPIKEY") # response = openai.ChatCompletion.create( # model="gpt-4o-mini", # messages=[{"role":"user","content":prompt}], # maxtokens=200 # ) # answer = response['choices'][0]['message']['content']
# Abhi LLM band hai, hum simple response de rahe hain: answer = ( f"🤖 Based on model results:\n" f"{pred['explanation']}\n\n" f"Your question: {user_question}\n" f"This image seems normal, but always consult a qualified doctor for medical advice." )
return answer
-----------------------------
4️⃣ Gradio Interface
-----------------------------
demo = gr.Interface( fn=chatwithagent, inputs=[ gr.Image(type="pil", label="Upload Medical Image (e.g., X-ray)"), gr.Textbox(lines=2, placeholder="Ask something about the image...") ], outputs=gr.Textbox(label="AI Response", lines=10), title="🩻 Medical Imaging AI Agent (Demo)", description=( "Upload a de-identified medical image to get a demo analysis.\n" "⚠️ This tool is for educational/demo purposes only and not for clinical use." ), theme="default" )
if _name == "main_": demo.launch()
