gauri-sharan commited on
Commit
4c0801c
·
verified ·
1 Parent(s): 65df9b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -26
app.py CHANGED
@@ -1,27 +1,42 @@
 
1
  import gradio as gr
2
- from yahoo_fin import stock_info
3
-
4
- def get_stock_info(symbol):
5
- try:
6
- # Fetch real-time stock price
7
- price = stock_info.get_live_price(symbol)
8
- # Fetch company information
9
- company_info = stock_info.get_quote_table(symbol)
10
-
11
- # Handling 'None' values gracefully if they exist
12
- company_name = company_info.get('Name', 'N/A')
13
- sector = company_info.get('Sector', 'N/A')
14
- market_cap = company_info.get('Market Cap', 'N/A')
15
-
16
- return f"Company: {company_name}\nSector: {sector}\nMarket Cap: {market_cap}\nStock Price: ${price:.2f}"
17
- except Exception as e:
18
- return f"Error retrieving data: {e}"
19
-
20
- # Gradio Interface
21
- iface = gr.Interface(fn=get_stock_info,
22
- inputs=gr.Textbox(label="Enter Stock Symbol (e.g., AAPL)"),
23
- outputs="text",
24
- title="Stock Market Agent",
25
- description="Get real-time stock prices and company information from Yahoo Finance.")
26
-
27
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
  import gradio as gr
3
+
4
+ # Function to fetch and return data for a given ticker symbol
5
+ def fetch_stock_data(ticker_symbol):
6
+ # Create a Ticker object
7
+ ticker = yf.Ticker(ticker_symbol)
8
+
9
+ # Fetch historical market data
10
+ historical_data = ticker.history(period="1y") # data for the last year
11
+
12
+ # Fetch basic financials
13
+ financials = ticker.financials
14
+
15
+ # Fetch stock actions like dividends and splits
16
+ actions = ticker.actions
17
+
18
+ return historical_data, financials, actions
19
+
20
+ # Gradio interface
21
+ def create_gradio_interface():
22
+ # Define the input and output for Gradio
23
+ ticker_input = gr.inputs.Textbox(label="Enter Ticker Symbol")
24
+ historical_output = gr.outputs.Dataframe(label="Historical Data (1 Year)")
25
+ financials_output = gr.outputs.Dataframe(label="Financials")
26
+ actions_output = gr.outputs.Dataframe(label="Stock Actions")
27
+
28
+ # Define the Gradio interface
29
+ interface = gr.Interface(
30
+ fn=fetch_stock_data,
31
+ inputs=ticker_input,
32
+ outputs=[historical_output, financials_output, actions_output],
33
+ live=True,
34
+ title="Stock Data Fetcher",
35
+ description="Enter a ticker symbol to fetch historical data, financials, and stock actions."
36
+ )
37
+
38
+ # Launch the interface
39
+ interface.launch()
40
+
41
+ # Run the Gradio interface
42
+ create_gradio_interface()