Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,6 @@ from PIL import Image
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from datetime import datetime
|
| 6 |
-
from reportlab.lib.pagesizes import letter
|
| 7 |
-
from reportlab.pdfgen import canvas
|
| 8 |
|
| 9 |
# Load BLIP model and processor
|
| 10 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
@@ -13,7 +11,7 @@ model.eval()
|
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
model.to(device)
|
| 15 |
|
| 16 |
-
# Inference function to generate captions
|
| 17 |
def generate_captions_from_image(image):
|
| 18 |
if image.mode != "RGB":
|
| 19 |
image = image.convert("RGB")
|
|
@@ -25,12 +23,12 @@ def generate_captions_from_image(image):
|
|
| 25 |
|
| 26 |
return caption
|
| 27 |
|
| 28 |
-
# Function to generate the daily progress report
|
| 29 |
def generate_dpr(files):
|
| 30 |
dpr_text = []
|
| 31 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
|
| 33 |
-
# Add header to the
|
| 34 |
dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
|
| 35 |
|
| 36 |
# Process each uploaded file (image)
|
|
@@ -48,32 +46,16 @@ def generate_dpr(files):
|
|
| 48 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
| 49 |
dpr_text.append(dpr_section)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 54 |
-
c.drawString(100, 750, "Daily Progress Report")
|
| 55 |
-
c.drawString(100, 730, f"Generated on: {current_time}")
|
| 56 |
|
| 57 |
-
|
| 58 |
-
y_position = 700
|
| 59 |
-
for section in dpr_text:
|
| 60 |
-
c.drawString(100, y_position, section)
|
| 61 |
-
y_position -= 100 # Move down for the next section
|
| 62 |
-
if y_position < 100:
|
| 63 |
-
c.showPage()
|
| 64 |
-
y_position = 750
|
| 65 |
-
|
| 66 |
-
c.save()
|
| 67 |
-
|
| 68 |
-
return pdf_path
|
| 69 |
-
|
| 70 |
-
# Gradio interface for uploading multiple files
|
| 71 |
iface = gr.Interface(
|
| 72 |
fn=generate_dpr,
|
| 73 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
| 74 |
-
outputs="
|
| 75 |
title="Daily Progress Report Generator",
|
| 76 |
-
description="Upload up to 10 site photos. The AI model will dynamically detect construction activities, materials, and progress and generate a
|
| 77 |
allow_flagging="never" # Optional: Disable flagging
|
| 78 |
)
|
| 79 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from datetime import datetime
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load BLIP model and processor
|
| 8 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
model.to(device)
|
| 13 |
|
| 14 |
+
# Inference function to generate captions dynamically based on image content
|
| 15 |
def generate_captions_from_image(image):
|
| 16 |
if image.mode != "RGB":
|
| 17 |
image = image.convert("RGB")
|
|
|
|
| 23 |
|
| 24 |
return caption
|
| 25 |
|
| 26 |
+
# Function to generate the daily progress report (DPR) text
|
| 27 |
def generate_dpr(files):
|
| 28 |
dpr_text = []
|
| 29 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 30 |
|
| 31 |
+
# Add header to the DPR
|
| 32 |
dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
|
| 33 |
|
| 34 |
# Process each uploaded file (image)
|
|
|
|
| 46 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
| 47 |
dpr_text.append(dpr_section)
|
| 48 |
|
| 49 |
+
# Return the generated DPR as a text output
|
| 50 |
+
return "\n".join(dpr_text)
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Gradio interface for uploading multiple files and displaying the text-based DPR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
iface = gr.Interface(
|
| 54 |
fn=generate_dpr,
|
| 55 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
| 56 |
+
outputs="text", # Display the DPR as text in the output section
|
| 57 |
title="Daily Progress Report Generator",
|
| 58 |
+
description="Upload up to 10 site photos. The AI model will dynamically detect construction activities, materials, and progress and generate a text-based Daily Progress Report (DPR).",
|
| 59 |
allow_flagging="never" # Optional: Disable flagging
|
| 60 |
)
|
| 61 |
|