Nerva1228 commited on
Commit
8519f94
·
verified ·
1 Parent(s): 137171c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -43
app.py CHANGED
@@ -170,49 +170,43 @@ def infer(request: gr.Request):
170
  return "No tasks found or failed to fetch tasks."
171
 
172
  for task in img2text_tasks:
173
- if task['status'] == 'Pending':
174
- try:
175
- image_url = task['content']['url']
176
- prompt = task['content']['prompt']
177
-
178
- # Fetch the image from the URL
179
- image_response = requests.get(image_url)
180
- image = Image.open(BytesIO(image_response.content))
181
-
182
- # Resize image
183
- max_size = 256
184
- width, height = image.size
185
- if width > height:
186
- new_width = max_size
187
- new_height = int((new_width / width) * height)
188
- else:
189
- new_height = max_size
190
- new_width = int((new_height / height) * width)
191
- image = image.resize((new_width, new_height), Image.LANCZOS)
192
-
193
- # Process the image and prompt
194
- inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
195
- generated_ids = model.generate(
196
- input_ids=inputs["input_ids"],
197
- pixel_values=inputs["pixel_values"],
198
- max_new_tokens=1024,
199
- do_sample=False,
200
- num_beams=3
201
- )
202
-
203
- generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
204
- parsed_answer = processor.post_process_generation(generated_text, task=prompt, image_size=(image.width, image.height))
205
- print(task['id'])
206
- print(parsed_answer)
207
-
208
- # Update the task status to Successed with result
209
- update_task_status('img2text', task['id'], 'Successed', {"text": parsed_answer})
210
-
211
- except Exception as e:
212
- print(f"Error processing task {task['id']}: {e}")
213
- # If error occurs, update the task status to Failed
214
- update_task_status('img2text', task['id'], 'Failed')
215
- return f"Error processing task {task['id']}: {e}"
216
 
217
  return "No pending tasks found."
218
 
 
170
  return "No tasks found or failed to fetch tasks."
171
 
172
  for task in img2text_tasks:
173
+ try:
174
+ image_url = task['content']['url']
175
+ prompt = task['content']['prompt']
176
+ print(image_url)
177
+ print(prompt)
178
+ # Fetch the image from the URL
179
+ image_response = requests.get(image_url)
180
+ image = Image.open(BytesIO(image_response.content))
181
+
182
+ # Resize image
183
+ max_size = 256
184
+ width, height = image.size
185
+ if width > height:
186
+ new_width = max_size
187
+ new_height = int((new_width / width) * height)
188
+ else:
189
+ new_height = max_size
190
+ new_width = int((new_height / height) * width)
191
+ image = image.resize((new_width, new_height), Image.LANCZOS)
192
+
193
+ # Process the image and prompt
194
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
195
+ generated_ids = model.generate(
196
+ input_ids=inputs["input_ids"],
197
+ pixel_values=inputs["pixel_values"],
198
+ max_new_tokens=1024,
199
+ do_sample=False,
200
+ num_beams=3
201
+ )
202
+
203
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
204
+ parsed_answer = processor.post_process_generation(generated_text, task=prompt, image_size=(image.width, image.height))
205
+ print(task['id'])
206
+ print(parsed_answer)
207
+
208
+ # Update the task status to Successed with result
209
+ update_task_status('img2text', task['id'], 'Successed', {"text": parsed_answer})
 
 
 
 
 
 
210
 
211
  return "No pending tasks found."
212