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 the image is provided, include it in the analysis 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 is provided, include it in the analysis if location: messages.append({ "role": "user", "content": [{"type": "text", "text": f"Analyze the environmental impact of {location}."}] }) # If product information is provided, include it in the analysis 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": "", # Optional "X-Title": "", # 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 # Gradio returns file path as string for "filepath" type result = analyze_environmental_impact(api_key, image=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="text") 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()