Manasa1 commited on
Commit
76ba4f4
·
verified ·
1 Parent(s): a8a3333

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -1,50 +1,60 @@
 
1
  import yfinance as yf
2
  import pandas as pd
3
- import gradio as gr
4
- import os
5
 
6
- # Company dictionary mapping names to tickers
7
- companies = {
8
  "Apple": "AAPL",
9
- "Microsoft": "MSFT",
10
  "Google": "GOOGL",
 
11
  "Amazon": "AMZN",
12
  "Tesla": "TSLA"
13
  }
14
 
15
- def fetch_esg_data(company_name):
16
- ticker = companies[company_name]
17
- try:
18
- stock = yf.Ticker(ticker)
19
- esg_data = stock.sustainability
20
- if esg_data is not None:
21
- csv_file = f"{ticker}_esg_data.csv"
22
- esg_data.to_csv(csv_file)
23
- return f"ESG data for {company_name} saved to {csv_file}", csv_file
24
- else:
25
- return f"No ESG data available for {company_name}", None
26
- except Exception as e:
27
- return str(e), None
28
-
29
- def main():
30
- company_names = list(companies.keys())
31
-
32
- def wrapper(company_name):
33
- msg, csv_file = fetch_esg_data(company_name)
34
- if csv_file and os.path.exists(csv_file):
35
- return msg, gr.File(csv_file)
36
- return msg, None
37
-
38
- interface = gr.Interface(
39
- fn=wrapper,
40
- inputs=gr.inputs.Dropdown(choices=company_names, label="Select a company"),
41
- outputs=["text", "file"],
42
- title="Fetch ESG Data",
43
- description="Select a company to fetch its ESG data and save it to a CSV file."
44
- )
45
-
46
- interface.launch()
47
-
48
- if __name__ == "__main__":
49
- main()
 
 
 
 
 
 
 
 
 
 
 
50
 
 
1
+ import gradio as gr
2
  import yfinance as yf
3
  import pandas as pd
 
 
4
 
5
+ # Mapping company names to their ticker symbols
6
+ company_dict = {
7
  "Apple": "AAPL",
 
8
  "Google": "GOOGL",
9
+ "Microsoft": "MSFT",
10
  "Amazon": "AMZN",
11
  "Tesla": "TSLA"
12
  }
13
 
14
+ # Function to fetch ESG data and save it to a CSV file
15
+ def fetch_and_save_esg_data(company_name):
16
+ # Get the ticker symbol from the company name
17
+ ticker = company_dict[company_name]
18
+
19
+ # Fetch ESG data from Yahoo Finance
20
+ stock = yf.Ticker(ticker)
21
+ esg_data = stock.sustainability
22
+
23
+ # If the data is available, save it to a CSV file
24
+ if esg_data is not None:
25
+ # Convert the ESG data to a pandas DataFrame
26
+ esg_df = pd.DataFrame(esg_data)
27
+
28
+ # Save it to a CSV file
29
+ filename = f"{ticker}_esg_data.csv"
30
+ esg_df.to_csv(filename)
31
+
32
+ return f"ESG data for {company_name} saved to {filename}", filename
33
+ else:
34
+ return f"No ESG data available for {company_name}", None
35
+
36
+ # Gradio interface with a dropdown for company selection
37
+ def app_interface():
38
+ with gr.Blocks() as app:
39
+ # Dropdown to select company name
40
+ company = gr.Dropdown(label="Select Company", choices=list(company_dict.keys()), value="Apple")
41
+
42
+ # Button to trigger ESG data extraction
43
+ fetch_button = gr.Button("Fetch ESG Data")
44
+
45
+ # Output text and download link
46
+ output_text = gr.Textbox(label="Output")
47
+ download_link = gr.File(label="Download CSV")
48
+
49
+ # Define the action when the button is clicked
50
+ fetch_button.click(fn=fetch_and_save_esg_data,
51
+ inputs=company,
52
+ outputs=[output_text, download_link])
53
+
54
+ return app
55
+
56
+ # Launch the Gradio app
57
+ app = app_interface()
58
+ app.launch()
59
+
60