Yash Singhal commited on
Commit
48bbfda
·
1 Parent(s): ad3b107

modify app.py

Browse files
Files changed (1) hide show
  1. app.py +162 -9
app.py CHANGED
@@ -1,3 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pandas as pd
2
  import numpy as np
3
  import gradio as gr
@@ -55,7 +160,6 @@ class CrowdPredictor:
55
  self.prev_crowd_count = int(prediction[0][0])
56
 
57
  return self.prev_crowd_count
58
-
59
 
60
  def predict_batch(self, batch_data):
61
  os.system('cls')
@@ -68,8 +172,23 @@ class CrowdPredictor:
68
  prediction = self.predict_single(camera_location_input)
69
  predictions.append(prediction)
70
 
71
- return predictions
 
 
 
 
 
 
 
 
 
 
 
72
 
 
 
 
 
73
 
74
  # Instantiate the predictor
75
  predictor = CrowdPredictor()
@@ -77,27 +196,61 @@ predictor = CrowdPredictor()
77
  # Gradio interface setup
78
  with gr.Blocks() as prediction_block:
79
  gr.Label("Crowd Count Prediction")
 
 
80
  with gr.Tab("Single Prediction"):
81
- # camera_location_input = gr.Text(label="Camera Location (Category)")
82
- camera_location_input = gr.Dropdown(choices=[f"Camera_{i}" for i in range(1, 101)], label="Camera Location (Category)"),
83
  single_predict_btn = gr.Button("Predict")
84
  single_result = gr.Number(label="Predicted Crowd Count")
85
-
86
  single_predict_btn.click(
87
  predictor.predict_single,
88
- inputs=camera_location_input,
89
  outputs=single_result
90
  )
91
-
 
92
  with gr.Tab("Batch Prediction"):
93
  batch_input = gr.File(label="Upload CSV")
94
  batch_predict_btn = gr.Button("Predict")
95
- output = gr.Textbox(label="Predicted Crowd Counts")
 
 
 
 
 
 
 
 
 
96
 
97
  batch_predict_btn.click(
98
  predictor.predict_batch,
99
  inputs=batch_input,
100
- outputs=output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  )
102
 
 
103
  prediction_block.launch(share=True, debug=True)
 
1
+ # import pandas as pd
2
+ # import numpy as np
3
+ # import gradio as gr
4
+ # from tensorflow.keras.models import load_model
5
+ # from preprocess import create_features, cylindrical_encoding
6
+ # import pickle
7
+ # import os
8
+
9
+ # class CrowdPredictor:
10
+ # def __init__(self):
11
+ # # Load model, encoder, and scaler
12
+ # self.model = load_model('lstm_model.h5')
13
+ # self.encoder = pickle.load(open('binary_encoder.pkl', 'rb'))
14
+ # self.scaler = pickle.load(open('min_max_scaler.pkl', 'rb'))
15
+
16
+ # # Load initial data
17
+ # self.data = pd.read_csv('synthetic_crowd_data.csv')[-60:]
18
+ # self.prev_crowd_count = self.data['crowd_count'].iloc[-1]
19
+ # self.data['crowd_count'] = self.data['crowd_count'].shift(1)
20
+ # self.data['datetime'] = pd.to_datetime(self.data['datetime'])
21
+ # self.data.dropna(inplace=True)
22
+
23
+ # # Initialize current datetime
24
+ # self.curr_datetime = self.data['datetime'].iloc[-1] + pd.Timedelta(minutes=1)
25
+
26
+ # def update_data(self, input_df):
27
+ # # Update the internal data attribute
28
+ # self.data = pd.concat([self.data, input_df], ignore_index=True)
29
+ # self.data = self.data[-60:] # Keep only the last 60 records
30
+ # self.curr_datetime = self.data['datetime'].iloc[-1] + pd.Timedelta(minutes=1)
31
+
32
+ # def predict_single(self, camera_location):
33
+ # os.system('cls') # Clear console
34
+
35
+ # # Prepare the input row with updated datetime and previous crowd count
36
+ # datetime = self.curr_datetime
37
+ # input_df = pd.DataFrame({
38
+ # 'datetime': [datetime],
39
+ # 'camera_location': [camera_location],
40
+ # 'crowd_count': [self.prev_crowd_count]
41
+ # })
42
+
43
+ # # Update data with new input
44
+ # self.update_data(input_df)
45
+
46
+ # # Feature engineering and scaling
47
+ # features = create_features(self.data)
48
+ # df = cylindrical_encoding(features)
49
+ # df = self.encoder.transform(df)
50
+ # df[['dayofyear', 'dayofmonth', 'weekofyear']] = self.scaler.transform(df[['dayofyear', 'dayofmonth', 'weekofyear']])
51
+
52
+ # # Model prediction
53
+ # X = np.expand_dims(df, axis=0).astype('float32')
54
+ # prediction = self.model.predict(X)
55
+ # self.prev_crowd_count = int(prediction[0][0])
56
+
57
+ # return self.prev_crowd_count
58
+
59
+
60
+ # def predict_batch(self, batch_data):
61
+ # os.system('cls')
62
+
63
+ # df = pd.read_csv(batch_data.name)
64
+ # predictions = []
65
+
66
+ # for index, row in df.iterrows():
67
+ # camera_location_input = row['camera_location']
68
+ # prediction = self.predict_single(camera_location_input)
69
+ # predictions.append(prediction)
70
+
71
+ # return predictions
72
+
73
+
74
+ # # Instantiate the predictor
75
+ # predictor = CrowdPredictor()
76
+
77
+ # # Gradio interface setup
78
+ # with gr.Blocks() as prediction_block:
79
+ # gr.Label("Crowd Count Prediction")
80
+ # with gr.Tab("Single Prediction"):
81
+ # # camera_location_input = gr.Text(label="Camera Location (Category)")
82
+ # camera_location_input = gr.Dropdown(choices=[f"Camera_{i}" for i in range(1, 101)], label="Camera Location (Category)"),
83
+ # single_predict_btn = gr.Button("Predict")
84
+ # single_result = gr.Number(label="Predicted Crowd Count")
85
+
86
+ # single_predict_btn.click(
87
+ # predictor.predict_single,
88
+ # inputs=camera_location_input,
89
+ # outputs=single_result
90
+ # )
91
+
92
+ # with gr.Tab("Batch Prediction"):
93
+ # batch_input = gr.File(label="Upload CSV")
94
+ # batch_predict_btn = gr.Button("Predict")
95
+ # output = gr.Textbox(label="Predicted Crowd Counts")
96
+
97
+ # batch_predict_btn.click(
98
+ # predictor.predict_batch,
99
+ # inputs=batch_input,
100
+ # outputs=output
101
+ # )
102
+
103
+ # prediction_block.launch(share=True, debug=True)
104
+
105
+
106
  import pandas as pd
