Kaushik Rajan commited on
Commit
13aa6c9
·
1 Parent(s): 657efc9

Fix plotting functionality: handle empty data properly, remove GPU decorator, add error handling

Browse files
Files changed (1) hide show
  1. app.py +42 -22
app.py CHANGED
@@ -9,7 +9,6 @@ import gradio as gr
9
  import numpy as np
10
  import pandas as pd
11
  import plotly.express as px
12
- import spaces
13
 
14
  # --- Game Configuration ---
15
  INITIAL_BUDGET = 1000
@@ -347,28 +346,49 @@ def create_interface():
347
  analysis_box = gr.Textbox("", label="Strategy Insights", lines=5, interactive=False)
348
 
349
  def create_plots(history):
350
- df = pd.DataFrame(history)
351
- if df.empty:
352
- return None, None, None
353
-
354
- fig_ms = px.line(df, x="Quarter", y=["Player Market Share", "AI Market Share"],
355
- title="Market Share (%)", markers=True,
356
- color_discrete_map={"Player Market Share": "#3b82f6", "AI Market Share": "#ef4444"})
357
- fig_ms.update_layout(yaxis_range=[0,100], legend_title_text='')
358
-
359
- fig_b = px.line(df, x="Quarter", y=["Player Budget", "AI Budget"],
360
- title="Budget ($)", markers=True,
361
- color_discrete_map={"Player Budget": "#3b82f6", "AI Budget": "#ef4444"})
362
- fig_b.update_layout(legend_title_text='')
363
-
364
- fig_q = px.line(df, x="Quarter", y=["Player Product Quality", "AI Product Quality"],
365
- title="Product Quality Index", markers=True,
366
- color_discrete_map={"Player Product Quality": "#3b82f6", "AI Product Quality": "#ef4444"})
367
- fig_q.update_layout(legend_title_text='')
368
-
369
- return fig_ms, fig_b, fig_q
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370
 
371
- @spaces.GPU
372
  def game_step_and_update(env, mode, rd_raw, mkt_raw, sales_raw, rd_pct, mkt_pct, sales_pct):
373
  player_budget = env.player_stats["budget"]
374
 
 
9
  import numpy as np
10
  import pandas as pd
11
  import plotly.express as px
 
12
 
13
  # --- Game Configuration ---
14
  INITIAL_BUDGET = 1000
 
346
  analysis_box = gr.Textbox("", label="Strategy Insights", lines=5, interactive=False)
347
 
348
  def create_plots(history):
349
+ try:
350
+ df = pd.DataFrame(history)
351
+
352
+ # Create empty figures if no data
353
+ if df.empty:
354
+ # Create empty plot with proper structure
355
+ empty_fig_ms = px.line(title="Market Share (%)")
356
+ empty_fig_ms.update_layout(yaxis_range=[0,100], legend_title_text='',
357
+ xaxis_title="Quarter", yaxis_title="Market Share (%)")
358
+
359
+ empty_fig_b = px.line(title="Budget ($)")
360
+ empty_fig_b.update_layout(legend_title_text='',
361
+ xaxis_title="Quarter", yaxis_title="Budget ($)")
362
+
363
+ empty_fig_q = px.line(title="Product Quality Index")
364
+ empty_fig_q.update_layout(legend_title_text='',
365
+ xaxis_title="Quarter", yaxis_title="Quality Index")
366
+
367
+ return empty_fig_ms, empty_fig_b, empty_fig_q
368
+
369
+ fig_ms = px.line(df, x="Quarter", y=["Player Market Share", "AI Market Share"],
370
+ title="Market Share (%)", markers=True,
371
+ color_discrete_map={"Player Market Share": "#3b82f6", "AI Market Share": "#ef4444"})
372
+ fig_ms.update_layout(yaxis_range=[0,100], legend_title_text='')
373
+
374
+ fig_b = px.line(df, x="Quarter", y=["Player Budget", "AI Budget"],
375
+ title="Budget ($)", markers=True,
376
+ color_discrete_map={"Player Budget": "#3b82f6", "AI Budget": "#ef4444"})
377
+ fig_b.update_layout(legend_title_text='')
378
+
379
+ fig_q = px.line(df, x="Quarter", y=["Player Product Quality", "AI Product Quality"],
380
+ title="Product Quality Index", markers=True,
381
+ color_discrete_map={"Player Product Quality": "#3b82f6", "AI Product Quality": "#ef4444"})
382
+ fig_q.update_layout(legend_title_text='')
383
+
384
+ return fig_ms, fig_b, fig_q
385
+
386
+ except Exception as e:
387
+ print(f"Error creating plots: {e}")
388
+ # Return empty figures as fallback
389
+ empty_fig = px.line(title="Error loading chart")
390
+ return empty_fig, empty_fig, empty_fig
391
 
 
392
  def game_step_and_update(env, mode, rd_raw, mkt_raw, sales_raw, rd_pct, mkt_pct, sales_pct):
393
  player_budget = env.player_stats["budget"]
394