File size: 2,937 Bytes
1369577
 
 
 
 
 
 
 
 
 
 
 
3897b29
1369577
 
 
 
3897b29
1369577
 
 
 
3897b29
1369577
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3897b29
1369577
 
 
 
ce6dba8
3897b29
1369577
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
from openai import OpenAI

# Function to initialize the client with the provided API key
def initialize_client(api_key):
    client = OpenAI(
        base_url="https://openrouter.ai/api/v1",
        api_key=api_key
    )
    return client

# Function to analyze environmental impact using Gemini 2.5 Pro
def analyze_environmental_impact(api_key, image=None, location=None, product_info=None):
    client = initialize_client(api_key)  # Initialize client with the user's 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}."}]
        })
    
    # Call the model for environmental analysis
    try:
        completion = client.chat.completions.create(
            extra_headers={
                "HTTP-Referer": "<YOUR_SITE_URL>",  # Optional
                "X-Title": "<YOUR_SITE_NAME>",      # Optional
            },
            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)}"

# Gradio interface
def gradio_interface(api_key, image, location, product_info):
    image_url = image if image else None  # For filepath, Gradio returns the path of the uploaded image
    result = analyze_environmental_impact(api_key, image_url=image_url, location=location, product_info=product_info)
    return result

# Create Gradio inputs and outputs
api_key_input = gr.Textbox(label="Enter Your OpenRouter API Key", type="password")
image_input = gr.Image(label="Upload an Image (Optional)", type="filepath")  # Fix: Changed to '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)

# Create Gradio interface
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."
)

# Launch the Gradio app
app.launch()