Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import time
|
| 4 |
-
from PIL import Image
|
| 5 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 6 |
-
from
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load model and processor
|
| 9 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 10 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
-
model.to(device)
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 21 |
-
output = model.generate(**inputs, max_new_tokens=50)
|
| 22 |
-
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
for i, img in enumerate(images[:10]): # Limit to 10 images
|
| 33 |
-
caption = generate_caption(img)
|
| 34 |
-
results.append(f"Image {i+1}: {caption}")
|
| 35 |
-
pdf_file = create_pdf(results)
|
| 36 |
-
return "\n\n".join(results), pdf_file
|
| 37 |
|
| 38 |
-
|
| 39 |
-
fn=
|
| 40 |
-
inputs=gr.
|
| 41 |
-
outputs=["
|
| 42 |
-
title="Auto
|
| 43 |
-
description="Upload construction site
|
| 44 |
-
allow_flagging="never"
|
| 45 |
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from fpdf import FPDF
|
| 5 |
+
import os
|
| 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 |
+
def analyze_image(image):
|
| 12 |
+
raw_image = Image.fromarray(image)
|
| 13 |
+
text = "Describe the construction site"
|
| 14 |
+
inputs = processor(raw_image, text, return_tensors="pt")
|
| 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 |
+
os.makedirs("reports", exist_ok=True)
|
| 25 |
+
file_path = f"reports/DPR_{date_str}.pdf"
|
| 26 |
+
pdf.output(file_path)
|
| 27 |
+
return caption, file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=analyze_image,
|
| 31 |
+
inputs=gr.Image(type="numpy", label="Upload Site Photo"),
|
| 32 |
+
outputs=[gr.Textbox(label="Generated Caption"), gr.File(label="Download DPR PDF")],
|
| 33 |
+
title="Auto DPR Generator",
|
| 34 |
+
description="Upload a construction site image to generate a Daily Progress Report."
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
+
demo.launch()
|