|
import gradio as gr |
|
from openai import OpenAI |
|
|
|
|
|
def initialize_client(api_key): |
|
client = OpenAI( |
|
base_url="https://openrouter.ai/api/v1", |
|
api_key=api_key |
|
) |
|
return client |
|
|
|
|
|
def analyze_environmental_impact(api_key, image=None, location=None, product_info=None): |
|
client = initialize_client(api_key) |
|
|
|
messages = [] |
|
|
|
|
|
if image: |
|
messages.append({ |
|
"role": "user", |
|
"content": [ |
|
{"type": "text", "text": "Analyze the environmental impact of this image."}, |
|
{"type": "image_url", "image_url": {"url": image}} |
|
] |
|
}) |
|
|
|
|
|
if location: |
|
messages.append({ |
|
"role": "user", |
|
"content": [{"type": "text", "text": f"Analyze the environmental impact of {location}."}] |
|
}) |
|
|
|
|
|
if product_info: |
|
messages.append({ |
|
"role": "user", |
|
"content": [{"type": "text", "text": f"Analyze the environmental impact of this product: {product_info}."}] |
|
}) |
|
|
|
|
|
try: |
|
completion = client.chat.completions.create( |
|
extra_headers={ |
|
"HTTP-Referer": "<YOUR_SITE_URL>", |
|
"X-Title": "<YOUR_SITE_NAME>", |
|
}, |
|
model="google/gemini-2.5-pro-exp-03-25:free", |
|
messages=messages |
|
) |
|
|
|
response = completion.choices[0].message.content |
|
return response |
|
|
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
def gradio_interface(api_key, image, location, product_info): |
|
image_url = image if image else None |
|
result = analyze_environmental_impact(api_key, image=image_url, location=location, product_info=product_info) |
|
return result |
|
|
|
|
|
api_key_input = gr.Textbox(label="Enter Your OpenRouter API Key", type="text") |
|
image_input = gr.Image(label="Upload an Image (Optional)", type="filepath") |
|
location_input = gr.Textbox(label="Enter Location for Environmental Impact (Optional)", type="text") |
|
product_info_input = gr.Textbox(label="Enter Product Information (Optional)", type="text") |
|
|
|
output = gr.Textbox(label="Environmental Impact Analysis Output", lines=10) |
|
|
|
|
|
app = gr.Interface( |
|
fn=gradio_interface, |
|
inputs=[api_key_input, image_input, location_input, product_info_input], |
|
outputs=output, |
|
title="Smart Environmental Impact Analyzer", |
|
description="Analyze environmental impact based on images, location, or product information. Please provide your OpenRouter API key." |
|
) |
|
|
|
|
|
app.launch() |