Spaces:
Runtime error
Runtime error
| import os | |
| os.system('pip install matplotlib') | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import gradio as gr | |
| from groq import Groq | |
| GROQ_API_KEY = "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" | |
| client = Groq(api_key=GROQ_API_KEY) | |
| def analyze_traffic(file): | |
| # Load CSV file | |
| traffic_data = pd.read_csv(file.name) | |
| # Add a 'TotalVehicles' column to sum up the total number of vehicles per row | |
| traffic_data['TotalVehicles'] = traffic_data['CarCount'] + traffic_data['BikeCount'] + traffic_data['BusCount'] + traffic_data['TruckCount'] | |
| # Group by 'Day of the week' and calculate the total number of vehicles for each day | |
| daywise_traffic = traffic_data.groupby('Day of the week')['TotalVehicles'].sum().reset_index() | |
| # Sort by 'Day of the week' to display in order (assuming day names are in order) | |
| day_of_week_order = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] | |
| daywise_traffic['Day of the week'] = pd.Categorical(daywise_traffic['Day of the week'], categories=day_of_week_order, ordered=True) | |
| daywise_traffic = daywise_traffic.sort_values('Day of the week') | |
| # Plot the total number of vehicles for each day of the week | |
| plt.figure(figsize=(10, 6)) | |
| plt.bar(daywise_traffic['Day of the week'], daywise_traffic['TotalVehicles'], color='skyblue', alpha=0.7) | |
| plt.title("Total Vehicle Count by Day of the Week") | |
| plt.xlabel("Day of the Week") | |
| plt.ylabel("Total Number of Vehicles") | |
| plt.xticks(rotation=45) | |
| plt.grid(True) | |
| plt.tight_layout() | |
| # Save the plot to an image file | |
| plot_path = "traffic_plot.png" | |
| plt.savefig(plot_path) | |
| plt.close() | |
| # Generate summary statistics | |
| summary = traffic_data.describe() | |
| # Prepare RAG prompt with detailed questions for improvement suggestions | |
| prompt = f""" | |
| Based on the following traffic data and the summary statistics, provide realistic suggestions for improving traffic flow and reducing congestion: | |
| 1. What strategies can be implemented to reduce peak-hour traffic congestion, especially on days like Friday and Monday, based on the 'TotalVehicles' counts for those days? | |
| 2. How can the number of vehicles for each type (Car, Bike, Bus, Truck) be optimized to create smoother traffic flow and better distribution across different types of vehicles? | |
| 3. Suggest traffic management techniques, such as signal timings, road expansions, or public transport incentives, that could be employed based on traffic volume trends. | |
| 4. What other factors (e.g., weather, special events, public holidays) should be considered to improve the flow, and how can this data be integrated into the model? | |
| Summary statistics of the traffic data: | |
| {summary.to_string()} | |
| Sample Data: | |
| {traffic_data.head().to_string()} | |
| """ | |
| # Use Groq API to generate strategies | |
| chat_completion = client.chat.completions.create( | |
| messages=[{"role": "user", "content": prompt}], | |
| model="llama3-8b-8192" | |
| ) | |
| # Extract and return Groq's response and the plot image path | |
| optimization_suggestions = chat_completion.choices[0].message.content | |
| return optimization_suggestions, plot_path | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=analyze_traffic, | |
| inputs=gr.inputs.File(label="Upload Traffic Data CSV"), | |
| outputs=[gr.outputs.Textbox(label="Optimization Suggestions"), gr.outputs.Image(label="Traffic Data Plot")], | |
| title="Traffic Flow Optimization", | |
| description="Upload your traffic data (CSV file) to analyze and get traffic optimization suggestions based on real-time data. The analysis includes traffic flow optimization recommendations and a plot of vehicle counts by day of the week." | |
| ) | |
| # Launch Gradio interface | |
| iface.launch() | |