Engineer786 commited on
Commit
4db256c
·
verified ·
1 Parent(s): 7ab05ba

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +85 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import plotly.express as px
4
+ from groq import Groq
5
+ import gradio as gr
6
+
7
+
8
+ GROQ_API_KEY ="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"
9
+ client = Groq(api_key=GROQ_API_KEY)
10
+
11
+ # Function to process the traffic dataset
12
+ def process_traffic_data(csv_file):
13
+ data = pd.read_csv(csv_file)
14
+ peak_hours = data.groupby("Time")["Total"].sum().idxmax()
15
+ worst_situation = data.groupby("Traffic Situation")["Total"].sum().idxmax()
16
+ vehicle_contribution = (
17
+ data[["CarCount", "BikeCount", "BusCount", "TruckCount"]]
18
+ .sum()
19
+ .apply(lambda x: round((x / data["Total"].sum()) * 100, 2))
20
+ )
21
+ summary = (
22
+ f"Peak traffic hour: {peak_hours}. Worst traffic situation: {worst_situation}.\n"
23
+ f"Vehicle Contribution to Traffic:\n"
24
+ f"- Cars: {vehicle_contribution['CarCount']}%\n"
25
+ f"- Bikes: {vehicle_contribution['BikeCount']}%\n"
26
+ f"- Buses: {vehicle_contribution['BusCount']}%\n"
27
+ f"- Trucks: {vehicle_contribution['TruckCount']}%\n"
28
+ )
29
+ return data, summary
30
+
31
+ # Function to generate suggestions using Groq API
32
+ def generate_suggestions(summary):
33
+ input_content = (
34
+ f"Here is the detailed traffic data summary:\n{summary}"
35
+ f"Suggest realistic traffic optimization strategies with supporting stats."
36
+ )
37
+ chat_completion = client.chat.completions.create(
38
+ messages=[{"role": "user", "content": input_content}],
39
+ model="llama3-8b-8192",
40
+ )
41
+ return chat_completion.choices[0].message.content
42
+
43
+ # Function to create traffic visualization
44
+ def create_visualization(data):
45
+ fig = px.bar(
46
+ data,
47
+ x="Day of the week",
48
+ y="Total",
49
+ color="Traffic Situation",
50
+ title="Total Traffic by Day and Traffic Situation",
51
+ labels={"Total": "Total Vehicles", "Day of the week": "Day"},
52
+ )
53
+ return fig.to_html()
54
+
55
+ # Gradio interface function
56
+ def analyze_traffic(file):
57
+ data, summary = process_traffic_data(file.name)
58
+ suggestions = generate_suggestions(summary)
59
+ visualization_html = create_visualization(data)
60
+ return summary, suggestions, visualization_html
61
+
62
+ # Gradio interface definition
63
+ with gr.Blocks() as demo:
64
+ gr.Markdown("# Traffic Optimization Application")
65
+ gr.Markdown(
66
+ "Upload a CSV file with traffic data to generate traffic flow optimization suggestions and visualize traffic patterns."
67
+ )
68
+
69
+ with gr.Row():
70
+ file_input = gr.File(label="Upload Traffic Data CSV")
71
+ analyze_button = gr.Button("Analyze Traffic Data")
72
+
73
+ with gr.Row():
74
+ summary_output = gr.Textbox(label="Traffic Data Summary", lines=8)
75
+ suggestions_output = gr.Textbox(label="Optimization Suggestions", lines=8)
76
+
77
+ visualization_output = gr.HTML(label="Traffic Visualization")
78
+
79
+ analyze_button.click(
80
+ analyze_traffic, inputs=file_input, outputs=[summary_output, suggestions_output, visualization_output]
81
+ )
82
+
83
+ # Launch Gradio app
84
+ if __name__ == "__main__":
85
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ pandas
2
+ plotly
3
+ gradio
4
+ groq