| |
|
|
| import gradio as gr |
| import json |
| from Agent import create_environmental_analyzer |
|
|
| def analyze_environmental_impact(product_description): |
| """Main analysis function that processes input and returns formatted results""" |
| |
| if not product_description.strip(): |
| return "Please enter a product description to analyze.", "", "", "" |
| |
| |
| analyzer = create_environmental_analyzer() |
| |
| |
| initial_state = { |
| "messages": [], |
| "product_description": product_description, |
| "extracted_data": {}, |
| "carbon_footprint": 0.0, |
| "environmental_score": 0.0, |
| "recommendations": [], |
| "analysis_complete": False |
| } |
| |
| |
| final_state = analyzer.invoke(initial_state) |
| |
| |
| score_display, status_message, details_json, recommendations_text = format_results(final_state) |
| |
| return score_display, status_message, details_json, recommendations_text |
|
|
| def format_results(state): |
| """Format the analysis results for Gradio display""" |
| |
| |
| score = state['environmental_score'] |
| carbon = state['carbon_footprint'] |
| |
| score_display = f""" |
| ## Environmental Impact Score |
| |
| **Score: {score}/100** |
| |
| **Carbon Footprint: {carbon:.2f} kg CO2e** |
| """ |
| |
| |
| if score >= 80: |
| status_message = "🌟 **Excellent environmental performance!**\n\nThis product demonstrates outstanding sustainability practices." |
| elif score >= 60: |
| status_message = "⚡ **Good, but room for improvement**\n\nThis product has decent environmental performance with potential for enhancement." |
| else: |
| status_message = "🚨 **High environmental impact**\n\nThis product has significant environmental concerns that should be addressed." |
| |
| |
| details_json = state['extracted_data'] |
| |
| |
| recommendations_text = "## Sustainability Recommendations\n\n" |
| if state['recommendations']: |
| for i, rec in enumerate(state['recommendations'], 1): |
| recommendations_text += f"{i}. {rec}\n\n" |
| else: |
| recommendations_text += "No specific recommendations available." |
| |
| return score_display, status_message, details_json, recommendations_text |
|
|
| def create_gradio_interface(): |
| """Create the Gradio interface""" |
| |
| with gr.Blocks( |
| title="🌱 AI Environmental Impact Analyzer", |
| theme=gr.themes.Soft() |
| ) as interface: |
| |
| |
| gr.Markdown("# 🌱 AI Environmental Impact Analyzer") |
| gr.Markdown("Analyze the environmental footprint of consumer products using AI") |
| |
| with gr.Row(): |
| with gr.Column(scale=2): |
| |
| product_input = gr.Textbox( |
| label="Product Description/URL", |
| placeholder="e.g., Organic cotton t-shirt manufactured in India, packaged in recyclable materials...", |
| lines=4, |
| max_lines=8 |
| ) |
| |
| analyze_btn = gr.Button( |
| "🔍 Analyze Environmental Impact", |
| variant="primary", |
| size="lg" |
| ) |
| |
| with gr.Column(scale=3): |
| |
| with gr.Group(): |
| score_output = gr.Markdown( |
| label="Environmental Score", |
| value="Enter a product description/URL and click analyze to see results." |
| ) |
| |
| status_output = gr.Markdown( |
| label="Performance Status" |
| ) |
| |
| |
| with gr.Tabs(): |
| with gr.Tab("📊 Analysis Details"): |
| details_output = gr.JSON( |
| label="Extracted Environmental Data", |
| value={} |
| ) |
| |
| with gr.Tab("💡 Recommendations"): |
| recommendations_output = gr.Markdown( |
| label="Sustainability Recommendations", |
| value="Analysis results will appear here..." |
| ) |
| |
| |
| analyze_btn.click( |
| fn=analyze_environmental_impact, |
| inputs=[product_input], |
| outputs=[score_output, status_output, details_output, recommendations_output], |
| show_progress=True |
| ) |
| |
| |
| gr.Examples( |
| examples=[ |
| ["Organic cotton t-shirt manufactured in India, packaged in recyclable cardboard"], |
| ["Plastic water bottle made from recycled materials, shipped internationally"], |
| ["Local handmade wooden furniture using sustainable forest wood"], |
| ["Electronic smartphone with aluminum body, manufactured in China"] |
| ], |
| inputs=[product_input], |
| label="Example Products to Analyze" |
| ) |
| gr.Markdown(""" |
| **Note**: add the Following Secret Key to your environment variables: |
| - `HF_API_KEY` (Hugging Face API token) to access private models or higher rate limits. |
| - `CLIMATIQ_API_KEY` (Climatiq API token) for carbon footprint calculations. |
| """) |
| |
| return interface |
|
|
| def main(): |
| """Launch the Gradio application""" |
| interface = create_gradio_interface() |
| |
| |
| interface.launch() |
|
|
| if __name__ == "__main__": |
| main() |
|
|