Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,240 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
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=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 the image is provided, include it in the analysis
|
19 |
-
if image:
|
20 |
-
messages.append({
|
21 |
-
"role": "user",
|
22 |
-
"content": [
|
23 |
-
{"type": "text", "text": "Analyze the environmental impact of this image."},
|
24 |
-
{"type": "image_url", "image_url": {"url": image}}
|
25 |
-
]
|
26 |
-
})
|
27 |
|
28 |
-
#
|
29 |
-
if
|
30 |
-
|
31 |
-
"
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
#
|
36 |
-
if product_info:
|
37 |
-
messages.append({
|
38 |
-
"role": "user",
|
39 |
-
"content": [{"type": "text", "text": f"Analyze the environmental impact of this product: {product_info}."}]
|
40 |
-
})
|
41 |
-
|
42 |
-
# Call the model for environmental analysis
|
43 |
try:
|
44 |
completion = client.chat.completions.create(
|
45 |
extra_headers={
|
46 |
-
"HTTP-Referer": "
|
47 |
-
"X-Title": "
|
48 |
},
|
49 |
model="google/gemini-2.5-pro-exp-03-25:free",
|
50 |
messages=messages
|
51 |
)
|
52 |
-
|
53 |
-
|
54 |
-
return response
|
55 |
-
|
56 |
except Exception as e:
|
57 |
-
return f"Error: {str(e)}"
|
58 |
-
|
59 |
-
# Gradio interface
|
60 |
-
def gradio_interface(api_key, image, location, product_info):
|
61 |
-
image_url = image if image else None # Gradio returns file path as string for "filepath" type
|
62 |
-
result = analyze_environmental_impact(api_key, image=image_url, location=location, product_info=product_info)
|
63 |
-
return result
|
64 |
-
|
65 |
-
# Create Gradio inputs and outputs
|
66 |
-
api_key_input = gr.Textbox(label="Enter Your OpenRouter API Key", type="password")
|
67 |
-
image_input = gr.Image(label="Upload an Image (Optional)", type="filepath") # Fix: Changed to 'filepath'
|
68 |
-
location_input = gr.Textbox(label="Enter Location for Environmental Impact (Optional)", type="text")
|
69 |
-
product_info_input = gr.Textbox(label="Enter Product Information (Optional)", type="text")
|
70 |
-
|
71 |
-
output = gr.Textbox(label="Environmental Impact Analysis Output", lines=10)
|
72 |
|
73 |
# Create Gradio interface
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
# Launch the
|
83 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
from openai import OpenAI
|
4 |
+
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 |
+
"""
|
11 |
+
Analyze environmental impact based on user inputs using Gemini 2.5 Pro through OpenRouter.
|
12 |
+
"""
|
13 |
+
if not api_key:
|
14 |
+
return "Please provide an OpenRouter API key."
|
15 |
+
|
16 |
client = OpenAI(
|
17 |
base_url="https://openrouter.ai/api/v1",
|
18 |
+
api_key=api_key,
|
19 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Prepare messages based on analysis type
|
22 |
+
if analysis_type == "Image Analysis":
|
23 |
+
if image is None:
|
24 |
+
return "Please upload an image for analysis."
|
25 |
+
|
26 |
+
# Save image to a temporary file
|
27 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
28 |
+
image_path = temp_file.name
|
29 |
+
image.save(image_path)
|
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
|
37 |
+
os.unlink(image_path)
|
38 |
+
|
39 |
+
prompt = """
|
40 |
+
Analyze this image for environmental impact factors. Consider:
|
41 |
+
1. Visible ecosystems, wildlife, or natural resources
|
42 |
+
2. Human infrastructure and its potential environmental footprint
|
43 |
+
3. Evidence of pollution, waste, or environmental degradation
|
44 |
+
4. Sustainable or eco-friendly elements
|
45 |
+
|
46 |
+
Provide a comprehensive environmental impact assessment and suggest ways to improve
|
47 |
+
sustainability based on what you see.
|
48 |
+
"""
|
49 |
+
|
50 |
+
messages = [
|
51 |
+
{
|
52 |
+
"role": "user",
|
53 |
+
"content": [
|
54 |
+
{
|
55 |
+
"type": "text",
|
56 |
+
"text": prompt
|
57 |
+
},
|
58 |
+
{
|
59 |
+
"type": "image_url",
|
60 |
+
"image_url": {
|
61 |
+
"url": f"data:image/jpeg;base64,{image_base64}"
|
62 |
+
}
|
63 |
+
}
|
64 |
+
]
|
65 |
+
}
|
66 |
+
]
|
67 |
+
|
68 |
+
elif analysis_type == "Geographical Assessment":
|
69 |
+
if not location:
|
70 |
+
return "Please provide a location for geographical assessment."
|
71 |
+
|
72 |
+
prompt = f"""
|
73 |
+
Provide an environmental impact assessment for the location: {location}.
|
74 |
+
|
75 |
+
Include information about:
|
76 |
+
1. Current environmental conditions (air quality, water resources, biodiversity)
|
77 |
+
2. Major environmental challenges and threats
|
78 |
+
3. Sustainability initiatives and progress
|
79 |
+
4. Carbon footprint and emissions data
|
80 |
+
5. Recommendations for improving environmental sustainability in this area
|
81 |
+
|
82 |
+
Present the information in a structured format with clear sections for each aspect.
|
83 |
+
"""
|
84 |
+
|
85 |
+
messages = [
|
86 |
+
{
|
87 |
+
"role": "user",
|
88 |
+
"content": prompt
|
89 |
+
}
|
90 |
+
]
|
91 |
+
|
92 |
+
elif analysis_type == "Product Assessment":
|
93 |
+
if not product_info:
|
94 |
+
return "Please provide product information for assessment."
|
95 |
+
|
96 |
+
prompt = f"""
|
97 |
+
Analyze the environmental impact of the following product:
|
98 |
+
|
99 |
+
{product_info}
|
100 |
+
|
101 |
+
Include in your assessment:
|
102 |
+
1. Materials and resources used
|
103 |
+
2. Manufacturing process impact
|
104 |
+
3. Transportation and distribution footprint
|
105 |
+
4. Usage phase environmental impact
|
106 |
+
5. End-of-life considerations
|
107 |
+
6. Overall sustainability score on a scale of 1-10
|
108 |
+
7. Recommendations for improving the product's environmental footprint
|
109 |
+
|
110 |
+
Be specific and provide actionable insights.
|
111 |
+
"""
|
112 |
+
|
113 |
+
messages = [
|
114 |
+
{
|
115 |
+
"role": "user",
|
116 |
+
"content": prompt
|
117 |
+
}
|
118 |
+
]
|
119 |
+
|
120 |
+
elif analysis_type == "Custom Query":
|
121 |
+
if not text_input:
|
122 |
+
return "Please provide a query for custom environmental analysis."
|
123 |
+
|
124 |
+
prompt = f"""
|
125 |
+
Provide an environmental impact analysis based on the following information:
|
126 |
+
|
127 |
+
{text_input}
|
128 |
+
|
129 |
+
Include in your response:
|
130 |
+
1. Key environmental concerns identified
|
131 |
+
2. Potential ecological impacts - short and long term
|
132 |
+
3. Carbon footprint considerations
|
133 |
+
4. Waste and pollution factors
|
134 |
+
5. Biodiversity impacts
|
135 |
+
6. Actionable recommendations for sustainability
|
136 |
+
7. References to relevant environmental principles or frameworks
|
137 |
+
|
138 |
+
Be specific, thorough, and provide practical advice.
|
139 |
+
"""
|
140 |
+
|
141 |
+
messages = [
|
142 |
+
{
|
143 |
+
"role": "user",
|
144 |
+
"content": prompt
|
145 |
+
}
|
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", # 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 |
|
163 |
# Create Gradio interface
|
164 |
+
with gr.Blocks(title="Smart Environmental Impact Analyzer") as app:
|
165 |
+
gr.Markdown("# 🌍 Smart Environmental Impact Analyzer")
|
166 |
+
gr.Markdown("""
|
167 |
+
This tool analyzes environmental impacts using Gemini 2.5 Pro AI.
|
168 |
+
Choose an analysis type and provide the required information.
|
169 |
+
""")
|
170 |
+
|
171 |
+
api_key = gr.Textbox(label="OpenRouter API Key", placeholder="Enter your OpenRouter API key", type="password")
|
172 |
+
|
173 |
+
with gr.Tabs():
|
174 |
+
with gr.TabItem("Image Analysis"):
|
175 |
+
image_input = gr.Image(type="pil", label="Upload an image for environmental analysis")
|
176 |
+
image_submit = gr.Button("Analyze Image")
|
177 |
+
image_output = gr.Textbox(label="Analysis Results", lines=15)
|
178 |
+
|
179 |
+
image_submit.click(
|
180 |
+
analyze_environmental_impact,
|
181 |
+
inputs=[api_key, gr.Textbox(value="Image Analysis", visible=False), image_input, None, None, None],
|
182 |
+
outputs=image_output
|
183 |
+
)
|
184 |
+
|
185 |
+
with gr.TabItem("Geographical Assessment"):
|
186 |
+
location_input = gr.Textbox(label="Location (city, region, or country)", placeholder="e.g., Paris, France")
|
187 |
+
location_submit = gr.Button("Analyze Location")
|
188 |
+
location_output = gr.Textbox(label="Analysis Results", lines=15)
|
189 |
+
|
190 |
+
location_submit.click(
|
191 |
+
analyze_environmental_impact,
|
192 |
+
inputs=[api_key, gr.Textbox(value="Geographical Assessment", visible=False), None, None, location_input, None],
|
193 |
+
outputs=location_output
|
194 |
+
)
|
195 |
+
|
196 |
+
with gr.TabItem("Product Assessment"):
|
197 |
+
product_info = gr.Textbox(
|
198 |
+
label="Product Information",
|
199 |
+
placeholder="Describe the product, materials, manufacturing process, lifecycle, etc.",
|
200 |
+
lines=5
|
201 |
+
)
|
202 |
+
product_submit = gr.Button("Analyze Product")
|
203 |
+
product_output = gr.Textbox(label="Analysis Results", lines=15)
|
204 |
+
|
205 |
+
product_submit.click(
|
206 |
+
analyze_environmental_impact,
|
207 |
+
inputs=[api_key, gr.Textbox(value="Product Assessment", visible=False), None, None, None, product_info],
|
208 |
+
outputs=product_output
|
209 |
+
)
|
210 |
+
|
211 |
+
with gr.TabItem("Custom Query"):
|
212 |
+
custom_input = gr.Textbox(
|
213 |
+
label="Custom Environmental Query",
|
214 |
+
placeholder="Enter your environmental question or describe a scenario to analyze",
|
215 |
+
lines=5
|
216 |
+
)
|
217 |
+
custom_submit = gr.Button("Analyze")
|
218 |
+
custom_output = gr.Textbox(label="Analysis Results", lines=15)
|
219 |
+
|
220 |
+
custom_submit.click(
|
221 |
+
analyze_environmental_impact,
|
222 |
+
inputs=[api_key, gr.Textbox(value="Custom Query", visible=False), None, custom_input, None, None],
|
223 |
+
outputs=custom_output
|
224 |
+
)
|
225 |
+
|
226 |
+
gr.Markdown("""
|
227 |
+
### Privacy Notice
|
228 |
+
Your API key is used only for making requests to OpenRouter and is not stored or logged.
|
229 |
+
The images and text you submit are processed by Gemini 2.5 Pro through OpenRouter's API.
|
230 |
+
|
231 |
+
### Usage Instructions
|
232 |
+
1. Enter your OpenRouter API key (get one from https://openrouter.ai)
|
233 |
+
2. Select the type of analysis you want to perform
|
234 |
+
3. Provide the required information (image, location, product details, or custom query)
|
235 |
+
4. Click the "Analyze" button for your selected tab
|
236 |
+
""")
|
237 |
|
238 |
+
# Launch the app
|
239 |
+
if __name__ == "__main__":
|
240 |
+
app.launch()
|