Spaces:
Running
Running
Upload 2 files
Browse files- app.py +54 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
3 |
+
from qwen_vl_utils import process_vision_info
|
4 |
+
|
5 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
6 |
+
"prithivMLmods/Radiology-Infer-Mini", torch_dtype="auto", device_map="auto"
|
7 |
+
)
|
8 |
+
processor = AutoProcessor.from_pretrained("prithivMLmods/Radiology-Infer-Mini")
|
9 |
+
|
10 |
+
def generate_report(image, text):
|
11 |
+
# Prepare the message
|
12 |
+
messages = [
|
13 |
+
{
|
14 |
+
"role": "user",
|
15 |
+
"content": [
|
16 |
+
{"type": "image", "image": image},
|
17 |
+
{"type": "text", "text": text},
|
18 |
+
],
|
19 |
+
}
|
20 |
+
]
|
21 |
+
|
22 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
23 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
24 |
+
inputs = processor(
|
25 |
+
text=[text],
|
26 |
+
images=image_inputs,
|
27 |
+
videos=video_inputs,
|
28 |
+
padding=True,
|
29 |
+
return_tensors="pt",
|
30 |
+
)
|
31 |
+
inputs = inputs.to("cpu")
|
32 |
+
|
33 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
34 |
+
generated_ids_trimmed = [
|
35 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
36 |
+
]
|
37 |
+
output_text = processor.batch_decode(
|
38 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
39 |
+
)
|
40 |
+
|
41 |
+
return output_text[0]
|
42 |
+
|
43 |
+
interface = gr.Interface(
|
44 |
+
fn=generate_report,
|
45 |
+
inputs=[
|
46 |
+
gr.Image(type="pil", label="Upload Image"),
|
47 |
+
gr.Textbox(label="Enter Description/Query", placeholder="Enter your query here..."),
|
48 |
+
],
|
49 |
+
outputs=gr.Textbox(label="Generated Report"),
|
50 |
+
title="Pter.AI Report Generator",
|
51 |
+
description="Upload a medical image and provide a description/query to generate a radiology report.",
|
52 |
+
)
|
53 |
+
|
54 |
+
interface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
requests
|
4 |
+
Pillow
|
5 |
+
open_clip_torch
|
6 |
+
ftfy
|
7 |
+
|
8 |
+
gradio
|