Rammohan0504 commited on
Commit
4d9c117
·
verified ·
1 Parent(s): 3f5540c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -1,6 +1,10 @@
1
- from transformers import BlipProcessor, BlipForConditionalGeneration
 
 
 
 
2
  from PIL import Image
3
- import gradio as gr
4
  import torch
5
  from datetime import datetime
6
  from reportlab.lib.pagesizes import letter
@@ -8,12 +12,8 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as PD
8
  from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
9
  from reportlab.lib import colors
10
  from simple_salesforce import Salesforce
11
- import os
12
  from dotenv import load_dotenv
13
- import base64
14
- import io
15
- import shutil
16
- import concurrent.futures
17
 
18
  # Load environment variables from .env file
19
  load_dotenv()
@@ -31,23 +31,22 @@ except Exception as e:
31
  print(f"Failed to connect to Salesforce: {str(e)}")
32
 
33
  # Load BLIP model and processor
 
34
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
35
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
36
- model.eval()
37
- device = "cuda" if torch.cuda.is_available() else "cpu"
38
- model.to(device)
39
 
40
  # Inference function to generate captions dynamically based on image content
41
  def generate_captions_from_image(image):
42
  if image.mode != "RGB":
43
  image = image.convert("RGB")
44
-
45
- # Resize image for faster processing (use smaller resolution to speed up inference)
46
- image = image.resize((320, 320)) # Reduced size for faster processing
47
 
48
  # Preprocess the image and generate a caption
49
  inputs = processor(image, return_tensors="pt").to(device, torch.float16)
50
- output = model.generate(**inputs, max_new_tokens=50)
51
  caption = processor.decode(output[0], skip_special_tokens=True)
52
 
53
  return caption
@@ -258,7 +257,6 @@ iface = gr.Interface(
258
  description="Upload up to 10 site photos. The AI model will generate a text-based Daily Progress Report (DPR), save it as a PDF, and upload the PDF and images to Salesforce under Daily_Progress_Reports__c in the Files related list. Download the PDF locally if needed.",
259
  allow_flagging="never",
260
  css="#gradio-share-link-button-0 { display: none !important; }"
261
-
262
  )
263
 
264
  if __name__ == "__main__":
 
1
+ import os
2
+ import shutil
3
+ import base64
4
+ import time
5
+ import concurrent.futures
6
  from PIL import Image
7
+ from transformers import BlipProcessor, BlipForConditionalGeneration
8
  import torch
9
  from datetime import datetime
10
  from reportlab.lib.pagesizes import letter
 
12
  from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
13
  from reportlab.lib import colors
14
  from simple_salesforce import Salesforce
 
15
  from dotenv import load_dotenv
16
+ import gradio as gr
 
 
 
17
 
18
  # Load environment variables from .env file
19
  load_dotenv()
 
31
  print(f"Failed to connect to Salesforce: {str(e)}")
32
 
33
  # Load BLIP model and processor
34
+ device = "cuda" if torch.cuda.is_available() else "cpu"
35
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
36
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
37
+ model.eval().to(device)
 
 
38
 
39
  # Inference function to generate captions dynamically based on image content
40
  def generate_captions_from_image(image):
41
  if image.mode != "RGB":
42
  image = image.convert("RGB")
43
+
44
+ # Resize for faster processing
45
+ image = image.resize((224, 224)) # Adjust to smaller resolution for faster inference
46
 
47
  # Preprocess the image and generate a caption
48
  inputs = processor(image, return_tensors="pt").to(device, torch.float16)
49
+ output = model.generate(**inputs, max_length=50)
50
  caption = processor.decode(output[0], skip_special_tokens=True)
51
 
52
  return caption
 
257
  description="Upload up to 10 site photos. The AI model will generate a text-based Daily Progress Report (DPR), save it as a PDF, and upload the PDF and images to Salesforce under Daily_Progress_Reports__c in the Files related list. Download the PDF locally if needed.",
258
  allow_flagging="never",
259
  css="#gradio-share-link-button-0 { display: none !important; }"
 
260
  )
261
 
262
  if __name__ == "__main__":