Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,67 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
from
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
)
|
17 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from medrax.agent import Agent
|
3 |
+
from medrax.tools import ChestXRayClassifierTool, ChestXRaySegmentationTool, XRayVQATool
|
4 |
+
from medrax.utils import load_prompts_from_file
|
5 |
+
from langchain_openai import ChatOpenAI
|
6 |
+
from langgraph.checkpoint.memory import MemorySaver
|
7 |
|
8 |
+
# MedRAX ajanını başlatma fonksiyonu (main.py'den uyarlandı)
|
9 |
+
def initialize_agent(model="chatgpt-4o-latest", temperature=0.2):
|
10 |
+
# Sistem promptunu yükle
|
11 |
+
prompts = load_prompts_from_file("medrax/docs/system_prompts.txt")
|
12 |
+
prompt = prompts["MEDICAL_ASSISTANT"]
|
13 |
|
14 |
+
# Kullanılacak araçlar
|
15 |
+
tools_dict = {
|
16 |
+
"ChestXRayClassifierTool": ChestXRayClassifierTool(device="cuda"),
|
17 |
+
"ChestXRaySegmentationTool": ChestXRaySegmentationTool(device="cuda"),
|
18 |
+
"XRayVQATool": XRayVQATool(cache_dir="/model-weights", device="cuda"),
|
19 |
+
}
|
20 |
|
21 |
+
# Bellek ve model ayarları
|
22 |
+
checkpointer = MemorySaver()
|
23 |
+
model = ChatOpenAI(model=model, temperature=temperature)
|
24 |
+
|
25 |
+
# Ajanı başlat
|
26 |
+
agent = Agent(
|
27 |
+
model,
|
28 |
+
tools=list(tools_dict.values()),
|
29 |
+
log_tools=True,
|
30 |
+
log_dir="logs",
|
31 |
+
system_prompt=prompt,
|
32 |
+
checkpointer=checkpointer,
|
33 |
)
|
34 |
+
return agent
|
35 |
+
|
36 |
+
# Gradio arayüzü için analiz fonksiyonu
|
37 |
+
def analyze_xray(image, question):
|
38 |
+
# Ajanı başlat
|
39 |
+
agent = initialize_agent()
|
40 |
+
|
41 |
+
# Görüntüyü ve soruyu ajana ilet
|
42 |
+
# Not: Bu kısım MedRAX'in gerçek işleyişine bağlı olarak özelleştirilmeli
|
43 |
+
response = agent.run(f"Analyze this chest X-ray image and answer: {question}", image=image)
|
44 |
+
return response
|
45 |
+
|
46 |
+
# Gradio arayüzü
|
47 |
+
with gr.Blocks(title="MedRAX - Chest X-ray Analysis") as demo:
|
48 |
+
gr.Markdown("# MedRAX: Medical Reasoning Agent for Chest X-ray")
|
49 |
+
gr.Markdown("Upload a chest X-ray image and ask a question about it.")
|
50 |
+
|
51 |
+
with gr.Row():
|
52 |
+
with gr.Column():
|
53 |
+
image_input = gr.Image(type="pil", label="Upload Chest X-ray")
|
54 |
+
question_input = gr.Textbox(label="Your Question", placeholder="E.g., Is there a sign of pneumonia?")
|
55 |
+
submit_btn = gr.Button("Analyze")
|
56 |
+
with gr.Column():
|
57 |
+
output_text = gr.Textbox(label="Analysis Result", interactive=False)
|
58 |
+
|
59 |
+
# Butona tıklayınca analiz yap
|
60 |
+
submit_btn.click(
|
61 |
+
fn=analyze_xray,
|
62 |
+
inputs=[image_input, question_input],
|
63 |
+
outputs=output_text
|
64 |
+
)
|
65 |
+
|
66 |
+
# Uygulamayı başlat
|
67 |
+
demo.launch()
|