Afeefa123 commited on
Commit
a99fb8d
·
verified ·
1 Parent(s): a7c5291

Update app.py

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