Afeefa123 commited on
Commit
12dd9a0
·
verified ·
1 Parent(s): b6150c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -46
app.py CHANGED
@@ -1,81 +1,86 @@
1
  import os
2
  import pandas as pd
3
- import gradio as gr
4
  import plotly.express as px
5
  from groq import Groq
 
 
6
 
7
  GROQ_API_KEY ="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"
8
 
9
  client = Groq(api_key=GROQ_API_KEY)
10
 
11
- # Function to read and process the dataset
12
  def process_traffic_data(csv_file):
13
- # Load the CSV dataset
14
  data = pd.read_csv(csv_file)
15
- # Example: Calculate peak hours and most congested traffic situations
16
- peak_hours = data.groupby('Time')['Total'].sum().idxmax()
17
- worst_situation = data.groupby('Traffic Situation')['Total'].sum().idxmax()
18
- summary = f"Peak traffic hour: {peak_hours}. Worst traffic situation: {worst_situation}."
 
 
 
 
 
 
 
 
 
 
 
19
  return data, summary
20
 
21
- # Function to generate suggestions based on the dataset
22
  def generate_suggestions(summary):
23
- input_content = f"Here is the traffic data summary: {summary}. Provide strategies to optimize traffic flow."
 
 
 
24
  chat_completion = client.chat.completions.create(
25
  messages=[{"role": "user", "content": input_content}],
26
  model="llama3-8b-8192",
27
  )
28
  return chat_completion.choices[0].message.content
29
 
30
- # Function to create a visual simulation of traffic flow
31
- def visualize_traffic_flow(data):
32
- # Example: Line chart showing total traffic flow over time
33
- fig = px.line(
34
  data,
35
- x="Time",
36
  y="Total",
37
  color="Traffic Situation",
38
- title="Traffic Flow Simulation",
39
- labels={"Total": "Total Vehicles", "Time": "Time of Day"},
40
  )
41
  return fig.to_html()
42
 
43
- # Function to combine all functionalities
44
- def traffic_optimizer(csv_file):
45
- # Process the dataset
46
- data, summary = process_traffic_data(csv_file)
47
-
48
- # Generate suggestions
49
  suggestions = generate_suggestions(summary)
50
-
51
- # Visual simulation
52
- visualization = visualize_traffic_flow(data)
53
-
54
- return summary, suggestions, visualization
55
 
56
- # Gradio Interface
57
  with gr.Blocks() as demo:
58
- gr.Markdown("# Traffic Flow Optimization App")
59
- gr.Markdown("Upload your traffic data CSV file to get optimization suggestions and a visual simulation.")
60
-
61
- with gr.Row():
62
- csv_input = gr.File(label="Upload CSV File", type="file")
63
-
64
  with gr.Row():
65
- summary_output = gr.Textbox(label="Traffic Summary")
66
- suggestions_output = gr.Textbox(label="Optimization Suggestions")
67
-
68
  with gr.Row():
69
- visualization_output = gr.HTML(label="Traffic Flow Simulation")
70
-
71
- submit_btn = gr.Button("Generate Insights")
72
-
73
- submit_btn.click(
74
- traffic_optimizer,
75
- inputs=[csv_input],
76
- outputs=[summary_output, suggestions_output, visualization_output],
77
  )
78
 
79
- # Run the Gradio app
80
  if __name__ == "__main__":
81
  demo.launch()
 
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()