107
  import numpy as np
108
  import gradio as gr
 
160
  self.prev_crowd_count = int(prediction[0][0])
161
 
162
  return self.prev_crowd_count
 
163
 
164
  def predict_batch(self, batch_data):
165
  os.system('cls')
 
172
  prediction = self.predict_single(camera_location_input)
173
  predictions.append(prediction)
174
 
175
+ # Prepare DataFrame for LinePlot output
176
+ plot_data = self.data[['datetime', 'crowd_count']]
177
+
178
+ return ', '.join(map(str, predictions)), plot_data
179
+
180
+ def predict_nxt_m_minutes(self, camera_location, m):
181
+ os.system('cls')
182
+
183
+ predictions = []
184
+ for _ in range(int(m)): # Ensure m is an integer
185
+ prediction = self.predict_single(camera_location)
186
+ predictions.append(prediction)
187
 
188
+ # Prepare DataFrame for LinePlot output
189
+ plot_data = self.data[['datetime', 'crowd_count']]
190
+
191
+ return ', '.join(map(str, predictions)), plot_data
192
 
193
  # Instantiate the predictor
194
  predictor = CrowdPredictor()
 
196
  # Gradio interface setup
197
  with gr.Blocks() as prediction_block:
198
  gr.Label("Crowd Count Prediction")
199
+
200
+ # Single Prediction Tab
201
  with gr.Tab("Single Prediction"):
202
+ single_camera_location = gr.Dropdown(choices=[f"Camera_{i}" for i in range(1, 101)], label="Camera Location (Category)")
 
203
  single_predict_btn = gr.Button("Predict")
204
  single_result = gr.Number(label="Predicted Crowd Count")
205
+
206
  single_predict_btn.click(
207
  predictor.predict_single,
208
+ inputs=single_camera_location,
209
  outputs=single_result
210
  )
211
+
212
+ # Batch Prediction Tab
213
  with gr.Tab("Batch Prediction"):
214
  batch_input = gr.File(label="Upload CSV")
215
  batch_predict_btn = gr.Button("Predict")
216
+ batch_output = gr.Textbox(label="Predicted Crowd Counts")
217
+ batch_plot = gr.LinePlot(
218
+ predictor.data,
219
+ x="datetime",
220
+ y="crowd_count",
221
+ title="Crowd Count History",
222
+ x_title="Datetime",
223
+ y_title="Crowd Count",
224
+ label="Crowd Count History"
225
+ )
226
 
227
  batch_predict_btn.click(
228
  predictor.predict_batch,
229
  inputs=batch_input,
230
+ outputs=[batch_output, batch_plot]
231
+ )
232
+
233
+ # Predict Next M Minutes Tab
234
+ with gr.Tab("Predict next M minutes"):
235
+ m = gr.Number(label="Minutes to predict", minimum=1)
236
+ next_camera_location = gr.Dropdown(choices=[f"Camera_{i}" for i in range(1, 101)], label="Camera Location (Category)")
237
+ predict_next_btn = gr.Button("Predict")
238
+ next_output = gr.Textbox(label="Predicted Crowd Counts")
239
+ next_plot = gr.LinePlot(
240
+ predictor.data,
241
+ x="datetime",
242
+ y="crowd_count",
243
+ title="Crowd Count History",
244
+ x_title="Datetime",
245
+ y_title="Crowd Count",
246
+ label="Crowd Count History"
247
+ )
248
+
249
+ predict_next_btn.click(
250
+ predictor.predict_nxt_m_minutes,
251
+ inputs=[next_camera_location, m],
252
+ outputs=[next_output, next_plot]
253
  )
254
 
255
+ # Launch the Gradio app
256
  prediction_block.launch(share=True, debug=True)