Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yfinance as yf
|
2 |
+
import gradio as gr
|
3 |
+
import llama_index as ll
|
4 |
+
import chatgroq
|
5 |
+
|
6 |
+
# Initialize Llama model (assuming you have a pre-trained model from llama)
|
7 |
+
llama_model = ll.LlamaModel('your_llama_model_path') # Replace with your Llama model path
|
8 |
+
|
9 |
+
# Initialize Chatgroq (Configure accordingly based on your API or setup)
|
10 |
+
chatgroq_model = chatgroq.ChatGroqModel('your_chatgroq_model_id') # Replace with your Chatgroq ID
|
11 |
+
|
12 |
+
# Function to fetch stock data from Yahoo Finance
|
13 |
+
def get_stock_data(ticker):
|
14 |
+
stock = yf.Ticker(ticker)
|
15 |
+
stock_info = stock.info
|
16 |
+
stock_price = stock_info.get('currentPrice', 'N/A')
|
17 |
+
stock_name = stock_info.get('shortName', 'N/A')
|
18 |
+
stock_sector = stock_info.get('sector', 'N/A')
|
19 |
+
stock_pe_ratio = stock_info.get('trailingPE', 'N/A')
|
20 |
+
return {
|
21 |
+
'stock_name': stock_name,
|
22 |
+
'stock_price': stock_price,
|
23 |
+
'sector': stock_sector,
|
24 |
+
'pe_ratio': stock_pe_ratio
|
25 |
+
}
|
26 |
+
|
27 |
+
# Llama-based reasoning function to respond as a stock analyst
|
28 |
+
def generate_analysis(stock_data):
|
29 |
+
prompt = f"""
|
30 |
+
You are a stock market analyst.
|
31 |
+
Here are the details of the stock:
|
32 |
+
Name: {stock_data['stock_name']}
|
33 |
+
Price: {stock_data['stock_price']}
|
34 |
+
Sector: {stock_data['sector']}
|
35 |
+
P/E Ratio: {stock_data['pe_ratio']}
|
36 |
+
|
37 |
+
Analyze the stock performance and provide a brief summary and advice to the user.
|
38 |
+
"""
|
39 |
+
response = llama_model.generate_response(prompt)
|
40 |
+
return response
|
41 |
+
|
42 |
+
# Chatgroq-based reasoning to understand context and provide advice
|
43 |
+
def generate_advice_based_on_context(stock_data, user_query):
|
44 |
+
context = f"The stock price of {stock_data['stock_name']} is {stock_data['stock_price']}."
|
45 |
+
advice_prompt = f"User wants to know about the stock performance. {context} {user_query}"
|
46 |
+
advice = chatgroq_model.get_advice(advice_prompt)
|
47 |
+
return advice
|
48 |
+
|
49 |
+
# Gradio Interface function
|
50 |
+
def stock_analysis(ticker, user_query=""):
|
51 |
+
stock_data = get_stock_data(ticker)
|
52 |
+
analysis = generate_analysis(stock_data)
|
53 |
+
|
54 |
+
# Generate user-specific advice (if any query is provided)
|
55 |
+
if user_query:
|
56 |
+
advice = generate_advice_based_on_context(stock_data, user_query)
|
57 |
+
return stock_data, analysis, advice
|
58 |
+
|
59 |
+
return stock_data, analysis
|
60 |
+
|
61 |
+
# Create the Gradio interface
|
62 |
+
iface = gr.Interface(
|
63 |
+
fn=stock_analysis,
|
64 |
+
inputs=[
|
65 |
+
gr.Textbox(label="Stock Ticker", placeholder="Enter stock ticker (e.g., AAPL)"),
|
66 |
+
gr.Textbox(label="User Query", placeholder="Ask about the stock (optional)", optional=True)
|
67 |
+
],
|
68 |
+
outputs=[
|
69 |
+
gr.JSON(label="Stock Data"),
|
70 |
+
gr.Textbox(label="Analysis"),
|
71 |
+
gr.Textbox(label="Advice", optional=True)
|
72 |
+
],
|
73 |
+
live=True
|
74 |
+
)
|
75 |
+
|
76 |
+
# Run the Gradio app
|
77 |
+
if __name__ == '__main__':
|
78 |
+
iface.launch()
|