| import os |
| import pandas as pd |
| import numpy as np |
| import matplotlib.pyplot as plt |
| import gradio as gr |
| from groq import Groq |
|
|
| |
| os.environ["GROQ_API_KEY"] = "My_API_key" |
|
|
| |
| client = Groq(api_key=os.environ.get("My_API_key")) |
|
|
| |
| def generate_traffic_optimization(data: str): |
| response = client.chat.completions.create( |
| messages=[ |
| { |
| "role": "user", |
| "content": f"Generate a detailed traffic flow optimization strategy for the following data. Include peak hours, vehicle type distributions, and actionable suggestions to improve flow: {data}", |
| } |
| ], |
| model="llama3-8b-8192", |
| ) |
| return response.choices[0].message.content |
|
|
| |
| def generate_traffic_chart(df_filtered): |
| |
| df_filtered['Total'] = df_filtered['CarCount'] + df_filtered['BikeCount'] + df_filtered['BusCount'] + df_filtered['TruckCount'] |
|
|
| |
| traffic_data_summary = df_filtered[['Time', 'Total', 'Traffic Situation', 'CarCount', 'BikeCount', 'BusCount', 'TruckCount']] |
|
|
| |
| avg_car_count = df_filtered['CarCount'].mean() |
| avg_bike_count = df_filtered['BikeCount'].mean() |
| avg_bus_count = df_filtered['BusCount'].mean() |
| avg_truck_count = df_filtered['TruckCount'].mean() |
| peak_traffic_time = df_filtered.loc[df_filtered['Total'].idxmax()]['Time'] |
|
|
| |
| summary_str = traffic_data_summary.head(10).to_string(index=False) |
|
|
| |
| optimization_strategy = generate_traffic_optimization(summary_str) |
|
|
| |
| time_labels = df_filtered['Time'].head(10) |
| car_counts = df_filtered['CarCount'].head(10) |
| bike_counts = df_filtered['BikeCount'].head(10) |
| bus_counts = df_filtered['BusCount'].head(10) |
| truck_counts = df_filtered['TruckCount'].head(10) |
|
|
| |
| fig, ax = plt.subplots(figsize=(10, 6)) |
| ax.bar(time_labels, car_counts, label='Cars', color='blue') |
| ax.bar(time_labels, bike_counts, bottom=car_counts, label='Bikes', color='green') |
| ax.bar(time_labels, bus_counts, bottom=np.array(car_counts) + np.array(bike_counts), label='Buses', color='red') |
| ax.bar(time_labels, truck_counts, bottom=np.array(car_counts) + np.array(bike_counts) + np.array(bus_counts), label='Trucks', color='yellow') |
|
|
| |
| ax.set_xlabel('Time') |
| ax.set_ylabel('Vehicle Count') |
| ax.set_title('Traffic Flow by Vehicle Type') |
| ax.legend() |
|
|
| |
| plt.xticks(rotation=45) |
| plt.tight_layout() |
| chart_path = "/tmp/traffic_chart.png" |
| plt.savefig(chart_path) |
| plt.close() |
|
|
| |
| insights = f""" |
| Average Car Count: {avg_car_count:.2f} |
| Average Bike Count: {avg_bike_count:.2f} |
| Average Bus Count: {avg_bus_count:.2f} |
| Average Truck Count: {avg_truck_count:.2f} |
| Peak Traffic Time: {peak_traffic_time} |
| """ |
| |
| return optimization_strategy + "\n\n" + insights, chart_path |
|
|
| |
| def process_traffic_file(file): |
| |
| df = pd.read_csv(file.name) |
| |
| |
| |
| df_filtered = df[df['Day of the week'] == 'Monday'] |
|
|
| |
| optimization_strategy, chart_path = generate_traffic_chart(df_filtered) |
|
|
| return optimization_strategy, chart_path |
|
|
| |
| iface = gr.Interface( |
| fn=process_traffic_file, |
| inputs=gr.File(label="Upload CSV with Traffic Data"), |
| outputs=[gr.Textbox(label="Optimization Strategy and Insights"), gr.Image(label="Traffic Flow Chart")], |
| title="Traffic Flow Optimization", |
| description="Upload a CSV file with traffic data, and the app will generate traffic optimization strategies, provide insights, and visualize the traffic flow." |
| ) |
|
|
| |
| iface.launch() |
|
|