shukdevdatta123 commited on
Commit
6a66e6f
·
verified ·
1 Parent(s): b93ab3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -49
app.py CHANGED
@@ -5,6 +5,7 @@ import requests
5
  from PIL import Image
6
  import io
7
  import tempfile
 
8
 
9
  def analyze_environmental_impact(api_key, analysis_type, image=None, text_input=None, location=None, product_info=None):
10
  """
@@ -30,7 +31,6 @@ def analyze_environmental_impact(api_key, analysis_type, image=None, text_input=
30
 
31
  # Convert image to base64
32
  with open(image_path, "rb") as img_file:
33
- import base64
34
  image_base64 = base64.b64encode(img_file.read()).decode("utf-8")
35
 
36
  # Clean up temp file
@@ -149,14 +149,22 @@ def analyze_environmental_impact(api_key, analysis_type, image=None, text_input=
149
  try:
150
  completion = client.chat.completions.create(
151
  extra_headers={
152
- "HTTP-Referer": "https://environmental-impact-analyzer.app", # Replace with your actual site
153
  "X-Title": "Smart Environmental Impact Analyzer",
154
  },
155
  model="google/gemini-2.5-pro-exp-03-25:free",
156
  messages=messages
157
  )
158
 
159
- return completion.choices[0].message.content
 
 
 
 
 
 
 
 
160
  except Exception as e:
161
  return f"Error during analysis: {str(e)}"
162
 
@@ -170,52 +178,57 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
170
 
171
  api_key = gr.Textbox(label="OpenRouter API Key", placeholder="Enter your OpenRouter API key", type="password")
172
 
173
- # Create hidden inputs for each tab
174
- with gr.Group(visible=False) as hidden_inputs:
175
- analysis_type_input = gr.Textbox(value="Image Analysis", visible=False)
176
- empty_text_input = gr.Textbox(value="", visible=False)
177
- empty_location_input = gr.Textbox(value="", visible=False)
178
- empty_product_input = gr.Textbox(value="", visible=False)
179
 
180
  with gr.Tabs():
181
  with gr.TabItem("Image Analysis"):
182
- image_input = gr.Image(type="pil", label="Upload an image for environmental analysis")
183
  image_submit = gr.Button("Analyze Image")
184
  image_output = gr.Textbox(label="Analysis Results", lines=15)
185
 
 
 
 
 
 
 
 
 
 
 
186
  image_submit.click(
187
- analyze_environmental_impact,
188
- inputs=[
189
- api_key,
190
- gr.Textbox(value="Image Analysis", visible=False),
191
- image_input,
192
- empty_text_input,
193
- empty_location_input,
194
- empty_product_input
195
- ],
196
  outputs=image_output
197
  )
198
 
199
  with gr.TabItem("Geographical Assessment"):
200
- location_input = gr.Textbox(label="Location (city, region, or country)", placeholder="e.g., Paris, France")
201
  location_submit = gr.Button("Analyze Location")
202
  location_output = gr.Textbox(label="Analysis Results", lines=15)
203
 
 
 
 
 
 
 
 
 
 
 
204
  location_submit.click(
205
- analyze_environmental_impact,
206
- inputs=[
207
- api_key,
208
- gr.Textbox(value="Geographical Assessment", visible=False),
209
- gr.Image(visible=False, type="pil"),
210
- empty_text_input,
211
- location_input,
212
- empty_product_input
213
- ],
214
  outputs=location_output
215
  )
216
 
217
  with gr.TabItem("Product Assessment"):
218
- product_info = gr.Textbox(
219
  label="Product Information",
220
  placeholder="Describe the product, materials, manufacturing process, lifecycle, etc.",
221
  lines=5
@@ -223,21 +236,24 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
223
  product_submit = gr.Button("Analyze Product")
224
  product_output = gr.Textbox(label="Analysis Results", lines=15)
225
 
 
 
 
 
 
 
 
 
 
 
226
  product_submit.click(
227
- analyze_environmental_impact,
228
- inputs=[
229
- api_key,
230
- gr.Textbox(value="Product Assessment", visible=False),
231
- gr.Image(visible=False, type="pil"),
232
- empty_text_input,
233
- empty_location_input,
234
- product_info
235
- ],
236
  outputs=product_output
237
  )
238
 
239
  with gr.TabItem("Custom Query"):
240
- custom_input = gr.Textbox(
241
  label="Custom Environmental Query",
242
  placeholder="Enter your environmental question or describe a scenario to analyze",
243
  lines=5
@@ -245,16 +261,19 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
245
  custom_submit = gr.Button("Analyze")
246
  custom_output = gr.Textbox(label="Analysis Results", lines=15)
247
 
 
 
 
 
 
 
 
 
 
 
248
  custom_submit.click(
249
- analyze_environmental_impact,
250
- inputs=[
251
- api_key,
252
- gr.Textbox(value="Custom Query", visible=False),
253
- gr.Image(visible=False, type="pil"),
254
- custom_input,
255
- empty_location_input,
256
- empty_product_input
257
- ],
258
  outputs=custom_output
259
  )
260
 
 
5
  from PIL import Image
6
  import io
7
  import tempfile
8
+ import base64
9
 
10
  def analyze_environmental_impact(api_key, analysis_type, image=None, text_input=None, location=None, product_info=None):
11
  """
 
