Rammohan0504 commited on
Commit
7d8ec5e
·
verified ·
1 Parent(s): 3d3c695

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -36
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 utils import create_pdf
 
 
 
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 generate_caption(image):
15
- start_time = time.time()
16
-
17
- if image.mode != "RGB":
18
- image = image.convert("RGB")
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
- duration = time.time() - start_time
25
- if duration > 5:
26
- caption = f"⚠️ Took {round(duration, 2)}s: {caption}"
 
 
27
 
28
- return caption
29
-
30
- def process_images(images):
31
- results = []
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
- iface = gr.Interface(
39
- fn=process_images,
40
- inputs=gr.File(label="Upload up to 10 Site Images", type="file", file_types=[".jpg", ".png"], multiple=True),
41
- outputs=["text", "file"],
42
- title="Auto-DPR Generator from Site Images",
43
- description="Upload construction site images. AI will auto-generate a progress summary and downloadable PDF.",
44
- allow_flagging="never"
45
  )
46
 
47
  if __name__ == "__main__":
48
- iface.launch()
 
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()