alexander-lazarin commited on
Commit
f95c642
·
1 Parent(s): 70b8860

Add a status bar to the Gradio UI

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -172,15 +172,17 @@ def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str
172
  except Exception as e:
173
  return f"Error with {model_name}: {str(e)}"
174
 
175
- def process_input(id_product_money: str, prompt: str) -> Tuple[List[str], str, str]:
176
  """Main processing function."""
177
  try:
178
- print("Getting marketplace and main image...")
 
179
  marketplace, main_image = get_marketplace_and_main_image(id_product_money)
180
  print(f"Marketplace: {marketplace}")
181
  print(f"Main image: {main_image}")
182
 
183
- print("\nGetting additional images...")
 
184
  additional_images = get_additional_images(id_product_money, marketplace)
185
  print(f"Additional images: {additional_images}")
186
 
@@ -193,23 +195,31 @@ def process_input(id_product_money: str, prompt: str) -> Tuple[List[str], str, s
193
  all_image_urls.append(url)
194
  print(f"\nAll image URLs: {all_image_urls}")
195
 
196
- print("\nDownloading and encoding images...")
 
197
  encoded_images = download_and_encode_images(all_image_urls, marketplace)
198
  print(f"Number of encoded images: {len(encoded_images)}")
199
 
200
  if not encoded_images:
201
  raise ValueError("No images could be downloaded")
202
 
203
- print("\nGetting Gemini responses...")
204
- # Get responses from both models
205
  gemini_1_5_response = get_gemini_response("gemini-1.5-flash", encoded_images, prompt)
 
 
 
206
  gemini_2_0_response = get_gemini_response("gemini-2.0-flash-exp", encoded_images, prompt)
207
 
208
- return all_image_urls, gemini_1_5_response, gemini_2_0_response
 
 
209
 
210
  except Exception as e:
211
  print(f"\nError in process_input: {str(e)}")
212
- return [], f"Error: {str(e)}", f"Error: {str(e)}"
 
 
213
 
214
  # Create Gradio interface
215
  with gr.Blocks() as demo:
@@ -221,6 +231,9 @@ with gr.Blocks() as demo:
221
 
222
  submit_btn = gr.Button("Analyze")
223
 
 
 
 
224
  with gr.Row():
225
  image_gallery = gr.Gallery(label="Product Images", show_label=True)
226
 
@@ -235,7 +248,8 @@ with gr.Blocks() as demo:
235
  submit_btn.click(
236
  fn=process_input,
237
  inputs=[id_input, prompt_input],
238
- outputs=[image_gallery, gemini_1_5_output, gemini_2_0_output]
 
239
  )
240
 
241
  if __name__ == "__main__":
 
172
  except Exception as e:
173
  return f"Error with {model_name}: {str(e)}"
174
 
175
+ def process_input(id_product_money: str, prompt: str, progress=gr.Progress()) -> Tuple[List[str], str, str, str]:
176
  """Main processing function."""
177
  try:
178
+ status_msg = "Getting product data from database..."
179
+ progress(0, desc=status_msg)
180
  marketplace, main_image = get_marketplace_and_main_image(id_product_money)
181
  print(f"Marketplace: {marketplace}")
182
  print(f"Main image: {main_image}")
183
 
184
+ status_msg = "Fetching additional product images..."
185
+ progress(0.2, desc=status_msg)
186
  additional_images = get_additional_images(id_product_money, marketplace)
187
  print(f"Additional images: {additional_images}")
188
 
 
195
  all_image_urls.append(url)
196
  print(f"\nAll image URLs: {all_image_urls}")
197
 
198
+ status_msg = "Downloading and processing images..."
199
+ progress(0.4, desc=status_msg)
200
  encoded_images = download_and_encode_images(all_image_urls, marketplace)
201
  print(f"Number of encoded images: {len(encoded_images)}")
202
 
203
  if not encoded_images:
204
  raise ValueError("No images could be downloaded")
205
 
206
+ status_msg = "Getting response from Gemini 1.5 Flash..."
207
+ progress(0.6, desc=status_msg)
208
  gemini_1_5_response = get_gemini_response("gemini-1.5-flash", encoded_images, prompt)
209
+
210
+ status_msg = "Getting response from Gemini 2.0 Flash Exp..."
211
+ progress(0.8, desc=status_msg)
212
  gemini_2_0_response = get_gemini_response("gemini-2.0-flash-exp", encoded_images, prompt)
213
 
214
+ status_msg = "Analysis complete!"
215
+ progress(1.0, desc=status_msg)
216
+ return all_image_urls, gemini_1_5_response, gemini_2_0_response, status_msg
217
 
218
  except Exception as e:
219
  print(f"\nError in process_input: {str(e)}")
220
+ status_msg = f"Error: {str(e)}"
221
+ progress(1.0, desc=status_msg)
222
+ return [], f"Error: {str(e)}", f"Error: {str(e)}", status_msg
223
 
224
  # Create Gradio interface
225
  with gr.Blocks() as demo:
 
231
 
232
  submit_btn = gr.Button("Analyze")
233
 
234
+ # Status indicator
235
+ status = gr.Textbox(label="Status", value="Waiting for input...", interactive=False)
236
+
237
  with gr.Row():
238
  image_gallery = gr.Gallery(label="Product Images", show_label=True)
239
 
 
248
  submit_btn.click(
249
  fn=process_input,
250
  inputs=[id_input, prompt_input],
251
+ outputs=[image_gallery, gemini_1_5_output, gemini_2_0_output, status],
252
+ show_progress="full"
253
  )
254
 
255
  if __name__ == "__main__":