Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,54 @@
|
|
1 |
-
import yfinance as yf
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
try:
|
7 |
-
# Create a Ticker object
|
8 |
-
ticker = yf.Ticker(ticker_symbol)
|
9 |
-
|
10 |
-
# Fetch historical market data
|
11 |
-
historical_data = ticker.history(period="1y") # data for the last year
|
12 |
-
|
13 |
-
# Fetch basic financials
|
14 |
-
financials = ticker.financials
|
15 |
-
|
16 |
-
# Fetch stock actions like dividends and splits
|
17 |
-
actions = ticker.actions
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
historical_output = gr.Dataframe(label="Historical Data (1 Year)")
|
28 |
-
financials_output = gr.Dataframe(label="Financials")
|
29 |
-
actions_output = gr.Dataframe(label="Stock Actions")
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
inputs=ticker_input,
|
35 |
-
outputs=[historical_output, financials_output, actions_output],
|
36 |
-
live=False, # Disable live updates to avoid the interface from triggering automatically
|
37 |
-
title="Stock Data Fetcher",
|
38 |
-
description="Enter a ticker symbol and click 'Submit' to fetch historical data, financials, and stock actions."
|
39 |
-
)
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain_groq import ChatGroq
|
3 |
+
import yfinance as yf
|
4 |
|
5 |
+
# Initialize the ChatGroq model
|
6 |
+
llm = ChatGroq(model_name="Llama3-8b-8192", api_key=groq_api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def fetch_stock_data(company_name):
|
9 |
+
# Fetch stock data using Yahoo Finance
|
10 |
+
ticker = yf.Ticker(company_name)
|
11 |
+
stock_info = ticker.info
|
12 |
+
|
13 |
+
# Extract relevant information
|
14 |
+
stock_data = {
|
15 |
+
"Company": stock_info.get("longName", "N/A"),
|
16 |
+
"Current Price": stock_info.get("currentPrice", "N/A"),
|
17 |
+
"Market Cap": stock_info.get("marketCap", "N/A"),
|
18 |
+
"PE Ratio": stock_info.get("trailingPE", "N/A"),
|
19 |
+
"Dividend Yield": stock_info.get("dividendYield", "N/A"),
|
20 |
+
"52 Week High": stock_info.get("fiftyTwoWeekHigh", "N/A"),
|
21 |
+
"52 Week Low": stock_info.get("fiftyTwoWeekLow", "N/A"),
|
22 |
+
"Sector": stock_info.get("sector", "N/A"),
|
23 |
+
"Industry": stock_info.get("industry", "N/A")
|
24 |
+
}
|
25 |
+
|
26 |
+
return stock_data
|
27 |
|
28 |
+
def respond_to_query(query):
|
29 |
+
# Use the LLM to process the query and fetch relevant data
|
30 |
+
response = llm.invoke(query)
|
31 |
+
return response
|
|
|
|
|
|
|
32 |
|
33 |
+
def main(company_name):
|
34 |
+
# Fetch stock data and generate a response
|
35 |
+
stock_data = fetch_stock_data(company_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Prepare a response string
|
38 |
+
response = f"Here is the data for {company_name}:\n"
|
39 |
+
for key, value in stock_data.items():
|
40 |
+
response += f"{key}: {value}\n"
|
41 |
+
|
42 |
+
return response
|
43 |
+
|
44 |
+
# Create Gradio interface
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=main,
|
47 |
+
inputs="text",
|
48 |
+
outputs="text",
|
49 |
+
title="Stock Price Data Fetcher",
|
50 |
+
description="Enter the company name or ticker symbol to get the latest stock price data."
|
51 |
+
)
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
iface.launch()
|