mgokg commited on
Commit
d0645f3
·
verified ·
1 Parent(s): bc8cf1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  import os
3
  import google.generativeai as genai
4
  import logging
 
 
5
 
6
  # Configure Logging
7
  logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -26,6 +28,9 @@ custom_css = """
26
  }
27
  """
28
 
 
 
 
29
  def predict(prompt):
30
  # Create the model
31
  generation_config = {
@@ -42,18 +47,21 @@ def predict(prompt):
42
  )
43
 
44
  try:
45
- #contents_to_send = [genai.Content(parts=[prompt])]
46
 
47
- response = model.generate_content(contents=prompt, tools='google_search_retrieval')
48
 
49
  if response and response.text:
50
  return response.text
51
  else:
52
  logging.error(f"Unexpected response: {response}")
53
  return "Error: Could not extract text from the response."
 
 
 
54
  except Exception as e:
55
- logging.error(f"An error occurred: {e}")
56
- return f"An error occurred: {e}"
57
 
58
 
59
  # Create the Gradio interface
 
2
  import os
3
  import google.generativeai as genai
4
  import logging
5
+ import time
6
+ import backoff
7
 
8
  # Configure Logging
9
  logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
 
28
  }
29
  """
30
 
31
+ @backoff.on_exception(backoff.expo,
32
+ (genai.APIError),
33
+ max_tries=3) # retry up to 3 times
34
  def predict(prompt):
35
  # Create the model
36
  generation_config = {
 
47
  )
48
 
49
  try:
50
+ contents_to_send = [genai.Content(parts=[prompt])]
51
 
52
+ response = model.generate_content(contents=contents_to_send, tools='google_search_retrieval')
53
 
54
  if response and response.text:
55
  return response.text
56
  else:
57
  logging.error(f"Unexpected response: {response}")
58
  return "Error: Could not extract text from the response."
59
+ except genai.APIError as e:
60
+ logging.error(f"API error occurred: {e}")
61
+ raise
62
  except Exception as e:
63
+ logging.error(f"An error occurred: {e}")
64
+ return f"An error occurred: {e}"
65
 
66
 
67
  # Create the Gradio interface