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gradio app
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import json
|
| 5 |
+
from Agent import create_environmental_analyzer
|
| 6 |
+
|
| 7 |
+
def analyze_environmental_impact(product_description):
|
| 8 |
+
"""Main analysis function that processes input and returns formatted results"""
|
| 9 |
+
|
| 10 |
+
if not product_description.strip():
|
| 11 |
+
return "Please enter a product description to analyze.", "", "", ""
|
| 12 |
+
|
| 13 |
+
# Initialize the analyzer
|
| 14 |
+
analyzer = create_environmental_analyzer()
|
| 15 |
+
|
| 16 |
+
# Run the analysis
|
| 17 |
+
initial_state = {
|
| 18 |
+
"messages": [],
|
| 19 |
+
"product_description": product_description,
|
| 20 |
+
"extracted_data": {},
|
| 21 |
+
"carbon_footprint": 0.0,
|
| 22 |
+
"environmental_score": 0.0,
|
| 23 |
+
"recommendations": [],
|
| 24 |
+
"analysis_complete": False
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Process through the workflow
|
| 28 |
+
final_state = analyzer.invoke(initial_state)
|
| 29 |
+
|
| 30 |
+
# Format results for display
|
| 31 |
+
score_display, status_message, details_json, recommendations_text = format_results(final_state)
|
| 32 |
+
|
| 33 |
+
return score_display, status_message, details_json, recommendations_text
|
| 34 |
+
|
| 35 |
+
def format_results(state):
|
| 36 |
+
"""Format the analysis results for Gradio display"""
|
| 37 |
+
|
| 38 |
+
# Environmental Score Display
|
| 39 |
+
score = state['environmental_score']
|
| 40 |
+
carbon = state['carbon_footprint']
|
| 41 |
+
|
| 42 |
+
score_display = f"""
|
| 43 |
+
## Environmental Impact Score
|
| 44 |
+
|
| 45 |
+
**Score: {score}/100**
|
| 46 |
+
|
| 47 |
+
**Carbon Footprint: {carbon:.2f} kg CO2e**
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
# Status Message based on score
|
| 51 |
+
if score >= 80:
|
| 52 |
+
status_message = "🌟 **Excellent environmental performance!**\n\nThis product demonstrates outstanding sustainability practices."
|
| 53 |
+
elif score >= 60:
|
| 54 |
+
status_message = "⚡ **Good, but room for improvement**\n\nThis product has decent environmental performance with potential for enhancement."
|
| 55 |
+
else:
|
| 56 |
+
status_message = "🚨 **High environmental impact**\n\nThis product has significant environmental concerns that should be addressed."
|
| 57 |
+
|
| 58 |
+
# Analysis Details as JSON
|
| 59 |
+
details_json = state['extracted_data']
|
| 60 |
+
|
| 61 |
+
# Recommendations as formatted text
|
| 62 |
+
recommendations_text = "## Sustainability Recommendations\n\n"
|
| 63 |
+
if state['recommendations']:
|
| 64 |
+
for i, rec in enumerate(state['recommendations'], 1):
|
| 65 |
+
recommendations_text += f"{i}. {rec}\n\n"
|
| 66 |
+
else:
|
| 67 |
+
recommendations_text += "No specific recommendations available."
|
| 68 |
+
|
| 69 |
+
return score_display, status_message, details_json, recommendations_text
|
| 70 |
+
|
| 71 |
+
def create_gradio_interface():
|
| 72 |
+
"""Create the Gradio interface"""
|
| 73 |
+
|
| 74 |
+
with gr.Blocks(
|
| 75 |
+
title="🌱 AI Environmental Impact Analyzer",
|
| 76 |
+
theme=gr.themes.Soft()
|
| 77 |
+
) as interface:
|
| 78 |
+
|
| 79 |
+
# Header
|
| 80 |
+
gr.Markdown("# 🌱 AI Environmental Impact Analyzer")
|
| 81 |
+
gr.Markdown("Analyze the environmental footprint of consumer products using AI")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column(scale=2):
|
| 85 |
+
# Input section
|
| 86 |
+
product_input = gr.Textbox(
|
| 87 |
+
label="Product Description",
|
| 88 |
+
placeholder="e.g., Organic cotton t-shirt manufactured in India, packaged in recyclable materials...",
|
| 89 |
+
lines=4,
|
| 90 |
+
max_lines=8
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
analyze_btn = gr.Button(
|
| 94 |
+
"🔍 Analyze Environmental Impact",
|
| 95 |
+
variant="primary",
|
| 96 |
+
size="lg"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
with gr.Column(scale=3):
|
| 100 |
+
# Results section
|
| 101 |
+
with gr.Group():
|
| 102 |
+
score_output = gr.Markdown(
|
| 103 |
+
label="Environmental Score",
|
| 104 |
+
value="Enter a product description and click analyze to see results."
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
status_output = gr.Markdown(
|
| 108 |
+
label="Performance Status"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Detailed results in tabs
|
| 112 |
+
with gr.Tabs():
|
| 113 |
+
with gr.Tab("📊 Analysis Details"):
|
| 114 |
+
details_output = gr.JSON(
|
| 115 |
+
label="Extracted Environmental Data",
|
| 116 |
+
value={}
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with gr.Tab("💡 Recommendations"):
|
| 120 |
+
recommendations_output = gr.Markdown(
|
| 121 |
+
label="Sustainability Recommendations",
|
| 122 |
+
value="Analysis results will appear here..."
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Event handlers
|
| 126 |
+
analyze_btn.click(
|
| 127 |
+
fn=analyze_environmental_impact,
|
| 128 |
+
inputs=[product_input],
|
| 129 |
+
outputs=[score_output, status_output, details_output, recommendations_output],
|
| 130 |
+
show_progress=True
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Optional: Add examples
|
| 134 |
+
gr.Examples(
|
| 135 |
+
examples=[
|
| 136 |
+
["Organic cotton t-shirt manufactured in India, packaged in recyclable cardboard"],
|
| 137 |
+
["Plastic water bottle made from recycled materials, shipped internationally"],
|
| 138 |
+
["Local handmade wooden furniture using sustainable forest wood"],
|
| 139 |
+
["Electronic smartphone with aluminum body, manufactured in China"]
|
| 140 |
+
],
|
| 141 |
+
inputs=[product_input],
|
| 142 |
+
label="Example Products to Analyze"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
return interface
|
| 146 |
+
|
| 147 |
+
def main():
|
| 148 |
+
"""Launch the Gradio application"""
|
| 149 |
+
interface = create_gradio_interface()
|
| 150 |
+
|
| 151 |
+
# Launch with custom settings
|
| 152 |
+
interface.launch()
|
| 153 |
+
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
+
main()
|