anujkum0x commited on
Commit
7783537
·
verified ·
1 Parent(s): 9760855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -32
app.py CHANGED
@@ -163,7 +163,6 @@ def full_forecast_pipeline(file_obj, time_col, target_col, forecast_horizon, fin
163
  """
164
  try:
165
  data = load_data(file_obj)
166
- print("Column Names:", data.columns)
167
  if not isinstance(data, pd.DataFrame):
168
  return "Error loading data. Please check the file format and content.", None, None, None
169
 
@@ -171,10 +170,7 @@ def full_forecast_pipeline(file_obj, time_col, target_col, forecast_horizon, fin
171
  return "Error: Timestamp column or Value column not found in the data.", None, None, None
172
 
173
  # Convert time column to datetime
174
- try:
175
- data[time_col] = pd.to_datetime(data[time_col], format='%Y-%m')
176
- except ValueError as e:
177
- return f"Error converting time column: {e}", None, None, None
178
 
179
  # Sort the DataFrame by the time column
180
  data = data.sort_values(by=time_col)
@@ -196,10 +192,7 @@ def full_forecast_pipeline(file_obj, time_col, target_col, forecast_horizon, fin
196
  data = data.set_index(time_col)
197
 
198
  # Resample the data
199
- try:
200
- data = data.resample("M").mean()
201
- except ValueError as e:
202
- return f"Error resampling data: {e}", None, None, None
203
  data.reset_index(inplace=True)
204
 
205
  forecast_result = forecast_nixtla(data, forecast_horizon, finetune_steps, freq, time_col, target_col)
@@ -241,17 +234,6 @@ def get_column_names(file_obj):
241
  print(f"Error in get_column_names: {e}")
242
  return []
243
 
244
- def update_dropdown_choices(file_obj):
245
- """
246
- Updates the dropdown choices based on the uploaded file.
247
- """
248
- try:
249
- columns = get_column_names(file_obj)
250
- return gr.Dropdown.update(choices=columns), gr.Dropdown.update(choices=columns)
251
- except Exception as e:
252
- logger.error(f"Error updating dropdown choices: {e}", exc_info=True)
253
- return gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])
254
-
255
  def create_interface():
256
  with gr.Blocks() as iface:
257
  gr.Markdown("""
@@ -264,18 +246,14 @@ def create_interface():
264
  time_col_dropdown = gr.Dropdown(choices=[], label="Select Timestamp Column")
265
  target_col_dropdown = gr.Dropdown(choices=[], label="Select Value Column")
266
 
267
- # Remove the file_input.upload event
268
- # file_input.upload(
269
- # update_dropdown_choices,
270
- # [file_input],
271
- # [time_col_dropdown, target_col_dropdown]
272
- # )
273
 
274
- update_dropdown_button = gr.Button("Update Dropdowns")
275
- update_dropdown_button.click(
276
  update_dropdown_choices,
277
- inputs=[file_input],
278
- outputs=[time_col_dropdown, target_col_dropdown]
279
  )
280
 
281
  with gr.Row():
@@ -299,11 +277,11 @@ def create_interface():
299
  # Button to trigger the full pipeline
300
  btn = gr.Button("Generate Forecast")
301
  btn.click(
302
- fn=lambda *args: (update_dropdown_choices(file_input.value), full_forecast_pipeline(*args)), # Call update_dropdown_choices first
303
  inputs=[file_input, time_col_dropdown, target_col_dropdown, forecast_horizon_input, finetune_steps_input, freq_dropdown, start_date_input, end_date_input, start_time_input, end_time_input, resample_freq_dropdown],
304
  outputs=[output_html, output_plot, download_button, error_output]
305
  )
306
  return iface
307
 
308
  iface = create_interface()
309
- iface.launch()
 
163
  """
164
  try:
165
  data = load_data(file_obj)
 
166
  if not isinstance(data, pd.DataFrame):
167
  return "Error loading data. Please check the file format and content.", None, None, None
168
 
 
170
  return "Error: Timestamp column or Value column not found in the data.", None, None, None
171
 
172
  # Convert time column to datetime
173
+ data[time_col] = pd.to_datetime(data[time_col])
 
 
 
174
 
175
  # Sort the DataFrame by the time column
176
  data = data.sort_values(by=time_col)
 
192
  data = data.set_index(time_col)
193
 
194
  # Resample the data
195
+ data = data.resample(resample_freq).mean()
 
 
 
196
  data.reset_index(inplace=True)
197
 
198
  forecast_result = forecast_nixtla(data, forecast_horizon, finetune_steps, freq, time_col, target_col)
 
234
  print(f"Error in get_column_names: {e}")
235
  return []
236
 
 
 
 
 
 
 
 
 
 
 
 
237
  def create_interface():
238
  with gr.Blocks() as iface:
239
  gr.Markdown("""
 
246
  time_col_dropdown = gr.Dropdown(choices=[], label="Select Timestamp Column")
247
  target_col_dropdown = gr.Dropdown(choices=[], label="Select Value Column")
248
 
249
+ def update_dropdown_choices(file_obj):
250
+ columns = get_column_names(file_obj)
251
+ return gr.update(choices=columns), gr.update(choices=columns)
 
 
 
252
 
253
+ file_input.upload(
 
254
  update_dropdown_choices,
255
+ [file_input],
256
+ [time_col_dropdown, target_col_dropdown]
257
  )
258
 
259
  with gr.Row():
 
277
  # Button to trigger the full pipeline
278
  btn = gr.Button("Generate Forecast")
279
  btn.click(
280
+ fn=full_forecast_pipeline,
281
  inputs=[file_input, time_col_dropdown, target_col_dropdown, forecast_horizon_input, finetune_steps_input, freq_dropdown, start_date_input, end_date_input, start_time_input, end_time_input, resample_freq_dropdown],
282
  outputs=[output_html, output_plot, download_button, error_output]
283
  )
284
  return iface
285
 
286
  iface = create_interface()
287
+ iface.launch()