Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,6 @@ from PIL import Image
|
|
6 |
import io
|
7 |
import tempfile
|
8 |
import base64
|
9 |
-
import time
|
10 |
|
11 |
def analyze_environmental_impact(api_key, analysis_type, image=None, text_input=None, location=None, product_info=None):
|
12 |
"""
|
@@ -147,48 +146,27 @@ def analyze_environmental_impact(api_key, analysis_type, image=None, text_input=
|
|
147 |
]
|
148 |
|
149 |
# Make API call
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
timeout=60 # Set timeout to 60 seconds
|
165 |
-
)
|
166 |
-
|
167 |
-
# Check if completion and choices exist before accessing
|
168 |
-
if completion and hasattr(completion, 'choices') and completion.choices and len(completion.choices) > 0:
|
169 |
-
if hasattr(completion.choices[0], 'message') and completion.choices[0].message:
|
170 |
-
return completion.choices[0].message.content
|
171 |
-
else:
|
172 |
-
if attempt < max_retries - 1:
|
173 |
-
time.sleep(retry_delay)
|
174 |
-
continue
|
175 |
-
return "Error: Received empty message from API. Please try again."
|
176 |
else:
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
if attempt < max_retries - 1:
|
184 |
-
time.sleep(retry_delay)
|
185 |
-
continue
|
186 |
-
return f"Error during analysis: {str(e)}. Please check your API key and try again."
|
187 |
-
|
188 |
-
return "Failed to get a response after multiple attempts. Please try again later."
|
189 |
-
|
190 |
-
def clear_output():
|
191 |
-
return ""
|
192 |
|
193 |
# Create Gradio interface
|
194 |
with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
@@ -201,20 +179,15 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
|
201 |
api_key = gr.Textbox(label="OpenRouter API Key", placeholder="Enter your OpenRouter API key", type="password")
|
202 |
|
203 |
with gr.Tabs():
|
204 |
-
# Image Analysis Tab
|
205 |
with gr.TabItem("Image Analysis"):
|
206 |
image_input = gr.Image(type="pil", label="Upload an image for environmental analysis")
|
207 |
-
|
208 |
-
with gr.Row():
|
209 |
-
image_submit = gr.Button("Analyze Image", variant="primary")
|
210 |
-
image_clear = gr.Button("Clear Response", variant="secondary")
|
211 |
-
|
212 |
image_output = gr.Textbox(label="Analysis Results", lines=15)
|
213 |
|
214 |
image_submit.click(
|
215 |
analyze_environmental_impact,
|
216 |
inputs=[
|
217 |
-
api_key,
|
218 |
gr.Textbox(value="Image Analysis", visible=False),
|
219 |
image_input,
|
220 |
gr.Textbox(value="", visible=False),
|
@@ -223,27 +196,16 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
|
223 |
],
|
224 |
outputs=image_output
|
225 |
)
|
226 |
-
|
227 |
-
image_clear.click(
|
228 |
-
clear_output,
|
229 |
-
inputs=None,
|
230 |
-
outputs=image_output
|
231 |
-
)
|
232 |
|
233 |
-
# Geographical Assessment Tab
|
234 |
with gr.TabItem("Geographical Assessment"):
|
235 |
location_input = gr.Textbox(label="Location (city, region, or country)", placeholder="e.g., Paris, France")
|
236 |
-
|
237 |
-
with gr.Row():
|
238 |
-
location_submit = gr.Button("Analyze Location", variant="primary")
|
239 |
-
location_clear = gr.Button("Clear Response", variant="secondary")
|
240 |
-
|
241 |
location_output = gr.Textbox(label="Analysis Results", lines=15)
|
242 |
|
243 |
location_submit.click(
|
244 |
analyze_environmental_impact,
|
245 |
inputs=[
|
246 |
-
api_key,
|
247 |
gr.Textbox(value="Geographical Assessment", visible=False),
|
248 |
gr.Image(value=None, visible=False, type="pil"),
|
249 |
gr.Textbox(value="", visible=False),
|
@@ -253,30 +215,19 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
|
253 |
outputs=location_output
|
254 |
)
|
255 |
|
256 |
-
location_clear.click(
|
257 |
-
clear_output,
|
258 |
-
inputs=None,
|
259 |
-
outputs=location_output
|
260 |
-
)
|
261 |
-
|
262 |
-
# Product Assessment Tab
|
263 |
with gr.TabItem("Product Assessment"):
|
264 |
product_info = gr.Textbox(
|
265 |
label="Product Information",
|
266 |
placeholder="Describe the product, materials, manufacturing process, lifecycle, etc.",
|
267 |
lines=5
|
268 |
)
|
269 |
-
|
270 |
-
with gr.Row():
|
271 |
-
product_submit = gr.Button("Analyze Product", variant="primary")
|
272 |
-
product_clear = gr.Button("Clear Response", variant="secondary")
|
273 |
-
|
274 |
product_output = gr.Textbox(label="Analysis Results", lines=15)
|
275 |
|
276 |
product_submit.click(
|
277 |
analyze_environmental_impact,
|
278 |
inputs=[
|
279 |
-
api_key,
|
280 |
gr.Textbox(value="Product Assessment", visible=False),
|
281 |
gr.Image(value=None, visible=False, type="pil"),
|
282 |
gr.Textbox(value="", visible=False),
|
@@ -286,30 +237,19 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
|
286 |
outputs=product_output
|
287 |
)
|
288 |
|
289 |
-
product_clear.click(
|
290 |
-
clear_output,
|
291 |
-
inputs=None,
|
292 |
-
outputs=product_output
|
293 |
-
)
|
294 |
-
|
295 |
-
# Custom Query Tab
|
296 |
with gr.TabItem("Custom Query"):
|
297 |
custom_input = gr.Textbox(
|
298 |
label="Custom Environmental Query",
|
299 |
placeholder="Enter your environmental question or describe a scenario to analyze",
|
300 |
lines=5
|
301 |
)
|
302 |
-
|
303 |
-
with gr.Row():
|
304 |
-
custom_submit = gr.Button("Analyze", variant="primary")
|
305 |
-
custom_clear = gr.Button("Clear Response", variant="secondary")
|
306 |
-
|
307 |
custom_output = gr.Textbox(label="Analysis Results", lines=15)
|
308 |
|
309 |
custom_submit.click(
|
310 |
analyze_environmental_impact,
|
311 |
inputs=[
|
312 |
-
api_key,
|
313 |
gr.Textbox(value="Custom Query", visible=False),
|
314 |
gr.Image(value=None, visible=False, type="pil"),
|
315 |
custom_input,
|
@@ -318,12 +258,6 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
|
318 |
],
|
319 |
outputs=custom_output
|
320 |
)
|
321 |
-
|
322 |
-
custom_clear.click(
|
323 |
-
clear_output,
|
324 |
-
inputs=None,
|
325 |
-
outputs=custom_output
|
326 |
-
)
|
327 |
|
328 |
gr.Markdown("""
|
329 |
### Privacy Notice
|
@@ -335,7 +269,6 @@ with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
|
335 |
2. Select the type of analysis you want to perform
|
336 |
3. Provide the required information (image, location, product details, or custom query)
|
337 |
4. Click the "Analyze" button for your selected tab
|
338 |
-
5. Use the "Clear Response" button to reset results
|
339 |
""")
|
340 |
|
341 |
# Launch the app
|
|
|
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 |
"""
|
|
|
146 |
]
|
147 |
|
148 |
# Make API call
|
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 |
|
171 |
# Create Gradio interface
|
172 |
with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
|
|
179 |
api_key = gr.Textbox(label="OpenRouter API Key", placeholder="Enter your OpenRouter API key", type="password")
|
180 |
|
181 |
with gr.Tabs():
|
|
|
182 |
with gr.TabItem("Image Analysis"):
|
183 |
image_input = gr.Image(type="pil", label="Upload an image for environmental analysis")
|
184 |
+
image_submit = gr.Button("Analyze Image")
|
|
|
|
|
|
|
|
|
185 |
image_output = gr.Textbox(label="Analysis Results", lines=15)
|
186 |
|
187 |
image_submit.click(
|
188 |
analyze_environmental_impact,
|
189 |
inputs=[
|
190 |
+
api_key, # Directly pass the api_key component
|
191 |
gr.Textbox(value="Image Analysis", visible=False),
|
192 |
image_input,
|
193 |
gr.Textbox(value="", visible=False),
|
|
|
196 |
],
|
197 |
outputs=image_output
|
198 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
|
|
200 |
with gr.TabItem("Geographical Assessment"):
|
201 |
location_input = gr.Textbox(label="Location (city, region, or country)", placeholder="e.g., Paris, France")
|
202 |
+
location_submit = gr.Button("Analyze Location")
|
|
|
|
|
|
|
|
|
203 |
location_output = gr.Textbox(label="Analysis Results", lines=15)
|
204 |
|
205 |
location_submit.click(
|
206 |
analyze_environmental_impact,
|
207 |
inputs=[
|
208 |
+
api_key, # Directly pass the api_key component
|
209 |
gr.Textbox(value="Geographical Assessment", visible=False),
|
210 |
gr.Image(value=None, visible=False, type="pil"),
|
211 |
gr.Textbox(value="", visible=False),
|
|
|
215 |
outputs=location_output
|
216 |
)
|
217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
with gr.TabItem("Product Assessment"):
|
219 |
product_info = gr.Textbox(
|
220 |
label="Product Information",
|
221 |
placeholder="Describe the product, materials, manufacturing process, lifecycle, etc.",
|
222 |
lines=5
|
223 |
)
|
224 |
+
product_submit = gr.Button("Analyze Product")
|
|
|
|
|
|
|
|
|
225 |
product_output = gr.Textbox(label="Analysis Results", lines=15)
|
226 |
|
227 |
product_submit.click(
|
228 |
analyze_environmental_impact,
|
229 |
inputs=[
|
230 |
+
api_key, # Directly pass the api_key component
|
231 |
gr.Textbox(value="Product Assessment", visible=False),
|
232 |
gr.Image(value=None, visible=False, type="pil"),
|
233 |
gr.Textbox(value="", visible=False),
|
|
|
237 |
outputs=product_output
|
238 |
)
|
239 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
with gr.TabItem("Custom Query"):
|
241 |
custom_input = gr.Textbox(
|
242 |
label="Custom Environmental Query",
|
243 |
placeholder="Enter your environmental question or describe a scenario to analyze",
|
244 |
lines=5
|
245 |
)
|
246 |
+
custom_submit = gr.Button("Analyze")
|
|
|
|
|
|
|
|
|
247 |
custom_output = gr.Textbox(label="Analysis Results", lines=15)
|
248 |
|
249 |
custom_submit.click(
|
250 |
analyze_environmental_impact,
|
251 |
inputs=[
|
252 |
+
api_key, # Directly pass the api_key component
|
253 |
gr.Textbox(value="Custom Query", visible=False),
|
254 |
gr.Image(value=None, visible=False, type="pil"),
|
255 |
custom_input,
|
|
|
258 |
],
|
259 |
outputs=custom_output
|
260 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
|
262 |
gr.Markdown("""
|
263 |
### Privacy Notice
|
|
|
269 |
2. Select the type of analysis you want to perform
|
270 |
3. Provide the required information (image, location, product details, or custom query)
|
271 |
4. Click the "Analyze" button for your selected tab
|
|
|
272 |
""")
|
273 |
|
274 |
# Launch the app
|