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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import base64
|
| 9 |
+
|
| 10 |
+
def analyze_environmental_impact(api_key, analysis_type, image=None, text_input=None, location=None, product_info=None):
|
| 11 |
+
"""
|
| 12 |
+
Analyze environmental impact based on user inputs using Gemini 2.5 Pro through OpenRouter.
|
| 13 |
+
"""
|
| 14 |
+
if not api_key:
|
| 15 |
+
return "Please provide an OpenRouter API key."
|
| 16 |
+
|
| 17 |
+
client = OpenAI(
|
| 18 |
+
base_url="https://openrouter.ai/api/v1",
|
| 19 |
+
api_key=api_key,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Prepare messages based on analysis type
|
| 23 |
+
if analysis_type == "Image Analysis":
|
| 24 |
+
if image is None:
|
| 25 |
+
return "Please upload an image for analysis."
|
| 26 |
+
|
| 27 |
+
# Save image to a temporary file
|
| 28 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
| 29 |
+
image_path = temp_file.name
|
| 30 |
+
image.save(image_path)
|
| 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
|
| 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",
|
| 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:
|
| 173 |
+
gr.Markdown("# 🌍 Smart Environmental Impact Analyzer")
|
| 174 |
+
gr.Markdown("""
|
| 175 |
+
This tool analyzes environmental impacts using Gemini 2.5 Pro AI.
|
| 176 |
+
Choose an analysis type and provide the required information.
|
| 177 |
+
""")
|
| 178 |
+
|
| 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),
|
| 194 |
+
gr.Textbox(value="", visible=False),
|
| 195 |
+
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),
|
| 212 |
+
location_input,
|
| 213 |
+
gr.Textbox(value="", visible=False)
|
| 214 |
+
],
|
| 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),
|
| 234 |
+
gr.Textbox(value="", visible=False),
|
| 235 |
+
product_info
|
| 236 |
+
],
|
| 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,
|
| 256 |
+
gr.Textbox(value="", visible=False),
|
| 257 |
+
gr.Textbox(value="", visible=False)
|
| 258 |
+
],
|
| 259 |
+
outputs=custom_output
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
gr.Markdown("""
|
| 263 |
+
### Privacy Notice
|
| 264 |
+
Your API key is used only for making requests to OpenRouter and is not stored or logged.
|
| 265 |
+
The images and text you submit are processed by Gemini 2.5 Pro through OpenRouter's API.
|
| 266 |
+
|
| 267 |
+
### Usage Instructions
|
| 268 |
+
1. Enter your OpenRouter API key (get one from https://openrouter.ai)
|
| 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
|
| 275 |
+
if __name__ == "__main__":
|
| 276 |
+
app.launch()
|