Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,17 @@
|
|
1 |
-
import yfinance as yf
|
2 |
import gradio as gr
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
The stock symbol is: {stock_symbol}.
|
17 |
-
"""
|
18 |
-
stock_prompt = PromptTemplate(input_variables=["stock_symbol"], template=prompt_template)
|
19 |
-
|
20 |
-
# Function to get stock data from Yahoo Finance
|
21 |
-
def get_stock_data(stock_symbol):
|
22 |
-
stock = yf.Ticker(stock_symbol)
|
23 |
-
stock_info = stock.info # Get company info
|
24 |
-
stock_price = stock_info['currentPrice'] # Get current stock price
|
25 |
-
company_name = stock_info.get('shortName', 'N/A')
|
26 |
-
sector = stock_info.get('sector', 'N/A')
|
27 |
-
market_cap = stock_info.get('marketCap', 'N/A')
|
28 |
-
|
29 |
-
# Generate a response using Langchain LLM
|
30 |
-
stock_details = llm_chain.run(stock_prompt.format(stock_symbol=stock_symbol))
|
31 |
-
|
32 |
-
return f"Company: {company_name}\nSector: {sector}\nMarket Cap: {market_cap}\nStock Price: ${stock_price}\n\nDetailed Information: {stock_details}"
|
33 |
-
|
34 |
-
# Gradio Interface
|
35 |
-
def stock_market_agent(stock_symbol):
|
36 |
-
return get_stock_data(stock_symbol)
|
37 |
-
|
38 |
-
# Set up Gradio UI
|
39 |
-
iface = gr.Interface(fn=stock_market_agent,
|
40 |
inputs=gr.Textbox(label="Enter Stock Symbol (e.g., AAPL)"),
|
41 |
outputs="text",
|
42 |
title="Stock Market Agent",
|
|
|
|
|
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 |
+
return f"Company: {company_info['Name']}\nSector: {company_info['Sector']}\nMarket Cap: {company_info['Market Cap']}\nStock Price: ${price:.2f}"
|
11 |
+
except Exception as e:
|
12 |
+
return f"Error retrieving data: {e}"
|
13 |
+
|
14 |
+
iface = gr.Interface(fn=get_stock_info,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
inputs=gr.Textbox(label="Enter Stock Symbol (e.g., AAPL)"),
|
16 |
outputs="text",
|
17 |
title="Stock Market Agent",
|