Rammohan0504 commited on
Commit
43fb6e6
·
verified ·
1 Parent(s): 26d8d05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -12,6 +12,7 @@ import os
12
  from dotenv import load_dotenv
13
  import base64
14
  import io
 
15
 
16
  # Load environment variables from .env file
17
  load_dotenv()
@@ -40,6 +41,9 @@ def generate_captions_from_image(image):
40
  if image.mode != "RGB":
41
  image = image.convert("RGB")
42
 
 
 
 
43
  # Preprocess the image and generate a caption
44
  inputs = processor(image, return_tensors="pt").to(device, torch.float16)
45
  output = model.generate(**inputs, max_new_tokens=50)
@@ -136,16 +140,12 @@ def generate_dpr(files):
136
  # Add header to the DPR
137
  dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
138
 
139
- # Process each uploaded file (image)
140
- for file in files:
141
- # Open the image from file path
142
- image = Image.open(file.name)
143
-
144
- if image.mode != "RGB":
145
- image = image.convert("RGB")
146
-
147
- # Dynamically generate a caption based on the image
148
- caption = generate_captions_from_image(image)
149
  captions.append(caption)
150
 
151
  # Generate DPR section for this image with dynamic caption
 
12
  from dotenv import load_dotenv
13
  import base64
14
  import io
15
+ import concurrent.futures
16
 
17
  # Load environment variables from .env file
18
  load_dotenv()
 
41
  if image.mode != "RGB":
42
  image = image.convert("RGB")
43
 
44
+ # Resize image for faster processing
45
+ image = image.resize((640, 640))
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_new_tokens=50)
 
140
  # Add header to the DPR
141
  dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
142
 
143
+ # Process images in parallel for faster performance
144
+ with concurrent.futures.ThreadPoolExecutor() as executor:
145
+ results = list(executor.map(lambda file: generate_captions_from_image(Image.open(file.name)), files))
146
+
147
+ for i, file in enumerate(files):
148
+ caption = results[i]
 
 
 
 
149
  captions.append(caption)
150
 
151
  # Generate DPR section for this image with dynamic caption