Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| from datetime import datetime | |
| import os | |
| import plotly.graph_objects as go | |
| from transformers import pipeline | |
| # Load the image classification model | |
| classifier = pipeline("image-classification", model="google/vit-base-patch16-224") | |
| # CSV file to store reports | |
| CSV_FILE = "eco_reports.csv" | |
| # Load existing reports or create a new DataFrame | |
| if os.path.exists(CSV_FILE): | |
| df = pd.read_csv(CSV_FILE) | |
| else: | |
| df = pd.DataFrame(columns=[ | |
| "timestamp", "country", "city", "area", "sdg_category", "description", "image_path", "latitude", "longitude" | |
| ]) | |
| # Submit a new report | |
| def submit_report(country, city, area, sdg_category, description, image, latitude, longitude): | |
| timestamp = datetime.now().isoformat() | |
| image_path = image if image else "" | |
| new_report = { | |
| "timestamp": timestamp, | |
| "country": country, | |
| "city": city, | |
| "area": area, | |
| "sdg_category": sdg_category, | |
| "description": description, | |
| "image_path": image_path, | |
| "latitude": latitude, | |
| "longitude": longitude | |
| } | |
| global df | |
| df = pd.concat([df, pd.DataFrame([new_report])], ignore_index=True) | |
| df.to_csv(CSV_FILE, index=False) | |
| return "β Your report has been submitted. Thank you!" | |
| # Filter reports by SDG or city | |
| def filter_reports(sdg_category, city): | |
| filtered_df = df.copy() | |
| if sdg_category != "All": | |
| filtered_df = filtered_df[filtered_df["sdg_category"] == sdg_category] | |
| if city != "All": | |
| filtered_df = filtered_df[filtered_df["city"] == city] | |
| return [ | |
| gr.Image(value=row["image_path"], label=f"{row['city']} | {row['sdg_category']} | {row['description']}") | |
| for _, row in filtered_df.iterrows() | |
| ] | |
| # Generate a map of all reports | |
| def generate_map(): | |
| if df.empty or df["latitude"].isnull().all(): | |
| return go.Figure() | |
| fig = go.Figure(go.Scattermapbox( | |
| lat=df["latitude"], | |
| lon=df["longitude"], | |
| mode="markers", | |
| marker=go.scattermapbox.Marker(size=9, color='green'), | |
| text=df["description"], | |
| hoverinfo="text" | |
| )) | |
| fig.update_layout( | |
| mapbox_style="open-street-map", | |
| mapbox_zoom=1.5, | |
| mapbox_center={"lat": 20, "lon": 0}, | |
| margin={"l":0, "r":0, "t":0, "b":0} | |
| ) | |
| return fig | |
| # Image classification | |
| def classify_image(image): | |
| predictions = classifier(image) | |
| return predictions[0]["label"] | |
| # --- Gradio Interfaces --- | |
| # Tab 1: Submit Report | |
| report_interface = gr.Interface( | |
| fn=submit_report, | |
| inputs=[ | |
| gr.Textbox(label="π Country", placeholder="e.g. Kenya"), | |
| gr.Textbox(label="π City", placeholder="e.g. Nairobi"), | |
| gr.Textbox(label="ποΈ Area", placeholder="e.g. Westlands"), | |
| gr.Dropdown(label="π SDG Category", choices=[ | |
| "SDG 13: Climate Action", "SDG 11: Sustainable Cities", "SDG 15: Life on Land" | |
| ]), | |
| gr.Textbox(label="π Description", lines=3, placeholder="Describe the environmental issue..."), | |
| gr.Image(type="filepath", label="π· Upload a Photo"), | |
| gr.Number(label="Latitude"), | |
| gr.Number(label="Longitude") | |
| ], | |
| outputs="text", | |
| title="π© Submit an Environmental Report", | |
| description="Help fight climate change by reporting visible environmental issues around you." | |
| ) | |
| # Tab 2: View Reports | |
| view_interface = gr.Interface( | |
| fn=filter_reports, | |
| inputs=[ | |
| gr.Dropdown(choices=["All", "SDG 13: Climate Action", "SDG 11: Sustainable Cities", "SDG 15: Life on Land"], label="Filter by SDG"), | |
| gr.Dropdown(choices=["All"] + sorted(df["city"].dropna().unique().tolist()), label="Filter by City") | |
| ], | |
| outputs=gr.Gallery(label="Filtered Reports"), | |
| title="π View Community Reports", | |
| description="Browse recent reports submitted by users." | |
| ) | |
| # Tab 3: Map of Reports | |
| map_interface = gr.Interface( | |
| fn=generate_map, | |
| inputs=[], | |
| outputs=gr.Plot(), | |
| title="πΊοΈ Map of Reports", | |
| description="See where environmental issues are being reported." | |
| ) | |
| # Tab 4: AI Image Tagging | |
| ai_interface = gr.Interface( | |
| fn=classify_image, | |
| inputs=gr.Image(type="pil", label="Upload an Image"), | |
| outputs=gr.Textbox(label="Predicted Category"), | |
| title="π€ AI Tagging of Images", | |
| description="Use AI to classify the environmental issue in the photo." | |
| ) | |
| # Launch the app with all tabs | |
| app = gr.TabbedInterface( | |
| [report_interface, view_interface, map_interface, ai_interface], | |
| tab_names=["Submit Report", "View Reports", "Map View", "AI Tagging"] | |
| ) | |
| app.launch() | |