Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,24 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
# Initialize
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Function to fetch stock data from Yahoo Finance
|
8 |
def get_stock_data(ticker):
|
@@ -19,41 +35,38 @@ def get_stock_data(ticker):
|
|
19 |
'pe_ratio': stock_pe_ratio
|
20 |
}
|
21 |
|
22 |
-
#
|
23 |
-
def
|
24 |
-
|
25 |
-
|
26 |
-
Here are the details of the stock:
|
27 |
-
Name: {stock_data['stock_name']}
|
28 |
-
Price: {stock_data['stock_price']}
|
29 |
-
Sector: {stock_data['sector']}
|
30 |
-
P/E Ratio: {stock_data['pe_ratio']}
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
response = llama_model.generate_response(prompt)
|
35 |
-
return response
|
36 |
|
37 |
-
# Chatgroq
|
38 |
-
|
39 |
-
context = f"The stock price of {stock_data['stock_name']} is {stock_data['stock_price']}."
|
40 |
-
advice_prompt = f"User wants to know about the stock performance. {context} {user_query}"
|
41 |
-
advice = chatgroq_model.get_advice(advice_prompt)
|
42 |
-
return advice
|
43 |
|
44 |
-
#
|
45 |
-
def stock_analysis(ticker, user_query=""):
|
46 |
stock_data = get_stock_data(ticker)
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
advice = generate_advice_based_on_context(stock_data, user_query)
|
52 |
-
return stock_data, analysis, advice
|
53 |
|
|
|
|
|
|
|
54 |
return stock_data, analysis
|
55 |
|
56 |
-
#
|
57 |
iface = gr.Interface(
|
58 |
fn=stock_analysis,
|
59 |
inputs=[
|
@@ -63,11 +76,8 @@ iface = gr.Interface(
|
|
63 |
outputs=[
|
64 |
gr.JSON(label="Stock Data"),
|
65 |
gr.Textbox(label="Analysis"),
|
66 |
-
|
67 |
-
],
|
68 |
-
live=True
|
69 |
)
|
70 |
|
71 |
-
# Run the Gradio app
|
72 |
if __name__ == '__main__':
|
73 |
iface.launch()
|
|
|
1 |
+
import yfinance as yf
|
2 |
+
import gradio as gr
|
3 |
+
import chatgroq # Import Chatgroq API client
|
4 |
+
from langchain.chains import LLMChain
|
5 |
+
from langchain.memory import ConversationBufferMemory
|
6 |
+
from langchain.prompts import PromptTemplate
|
7 |
|
8 |
+
# Initialize memory for conversation
|
9 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
10 |
+
|
11 |
+
# Define the prompt template for stock analysis
|
12 |
+
prompt = """
|
13 |
+
You are a stock market analyst. The user is asking for stock analysis.
|
14 |
+
Here are the details of the stock:
|
15 |
+
- Name: {stock_name}
|
16 |
+
- Price: {stock_price}
|
17 |
+
- Sector: {sector}
|
18 |
+
- P/E Ratio: {pe_ratio}
|
19 |
+
|
20 |
+
Analyze the stock performance and provide a brief summary and advice to the user.
|
21 |
+
"""
|
22 |
|
23 |
# Function to fetch stock data from Yahoo Finance
|
24 |
def get_stock_data(ticker):
|
|
|
35 |
'pe_ratio': stock_pe_ratio
|
36 |
}
|
37 |
|
38 |
+
# Function to generate stock analysis based on the conversation context
|
39 |
+
def stock_analysis(ticker, user_query=""):
|
40 |
+
# Get API key from environment (no need to pass manually)
|
41 |
+
api_key = os.getenv("CHATGROQ_API_KEY") # Ensure Hugging Face secret is set
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
if not api_key:
|
44 |
+
return "API key is required for Chatgroq."
|
|
|
|
|
45 |
|
46 |
+
# Initialize Chatgroq model with the API key
|
47 |
+
chatgroq_model = chatgroq.ChatGroqModel(api_key)
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# Set the stock data in the prompt template
|
|
|
50 |
stock_data = get_stock_data(ticker)
|
51 |
+
prompt_input = {
|
52 |
+
'stock_name': stock_data['stock_name'],
|
53 |
+
'stock_price': stock_data['stock_price'],
|
54 |
+
'sector': stock_data['sector'],
|
55 |
+
'pe_ratio': stock_data['pe_ratio']
|
56 |
+
}
|
57 |
+
|
58 |
+
# Create LLM chain with Chatgroq model and prompt
|
59 |
+
chain = LLMChain(llm=chatgroq_model, prompt=PromptTemplate.from_template(prompt), memory=memory)
|
60 |
|
61 |
+
# Get analysis from Chatgroq-based agent
|
62 |
+
analysis = chain.run(input=prompt_input)
|
|
|
|
|
63 |
|
64 |
+
# Generate advice if there is a user query
|
65 |
+
if user_query:
|
66 |
+
return stock_data, analysis + "\n" + "Advice: " + user_query
|
67 |
return stock_data, analysis
|
68 |
|
69 |
+
# Gradio interface
|
70 |
iface = gr.Interface(
|
71 |
fn=stock_analysis,
|
72 |
inputs=[
|
|
|
76 |
outputs=[
|
77 |
gr.JSON(label="Stock Data"),
|
78 |
gr.Textbox(label="Analysis"),
|
79 |
+
]
|
|
|
|
|
80 |
)
|
81 |
|
|
|
82 |
if __name__ == '__main__':
|
83 |
iface.launch()
|