Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model and processor from your Hugging Face repo
|
| 7 |
+
model_id = "khalednabawi11/blip-roco-weights-v2"
|
| 8 |
+
|
| 9 |
+
processor = BlipProcessor.from_pretrained(model_id)
|
| 10 |
+
model = BlipForConditionalGeneration.from_pretrained(model_id)
|
| 11 |
+
model.eval()
|
| 12 |
+
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
model.to(device)
|
| 15 |
+
|
| 16 |
+
def generate_caption(image):
|
| 17 |
+
# Preprocess
|
| 18 |
+
inputs = processor(image, return_tensors="pt").to(device)
|
| 19 |
+
|
| 20 |
+
# Generate caption
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
output = model.generate(**inputs, max_new_tokens=50)
|
| 23 |
+
|
| 24 |
+
# Decode
|
| 25 |
+
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 26 |
+
return caption
|
| 27 |
+
|
| 28 |
+
# Gradio UI
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=generate_caption,
|
| 31 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 32 |
+
outputs=gr.Textbox(label="Generated Caption"),
|
| 33 |
+
title="BLIP Medical Caption Generator",
|
| 34 |
+
description="Upload an image and get a caption generated by your fine-tuned BLIP model.",
|
| 35 |
+
examples=["example1.png", "example2.png"] # Optional: add example images in your repo
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|