Manasa1 commited on
Commit
0e731dc
·
verified ·
1 Parent(s): 6453c43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -13
app.py CHANGED
@@ -21,7 +21,7 @@ COMPANY_DICT = {
21
  "Netflix": "NFLX"
22
  }
23
 
24
- def fetch_esg_data(company_name: str) -> Tuple[pd.DataFrame, Optional[str], dict, dict, dict]:
25
  """
26
  Fetch and process ESG data for the selected company.
27
 
@@ -30,7 +30,6 @@ def fetch_esg_data(company_name: str) -> Tuple[pd.DataFrame, Optional[str], dict
30
 
31
  Returns:
32
  Tuple containing:
33
- - DataFrame with ESG scores
34
  - Path to saved CSV file
35
  - Three plotly figures for different visualizations
36
  """
@@ -44,7 +43,7 @@ def fetch_esg_data(company_name: str) -> Tuple[pd.DataFrame, Optional[str], dict
44
  esg_data = stock.sustainability
45
 
46
  if esg_data is None:
47
- raise ValueError(f"No ESG data available for {company_name}")
48
 
49
  # Process ESG data
50
  esg_df = pd.DataFrame(esg_data)
@@ -65,11 +64,11 @@ def fetch_esg_data(company_name: str) -> Tuple[pd.DataFrame, Optional[str], dict
65
  scatter_fig = create_scatter_plot(plot_df)
66
  bar_fig = create_bar_plot(plot_df)
67
 
68
- return plot_df, csv_filename, line_fig, scatter_fig, bar_fig
69
 
70
  except Exception as e:
71
  logger.error(f"Error fetching ESG data: {str(e)}")
72
- return pd.DataFrame(), None, {}, {}, {}
73
 
74
  def create_line_plot(df: pd.DataFrame) -> dict:
75
  """Create a line plot using plotly"""
@@ -151,22 +150,45 @@ def create_interface() -> gr.Blocks:
151
  with gr.Column():
152
  bar_plot = gr.Plot(label="ESG Score Comparison")
153
 
154
- # Error message display
155
- error_message = gr.Markdown(visible=False)
 
 
 
156
 
157
- def handle_error(error):
158
- return gr.Markdown.update(visible=True, value=f"⚠️ Error: {error}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
- # Connect the button click to the fetch function
161
  plot_button.click(
162
- fn=fetch_esg_data,
163
  inputs=company,
164
  outputs=[
165
- error_message,
166
  csv_output,
167
  line_plot,
168
  scatter_plot,
169
- bar_plot
 
170
  ],
171
  api_name="generate_esg_analysis"
172
  )
 
21
  "Netflix": "NFLX"
22
  }
23
 
24
+ def fetch_esg_data(company_name: str) -> Tuple[Optional[str], dict, dict, dict]:
25
  """
26
  Fetch and process ESG data for the selected company.
27
 
 
30
 
31
  Returns:
32
  Tuple containing:
 
33
  - Path to saved CSV file
34
  - Three plotly figures for different visualizations
35
  """
 
43
  esg_data = stock.sustainability
44
 
45
  if esg_data is None:
46
+ return None, None, None, None
47
 
48
  # Process ESG data
49
  esg_df = pd.DataFrame(esg_data)
 
64
  scatter_fig = create_scatter_plot(plot_df)
65
  bar_fig = create_bar_plot(plot_df)
66
 
67
+ return csv_filename, line_fig, scatter_fig, bar_fig
68
 
69
  except Exception as e:
70
  logger.error(f"Error fetching ESG data: {str(e)}")
71
+ return None, None, None, None
72
 
73
  def create_line_plot(df: pd.DataFrame) -> dict:
74
  """Create a line plot using plotly"""
 
150
  with gr.Column():
151
  bar_plot = gr.Plot(label="ESG Score Comparison")
152
 
153
+ status_message = gr.Textbox(
154
+ label="Status",
155
+ interactive=False,
156
+ visible=True
157
+ )
158
 
159
+ def process_esg_data(company_name):
160
+ try:
161
+ csv_file, line, scatter, bar = fetch_esg_data(company_name)
162
+
163
+ if csv_file is None:
164
+ return (
165
+ None,
166
+ None,
167
+ None,
168
+ None,
169
+ f"No ESG data available for {company_name}"
170
+ )
171
+
172
+ return (
173
+ csv_file,
174
+ line,
175
+ scatter,
176
+ bar,
177
+ f"Successfully generated ESG analysis for {company_name}"
178
+ )
179
+ except Exception as e:
180
+ return None, None, None, None, f"Error: {str(e)}"
181
 
182
+ # Connect the button click to the process function
183
  plot_button.click(
184
+ fn=process_esg_data,
185
  inputs=company,
186
  outputs=[
 
187
  csv_output,
188
  line_plot,
189
  scatter_plot,
190
+ bar_plot,
191
+ status_message
192
  ],
193
  api_name="generate_esg_analysis"
194
  )