31
 
32
  # Convert image to base64
33
  with open(image_path, "rb") as img_file:
 
34
  image_base64 = base64.b64encode(img_file.read()).decode("utf-8")
35
 
36
  # Clean up temp file
 
149
  try:
150
  completion = client.chat.completions.create(
151
  extra_headers={
152
+ "HTTP-Referer": "https://environmental-impact-analyzer.app",
153
  "X-Title": "Smart Environmental Impact Analyzer",
154
  },
155
  model="google/gemini-2.5-pro-exp-03-25:free",
156
  messages=messages
157
  )
158
 
159
+ # Check if completion and choices exist before accessing
160
+ if completion and hasattr(completion, 'choices') and completion.choices and len(completion.choices) > 0:
161
+ if hasattr(completion.choices[0], 'message') and completion.choices[0].message:
162
+ return completion.choices[0].message.content
163
+ else:
164
+ return "Error: Received empty message from API."
165
+ else:
166
+ return "Error: Received incomplete response from API."
167
+
168
  except Exception as e:
169
  return f"Error during analysis: {str(e)}"
170
 
 
178
 
179
  api_key = gr.Textbox(label="OpenRouter API Key", placeholder="Enter your OpenRouter API key", type="password")
180
 
181
+ # Create inputs for each analysis type
182
+ image_input = gr.Image(type="pil", visible=False)
183
+ text_input = gr.Textbox(visible=False)
184
+ location_input = gr.Textbox(visible=False)
185
+ product_input = gr.Textbox(visible=False)
 
186
 
187
  with gr.Tabs():
188
  with gr.TabItem("Image Analysis"):
189
+ image_input_tab = gr.Image(type="pil", label="Upload an image for environmental analysis")
190
  image_submit = gr.Button("Analyze Image")
191
  image_output = gr.Textbox(label="Analysis Results", lines=15)
192
 
193
+ def process_image_tab():
194
+ return analyze_environmental_impact(
195
+ api_key.value,
196
+ "Image Analysis",
197
+ image=image_input_tab.value,
198
+ text_input=None,
199
+ location=None,
200
+ product_info=None
201
+ )
202
+
203
  image_submit.click(
204
+ fn=process_image_tab,
205
+ inputs=None,
 
 
 
 
 
 
 
206
  outputs=image_output
207
  )
208
 
209
  with gr.TabItem("Geographical Assessment"):
210
+ location_input_tab = gr.Textbox(label="Location (city, region, or country)", placeholder="e.g., Paris, France")
211
  location_submit = gr.Button("Analyze Location")
212
  location_output = gr.Textbox(label="Analysis Results", lines=15)
213
 
214
+ def process_location_tab():
215
+ return analyze_environmental_impact(
216
+ api_key.value,
217
+ "Geographical Assessment",
218
+ image=None,
219
+ text_input=None,
220
+ location=location_input_tab.value,
221
+ product_info=None
222
+ )
223
+
224
  location_submit.click(
225
+ fn=process_location_tab,
226
+ inputs=None,
 
 
 
 
 
 
 
227
  outputs=location_output
228
  )
229
 
230
  with gr.TabItem("Product Assessment"):
231
+ product_info_tab = gr.Textbox(
232
  label="Product Information",
233
  placeholder="Describe the product, materials, manufacturing process, lifecycle, etc.",
234
  lines=5
 
236
  product_submit = gr.Button("Analyze Product")
237
  product_output = gr.Textbox(label="Analysis Results", lines=15)
238
 
239
+ def process_product_tab():
240
+ return analyze_environmental_impact(
241
+ api_key.value,
242
+ "Product Assessment",
243
+ image=None,
244
+ text_input=None,
245
+ location=None,
246
+ product_info=product_info_tab.value
247
+ )
248
+
249
  product_submit.click(
250
+ fn=process_product_tab,
251
+ inputs=None,
 
 
 
 
 
 
 
252
  outputs=product_output
253
  )
254
 
255
  with gr.TabItem("Custom Query"):
256
+ custom_input_tab = gr.Textbox(
257
  label="Custom Environmental Query",
258
  placeholder="Enter your environmental question or describe a scenario to analyze",
259
  lines=5
 
261
  custom_submit = gr.Button("Analyze")
262
  custom_output = gr.Textbox(label="Analysis Results", lines=15)
263
 
264
+ def process_custom_tab():
265
+ return analyze_environmental_impact(
266
+ api_key.value,
267
+ "Custom Query",
268
+ image=None,
269
+ text_input=custom_input_tab.value,
270
+ location=None,
271
+ product_info=None
272
+ )
273
+
274
  custom_submit.click(
275
+ fn=process_custom_tab,
276
+ inputs=None,
 
 
 
 
 
 
 
277
  outputs=custom_output
278
  )
279