Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from groq import Groq
|
| 7 |
+
|
| 8 |
+
# Set up the API key (replace with your actual Groq API key)
|
| 9 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 10 |
+
|
| 11 |
+
# Groq client setup
|
| 12 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 13 |
+
|
| 14 |
+
# Function to generate traffic optimization strategies from Groq
|
| 15 |
+
def generate_traffic_optimization(data: str):
|
| 16 |
+
response = client.chat.completions.create(
|
| 17 |
+
messages=[
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": f"Generate a traffic flow optimization strategy for the following data: {data}",
|
| 21 |
+
}
|
| 22 |
+
],
|
| 23 |
+
model="llama3-8b-8192", # You can choose a different model if necessary
|
| 24 |
+
)
|
| 25 |
+
return response.choices[0].message.content
|
| 26 |
+
|
| 27 |
+
# Visualization function to generate the traffic flow chart
|
| 28 |
+
def generate_traffic_chart(df_filtered):
|
| 29 |
+
# Summarize traffic data by aggregating vehicle counts for each record
|
| 30 |
+
df_filtered['Total'] = df_filtered['CarCount'] + df_filtered['BikeCount'] + df_filtered['BusCount'] + df_filtered['TruckCount']
|
| 31 |
+
|
| 32 |
+
# Select relevant columns for the optimization request (you can adjust this as needed)
|
| 33 |
+
traffic_data_summary = df_filtered[['Time', 'Total', 'Traffic Situation', 'CarCount', 'BikeCount', 'BusCount', 'TruckCount']]
|
| 34 |
+
|
| 35 |
+
# Convert the summary to a string format for input to the Groq API
|
| 36 |
+
summary_str = traffic_data_summary.head(10).to_string(index=False)
|
| 37 |
+
|
| 38 |
+
# Get the optimization strategy from Groq
|
| 39 |
+
optimization_strategy = generate_traffic_optimization(summary_str)
|
| 40 |
+
|
| 41 |
+
# Visualization of traffic flow data
|
| 42 |
+
time_labels = df_filtered['Time'].head(10)
|
| 43 |
+
car_counts = df_filtered['CarCount'].head(10)
|
| 44 |
+
bike_counts = df_filtered['BikeCount'].head(10)
|
| 45 |
+
bus_counts = df_filtered['BusCount'].head(10)
|
| 46 |
+
truck_counts = df_filtered['TruckCount'].head(10)
|
| 47 |
+
|
| 48 |
+
# Create the stacked bar chart for vehicle counts
|
| 49 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 50 |
+
ax.bar(time_labels, car_counts, label='Cars', color='blue')
|
| 51 |
+
ax.bar(time_labels, bike_counts, bottom=car_counts, label='Bikes', color='green')
|
| 52 |
+
ax.bar(time_labels, bus_counts, bottom=np.array(car_counts) + np.array(bike_counts), label='Buses', color='red')
|
| 53 |
+
ax.bar(time_labels, truck_counts, bottom=np.array(car_counts) + np.array(bike_counts) + np.array(bus_counts), label='Trucks', color='yellow')
|
| 54 |
+
|
| 55 |
+
# Customize chart
|
| 56 |
+
ax.set_xlabel('Time')
|
| 57 |
+
ax.set_ylabel('Vehicle Count')
|
| 58 |
+
ax.set_title('Traffic Flow by Vehicle Type')
|
| 59 |
+
ax.legend()
|
| 60 |
+
|
| 61 |
+
# Save the plot as a file
|
| 62 |
+
plt.xticks(rotation=45)
|
| 63 |
+
plt.tight_layout()
|
| 64 |
+
chart_path = "/tmp/traffic_chart.png"
|
| 65 |
+
plt.savefig(chart_path)
|
| 66 |
+
plt.close()
|
| 67 |
+
|
| 68 |
+
return optimization_strategy, chart_path
|
| 69 |
+
|
| 70 |
+
# Function to process the uploaded file and run traffic optimization
|
| 71 |
+
def process_traffic_file(file):
|
| 72 |
+
# Load the dataset
|
| 73 |
+
df = pd.read_csv(file.name)
|
| 74 |
+
|
| 75 |
+
# Optionally, you may filter data for specific days or time intervals
|
| 76 |
+
# For example, let's filter the data for a specific day:
|
| 77 |
+
df_filtered = df[df['Day of the week'] == 'Monday']
|
| 78 |
+
|
| 79 |
+
# Generate traffic chart and optimization strategy
|
| 80 |
+
optimization_strategy, chart_path = generate_traffic_chart(df_filtered)
|
| 81 |
+
|
| 82 |
+
return optimization_strategy, chart_path
|
| 83 |
+
|
| 84 |
+
# Gradio interface
|
| 85 |
+
iface = gr.Interface(
|
| 86 |
+
fn=process_traffic_file,
|
| 87 |
+
inputs=gr.File(label="Upload CSV with Traffic Data"),
|
| 88 |
+
outputs=[gr.Textbox(label="Optimization Strategy"), gr.Image(label="Traffic Flow Chart")],
|
| 89 |
+
title="Traffic Flow Optimization",
|
| 90 |
+
description="Upload a CSV file with traffic data, and the app will generate traffic optimization strategies and visualize the traffic flow."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Launch the app
|
| 94 |
+
iface.launch()
|