dev2607 commited on
Commit
b5e544b
·
verified ·
1 Parent(s): 6331cc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -14
app.py CHANGED
@@ -80,6 +80,7 @@ def parse_ingredients(text):
80
  ingredients = [i.strip().lower() for i in ingredients if i.strip()]
81
  return ingredients
82
 
 
83
  # Function to analyze ingredients with Gemini
84
  def analyze_ingredients_with_gemini(ingredients_list, health_conditions=None):
85
  """
@@ -90,7 +91,11 @@ def analyze_ingredients_with_gemini(ingredients_list, health_conditions=None):
90
 
91
  # Prepare the list of ingredients for the prompt
92
  ingredients_text = ", ".join(ingredients_list)
93
-
 
 
 
 
94
  # Create a prompt for Gemini
95
  if health_conditions and health_conditions.strip():
96
  prompt = f"""
@@ -115,19 +120,49 @@ def analyze_ingredients_with_gemini(ingredients_list, health_conditions=None):
115
  """
116
 
117
  try:
118
- # Check if Gemini API is available
119
- if not genai or not os.getenv("GEMINI_API_KEY"):
120
- return dummy_analyze(ingredients_list, health_conditions)
121
-
122
  # Call the Gemini API
123
- model = genai.GenerativeModel('gemini-pro')
124
- response = model.generate_content(prompt)
125
-
126
- # Check if response is valid
127
- if hasattr(response, 'text') and response.text:
128
- analysis = response.text
129
- else:
130
- return "Error: Received empty response from analysis service. Please try again."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
  # Add disclaimer
133
  disclaimer = """
@@ -140,7 +175,7 @@ def analyze_ingredients_with_gemini(ingredients_list, health_conditions=None):
140
 
141
  except Exception as e:
142
  # Fallback to basic analysis if API call fails
143
- return f"Error connecting to analysis service: {str(e)}\n\nPlease try again later."
144
 
145
  # Dummy analysis function for when API is not available
146
  def dummy_analyze(ingredients_list, health_conditions=None):
 
80
  ingredients = [i.strip().lower() for i in ingredients if i.strip()]
81
  return ingredients
82
 
83
+ # Function to analyze ingredients with Gemini
84
  # Function to analyze ingredients with Gemini
85
  def analyze_ingredients_with_gemini(ingredients_list, health_conditions=None):
86
  """
 
91
 
92
  # Prepare the list of ingredients for the prompt
93
  ingredients_text = ", ".join(ingredients_list)
94
+
95
+ # Check if Gemini API is available
96
+ if not genai or not os.getenv("GEMINI_API_KEY"):
97
+ return dummy_analyze(ingredients_list, health_conditions)
98
+
99
  # Create a prompt for Gemini
100
  if health_conditions and health_conditions.strip():
101
  prompt = f"""
 
120
  """
121
 
122
  try:
 
 
 
 
123
  # Call the Gemini API
124
+ try:
125
+ model = genai.GenerativeModel('gemini-pro')
126
+ response = model.generate_content(prompt)
127
+
128
+ # Check if response is valid
129
+ if hasattr(response, 'text') and response.text:
130
+ analysis = response.text
131
+ else:
132
+ # Fall back to alternative model if available
133
+ try:
134
+ models = genai.list_models()
135
+ available_models = [m.name for m in models]
136
+ if 'gemini-1.0-pro' in available_models:
137
+ model = genai.GenerativeModel('gemini-1.0-pro')
138
+ elif 'gemini-1.5-pro' in available_models:
139
+ model = genai.GenerativeModel('gemini-1.5-pro')
140
+ else:
141
+ # If no alternative model is available, use dummy analysis
142
+ return dummy_analyze(ingredients_list, health_conditions) + "\n\n(Using fallback analysis due to API model availability issues)"
143
+
144
+ response = model.generate_content(prompt)
145
+ analysis = response.text if hasattr(response, 'text') else "Error: Received empty response"
146
+ except Exception as model_e:
147
+ return dummy_analyze(ingredients_list, health_conditions) + f"\n\n(Using fallback analysis: {str(model_e)})"
148
+ except Exception as e:
149
+ if "404 models/gemini-pro is not found" in str(e):
150
+ # Try listing available models and use an alternative if possible
151
+ try:
152
+ models = genai.list_models()
153
+ available_models = [m.name for m in models]
154
+ if not available_models:
155
+ return dummy_analyze(ingredients_list, health_conditions) + "\n\n(Using fallback analysis due to API model availability issues)"
156
+
157
+ # Use first available model
158
+ model = genai.GenerativeModel(available_models[0])
159
+ response = model.generate_content(prompt)
160
+ analysis = response.text if hasattr(response, 'text') else "Error: Received empty response"
161
+ except Exception as model_e:
162
+ return dummy_analyze(ingredients_list, health_conditions) + f"\n\n(Using fallback analysis: {str(model_e)})"
163
+ else:
164
+ # Handle other exceptions
165
+ return dummy_analyze(ingredients_list, health_conditions) + f"\n\n(Using fallback analysis: {str(e)})"
166
 
167
  # Add disclaimer
168
  disclaimer = """
 
175
 
176
  except Exception as e:
177
  # Fallback to basic analysis if API call fails
178
+ return dummy_analyze(ingredients_list, health_conditions) + f"\n\n(Using fallback analysis: {str(e)})"
179
 
180
  # Dummy analysis function for when API is not available
181
  def dummy_analyze(ingredients_list, health_conditions=None):