Spaces:
Running
Running
Commit
·
c4c99cc
1
Parent(s):
af978d7
added relevent documents
Browse files- .gitignore +1 -0
- app.py +83 -2
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
app.py
CHANGED
@@ -1,4 +1,85 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
from langchain.agents import initialize_agent, AgentType
|
6 |
+
from langchain.tools import Tool
|
7 |
+
from langchain_huggingface import HuggingFacePipeline
|
8 |
+
import os
|
9 |
+
from dotenv import load_dotenv
|
10 |
|
11 |
+
load_dotenv()
|
12 |
+
NEWSAPI_KEY = os.getenv("NEWSAPI_KEY")
|
13 |
+
|
14 |
+
def fetch_stock_data(ticker):
|
15 |
+
stock = yf.Ticker(ticker)
|
16 |
+
hist = stock.history(period="1mo")
|
17 |
+
return hist.tail(5)
|
18 |
+
|
19 |
+
def fetch_stock_news(ticker, NEWSAPI_KEY):
|
20 |
+
api_url = f"https://newsapi.org/v2/everything?q={ticker}&apiKey={NEWSAPI_KEY}"
|
21 |
+
response = requests.get(api_url)
|
22 |
+
if response.status_code == 200:
|
23 |
+
articles = response.json().get('articles', [])
|
24 |
+
return [{"title": article['title'], "description": article['description']} for article in articles[:5]]
|
25 |
+
else:
|
26 |
+
return [{"error": "Unable to fetch news."}]
|
27 |
+
|
28 |
+
def calculate_moving_average(ticker, window=5):
|
29 |
+
stock = yf.Ticker(ticker)
|
30 |
+
hist = stock.history(period="1mo")
|
31 |
+
hist[f"{window}-day MA"] = hist["Close"].rolling(window=window).mean()
|
32 |
+
return hist[["Close", f"{window}-day MA"]].tail(5)
|
33 |
+
|
34 |
+
llm = HuggingFacePipeline.from_model_id(
|
35 |
+
model_id="microsoft/Phi-3-mini-4k-instruct",
|
36 |
+
task="text-generation",
|
37 |
+
pipeline_kwargs={
|
38 |
+
"max_new_tokens": 100,
|
39 |
+
"top_k": 50,
|
40 |
+
"temperature": 0.1,
|
41 |
+
},
|
42 |
+
)
|
43 |
+
|
44 |
+
stock_data_tool = Tool(
|
45 |
+
name="Stock Data Fetcher",
|
46 |
+
func=fetch_stock_data,
|
47 |
+
description="Fetch recent stock data for a given ticker."
|
48 |
+
)
|
49 |
+
|
50 |
+
stock_news_tool = Tool(
|
51 |
+
name="Stock News Fetcher",
|
52 |
+
func=lambda ticker: fetch_stock_news(ticker, NEWSAPI_KEY),
|
53 |
+
description="Fetch recent news articles about a stock ticker."
|
54 |
+
)
|
55 |
+
|
56 |
+
moving_average_tool = Tool(
|
57 |
+
name="Moving Average Calculator",
|
58 |
+
func=calculate_moving_average,
|
59 |
+
description="Calculate the moving average of a stock over a 5-day window."
|
60 |
+
)
|
61 |
+
|
62 |
+
tools = [stock_data_tool, stock_news_tool, moving_average_tool]
|
63 |
+
|
64 |
+
agent = initialize_agent(
|
65 |
+
tools=tools,
|
66 |
+
llm=llm,
|
67 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
68 |
+
verbose=True
|
69 |
+
)
|
70 |
+
|
71 |
+
st.title("Trading Helper Agent")
|
72 |
+
|
73 |
+
query = st.text_input("Enter your query:")
|
74 |
+
|
75 |
+
if st.button("Submit"):
|
76 |
+
if query:
|
77 |
+
with st.spinner("Processing..."):
|
78 |
+
try:
|
79 |
+
response = agent.run(query)
|
80 |
+
st.success("Response:")
|
81 |
+
st.write(response)
|
82 |
+
except Exception as e:
|
83 |
+
st.error(f"An error occurred: {e}")
|
84 |
+
else:
|
85 |
+
st.warning("Please enter a query.")
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
yfinance
|
3 |
+
requests
|
4 |
+
pandas
|
5 |
+
langchain
|
6 |
+
langchain_huggingface
|
7 |
+
python-dotenv
|