gauri-sharan commited on
Commit
3f7527c
·
verified ·
1 Parent(s): 3735aa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -39
app.py CHANGED
@@ -1,45 +1,54 @@
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
- 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
- return historical_data, financials, actions
20
- except Exception as e:
21
- return f"Error: {e}", None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Gradio interface
24
- def create_gradio_interface():
25
- # Define the input and output for Gradio
26
- ticker_input = gr.Textbox(label="Enter Ticker Symbol")
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
- # Define the Gradio interface with explicit submit button
32
- interface = gr.Interface(
33
- fn=fetch_stock_data,
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
- # Launch the interface
42
- interface.launch()
43
-
44
- # Run the Gradio interface
45
- create_gradio_interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()