dibend commited on
Commit
82d320b
1 Parent(s): 287b5d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -18
app.py CHANGED
@@ -61,7 +61,7 @@ def create_3d_correlation_matrix(df):
61
  xaxis=dict(title='Variables'),
62
  yaxis=dict(title='Variables'),
63
  zaxis=dict(title='Correlation')))
64
- return fig.to_html(full_html=False) # Return the HTML content of the plot
65
 
66
  # Gradio function
67
  def visualize_correlation(selected_series):
@@ -69,29 +69,40 @@ def visualize_correlation(selected_series):
69
  series_ids = [series for series in series_options if series_options[series] in selected_series]
70
 
71
  if not series_ids:
72
- return "<p style='color:red;'>Please select at least one indicator.</p>"
73
 
74
  # Fetch and process data
75
  df = fetch_fred_data(series_ids)
76
  if df.empty:
77
- return "<p style='color:red;'>Failed to fetch data for the selected indicators.</p>"
78
 
79
  standardized_df = standardize_data(df)
80
- plot_html = create_3d_correlation_matrix(standardized_df)
81
- return f"<div>{plot_html}</div>"
82
 
83
- # Gradio Interface
84
- interface = gr.Interface(
85
- fn=visualize_correlation,
86
- inputs=gr.CheckboxGroup(
87
- label="Select Economic Indicators",
88
- choices=list(series_options.values()), # Displayed names
89
- value=[],
90
- info="Choose one or more indicators to include in the correlation matrix.",
91
- ),
92
- outputs=gr.HTML(label="3D Correlation Matrix"),
93
- title="3D Correlation Matrix Visualization with FRED Data"
94
- )
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  # Launch the app
97
- interface.launch(debug=True)
 
61
  xaxis=dict(title='Variables'),
62
  yaxis=dict(title='Variables'),
63
  zaxis=dict(title='Correlation')))
64
+ return fig
65
 
66
  # Gradio function
67
  def visualize_correlation(selected_series):
 
69
  series_ids = [series for series in series_options if series_options[series] in selected_series]
70
 
71
  if not series_ids:
72
+ return None, "Please select at least one indicator."
73
 
74
  # Fetch and process data
75
  df = fetch_fred_data(series_ids)
76
  if df.empty:
77
+ return None, "Failed to fetch data for the selected indicators."
78
 
79
  standardized_df = standardize_data(df)
80
+ plot = create_3d_correlation_matrix(standardized_df)
81
+ return plot, None
82
 
83
+ # Gradio Blocks Interface
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("# 3D Correlation Matrix Visualization with FRED Data")
86
+
87
+ with gr.Row():
88
+ with gr.Column():
89
+ series_selector = gr.CheckboxGroup(
90
+ choices=list(series_options.values()),
91
+ label="Select Economic Indicators",
92
+ info="Choose one or more indicators to include in the correlation matrix.",
93
+ )
94
+ submit_button = gr.Button("Generate Matrix")
95
+
96
+ with gr.Column():
97
+ plot_output = gr.Plot(label="3D Correlation Matrix")
98
+ error_message = gr.Markdown("", visible=False)
99
+
100
+ # Event handler
101
+ submit_button.click(
102
+ fn=visualize_correlation,
103
+ inputs=[series_selector],
104
+ outputs=[plot_output, error_message],
105
+ )
106
 
107
  # Launch the app
108
+ demo.launch(debug=True)