Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
|
4 |
+
# Function to initialize the client with the provided API key
|
5 |
+
def initialize_client(api_key):
|
6 |
+
client = OpenAI(
|
7 |
+
base_url="https://openrouter.ai/api/v1",
|
8 |
+
api_key=api_key
|
9 |
+
)
|
10 |
+
return client
|
11 |
+
|
12 |
+
# Function to analyze environmental impact using Gemini 2.5 Pro
|
13 |
+
def analyze_environmental_impact(api_key, image_url=None, location=None, product_info=None):
|
14 |
+
client = initialize_client(api_key) # Initialize client with the user's API key
|
15 |
+
|
16 |
+
messages = []
|
17 |
+
|
18 |
+
if image_url:
|
19 |
+
messages.append({
|
20 |
+
"role": "user",
|
21 |
+
"content": [
|
22 |
+
{"type": "text", "text": "Analyze the environmental impact of this image."},
|
23 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
24 |
+
]
|
25 |
+
})
|
26 |
+
|
27 |
+
if location:
|
28 |
+
messages.append({
|
29 |
+
"role": "user",
|
30 |
+
"content": [{"type": "text", "text": f"Analyze the environmental impact of {location}."}]
|
31 |
+
})
|
32 |
+
|
33 |
+
if product_info:
|
34 |
+
messages.append({
|
35 |
+
"role": "user",
|
36 |
+
"content": [{"type": "text", "text": f"Analyze the environmental impact of this product: {product_info}."}]
|
37 |
+
})
|
38 |
+
|
39 |
+
# Call the model for environmental analysis
|
40 |
+
try:
|
41 |
+
completion = client.chat.completions.create(
|
42 |
+
extra_headers={
|
43 |
+
"HTTP-Referer": "<YOUR_SITE_URL>", # Optional
|
44 |
+
"X-Title": "<YOUR_SITE_NAME>", # Optional
|
45 |
+
},
|
46 |
+
model="google/gemini-2.5-pro-exp-03-25:free",
|
47 |
+
messages=messages
|
48 |
+
)
|
49 |
+
|
50 |
+
response = completion.choices[0].message.content
|
51 |
+
return response
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
return f"Error: {str(e)}"
|
55 |
+
|
56 |
+
# Gradio interface
|
57 |
+
def gradio_interface(api_key, image, location, product_info):
|
58 |
+
image_url = image.name if image else None
|
59 |
+
result = analyze_environmental_impact(api_key, image_url=image_url, location=location, product_info=product_info)
|
60 |
+
return result
|
61 |
+
|
62 |
+
# Create Gradio inputs and outputs
|
63 |
+
api_key_input = gr.Textbox(label="Enter Your OpenRouter API Key", type="text")
|
64 |
+
image_input = gr.Image(label="Upload an Image (Optional)", type="file")
|
65 |
+
location_input = gr.Textbox(label="Enter Location for Environmental Impact (Optional)", type="text")
|
66 |
+
product_info_input = gr.Textbox(label="Enter Product Information (Optional)", type="text")
|
67 |
+
|
68 |
+
output = gr.Textbox(label="Environmental Impact Analysis Output", lines=10)
|
69 |
+
|
70 |
+
# Create Gradio interface
|
71 |
+
app = gr.Interface(
|
72 |
+
fn=gradio_interface,
|
73 |
+
inputs=[api_key_input, image_input, location_input, product_info_input],
|
74 |
+
outputs=output,
|
75 |
+
title="Smart Environmental Impact Analyzer",
|
76 |
+
description="Analyze environmental impact based on images, location, or product information. Please provide your OpenRouter API key."
|
77 |
+
)
|
78 |
+
|
79 |
+
# Launch the Gradio app
|
80 |
+
app.launch()
|