File size: 4,740 Bytes
c6c2b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b78792
 
 
 
 
 
 
 
 
 
 
3b69a93
c6c2b6a
5b78792
 
5eb05ee
bc58772
5b78792
bc58772
5b78792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eb05ee
a1a3a62
824a6f2
5b78792
 
 
 
9bc45a2
 
 
5eb05ee
d8934b9
ec88703
 
d8934b9
5b78792
 
 
d8934b9
5b78792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eb05ee
5b78792
 
 
 
 
 
5eb05ee
 
 
 
c6c2b6a
5b78792
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# import gradio as gr
# from transformers import BlipProcessor, BlipForConditionalGeneration
# from PIL import Image
# import torch

# # Load model and processor from your Hugging Face repo
# model_id = "khalednabawi11/blip-roco-model"

# processor = BlipProcessor.from_pretrained(model_id)
# model = BlipForConditionalGeneration.from_pretrained(model_id)
# model.eval()

# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# model.to(device)

# def generate_caption(image):
#     # Preprocess
#     inputs = processor(image, return_tensors="pt").to(device)

#     # Generate caption
#     with torch.no_grad():
#         output = model.generate(**inputs, max_new_tokens=250, num_beams=5)

#     # Decode
#     caption = processor.decode(output[0], skip_special_tokens=True)
#     return caption

# # def generate_caption(image):
# #     prompt = "Radiology report:"
# #     inputs = processor(images=image, text=prompt, return_tensors="pt").to(device)

# #     output = model.generate(
# #         **inputs,
# #         max_length=250,
# #         num_beams=3,
# #         repetition_penalty=1.2,
# #         length_penalty=0.0,
# #         early_stopping=True,
# #         # truncation=True
# #     )

# #     caption = processor.batch_decode(output, skip_special_tokens=True)[0]

# #     return caption.strip()



# # Gradio UI
# demo = gr.Interface(
#     fn=generate_caption,
#     inputs=gr.Image(type="pil", label="Upload an Image"),
#     outputs=gr.Textbox(label="Generated Caption"),
#     title="BLIP Medical Caption Generator",
#     description="Upload an image and get a caption generated by your fine-tuned BLIP model.",
# )

# if __name__ == "__main__":
#     demo.launch()


# import os
# import gradio as gr
# from transformers import AutoProcessor, AutoModelForVision2Seq, AutoModelForImageTextToText
# from PIL import Image
# import torch
# from huggingface_hub import login
# hf_token = os.getenv("hf_token")
# login(token=hf_token)

# processor = AutoProcessor.from_pretrained("google/medgemma-4b-it")
# model = AutoModelForImageTextToText.from_pretrained("google/medgemma-4b-it", device_map = "cpu")


# processor = AutoProcessor.from_pretrained("google/gemma-3n-E4B-it-litert-preview")
# model = AutoModelForImageTextToText.from_pretrained("google/gemma-3n-E4B-it-litert-preview", device_map = "cpu")


# model.eval()

# # Inference function
# def generate_caption(image, prompt):
#     inputs = processor(images=image, text=prompt, return_tensors="pt")

#     with torch.no_grad():
#         outputs = model.generate(
#             **inputs,
#             max_new_tokens=256,
#             num_beams=4,
#             early_stopping=True
#         )

#     caption = processor.decode(outputs[0], skip_special_tokens=True)
#     return caption.strip()

# # Gradio UI
# demo = gr.Interface(
#     fn=generate_caption,
#     inputs=[
#         gr.Image(type="pil", label="Upload Medical Image"),
#         gr.Textbox(label="Prompt", value="Radiology report:")
#     ],
#     outputs=gr.Textbox(label="Generated Caption"),
#     title="Medical Scan Report Generator",
#     description="Upload a medical image and enter a prompt (e.g. 'Radiology report:') to generate a diagnostic caption.",
# )

# if __name__ == "__main__":
#     demo.launch()


import os
import torch
from transformers import pipeline
from PIL import Image
import gradio as gr
from huggingface_hub import login
hf_token = os.getenv("hf_token")
login(token=hf_token)


# model_id = "google/gemma-3n-E4B-it-litert-preview"
model_id = "google/medgemma-4b-it"

# Load the MedGemma pipeline
pipe = pipeline(
    "image-text-to-text",
    model=model_id,
    torch_dtype=torch.bfloat16,
    device="cuda" if torch.cuda.is_available() else "cpu",
)

# Inference function
def analyze_scan(image):
    messages = [
        {
            "role": "system",
            "content": [{"type": "text", "text": "You are an expert radiologist."}]
        },
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Describe this Medical Scan Image Giving a full detailed report"},
                {"type": "image", "image": image},
            ]
        }
    ]
    output = pipe(text=messages, max_new_tokens=200)
    return output[0]["generated_text"][-1]["content"]

# Gradio Interface
demo = gr.Interface(
    fn=analyze_scan,
    inputs=gr.Image(type="pil", label="Upload Medical Scan"),
    outputs=gr.Textbox(label="Scanalyze Medical Scan Report"),
    title="Medical Scan Analyzer (MedGemma)",
    description="Upload a Medical Scan image to get an AI-generated diagnostic report using Google's MedGemma model.",
    allow_flagging="never",
)

if __name__ == "__main__":
    demo.launch()