Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,32 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
from PIL import Image
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
from datetime import datetime
|
| 7 |
|
|
|
|
| 8 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 9 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
out = model.generate(**inputs)
|
| 16 |
-
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 17 |
-
|
| 18 |
-
date_str = datetime.now().strftime("%Y-%m-%d")
|
| 19 |
-
pdf = FPDF()
|
| 20 |
-
pdf.add_page()
|
| 21 |
-
pdf.set_font("Arial", size=12)
|
| 22 |
-
pdf.multi_cell(0, 10, f"Daily Progress Report - {date_str}\n\nCaption: {caption}")
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return caption
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 2 |
from PIL import Image
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
|
|
|
| 5 |
|
| 6 |
+
# Load BLIP model and processor
|
| 7 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 8 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 9 |
+
model.eval()
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
model.to(device)
|
| 12 |
|
| 13 |
+
# Inference function
|
| 14 |
+
def generate_caption(image):
|
| 15 |
+
if image.mode != "RGB":
|
| 16 |
+
image = image.convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
|
| 19 |
+
output = model.generate(**inputs, max_new_tokens=50)
|
| 20 |
+
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 21 |
+
return caption
|
| 22 |
|
| 23 |
+
# Gradio interface
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=generate_caption,
|
| 26 |
+
inputs=gr.Image(type="pil"),
|
| 27 |
+
outputs="text",
|
| 28 |
+
title="Construction Site Image-to-Text Generator",
|
| 29 |
+
description="Upload a site photo. The model will detect and describe construction activities."
|
| 30 |
)
|
| 31 |
|
| 32 |
+
iface.launch()
|
|
|