Dhahlan2000 commited on
Commit
2def788
·
1 Parent(s): 986b5c3

added improvements

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -22,15 +22,18 @@ model = AutoModelForCausalLM.from_pretrained(
22
  )
23
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
24
 
 
 
 
 
25
 
26
  def fetch_stock_data(ticker):
27
- print("fetching stock data")
28
  stock = yf.Ticker(ticker)
29
  hist = stock.history(period="1mo")
30
  return hist.tail(5)
31
 
32
  def fetch_stock_news(ticker, NEWSAPI_KEY):
33
- print("fetching stock news")
34
  api_url = f"https://newsapi.org/v2/everything?q={ticker}&apiKey={NEWSAPI_KEY}"
35
  response = requests.get(api_url)
36
  if response.status_code == 200:
@@ -40,7 +43,6 @@ def fetch_stock_news(ticker, NEWSAPI_KEY):
40
  return [{"error": "Unable to fetch news."}]
41
 
42
  def calculate_moving_average(ticker, window=5):
43
- print("calculating moving average")
44
  stock = yf.Ticker(ticker)
45
  hist = stock.history(period="1mo")
46
  hist[f"{window}-day MA"] = hist["Close"].rolling(window=window).mean()
@@ -51,7 +53,7 @@ llm = HuggingFacePipeline(pipeline=pipe)
51
  stock_data_tool = Tool(
52
  name="Stock Data Fetcher",
53
  func=fetch_stock_data,
54
- description="Fetch recent stock data for a given ticker."
55
  )
56
 
57
  stock_news_tool = Tool(
@@ -73,7 +75,7 @@ agent = initialize_agent(
73
  llm=llm,
74
  agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
75
  verbose=True,
76
- handle_parsing_errors=True
77
  )
78
 
79
  print(fetch_stock_data("AAPL"))
@@ -87,9 +89,11 @@ query = st.text_input("Enter your query:")
87
 
88
  if st.button("Submit"):
89
  if query:
 
90
  with st.spinner("Processing..."):
91
  try:
92
  response = agent.run(query)
 
93
  st.success("Response:")
94
  st.write(response)
95
  except Exception as e:
 
22
  )
23
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
24
 
25
+ def validate_ticker(ticker):
26
+ # Ensure ticker is uppercase and length is reasonable (1-5 characters)
27
+ return ticker.strip().upper()
28
+
29
 
30
  def fetch_stock_data(ticker):
31
+ ticker = validate_ticker(ticker) # Validate and clean input
32
  stock = yf.Ticker(ticker)
33
  hist = stock.history(period="1mo")
34
  return hist.tail(5)
35
 
36
  def fetch_stock_news(ticker, NEWSAPI_KEY):
 
37
  api_url = f"https://newsapi.org/v2/everything?q={ticker}&apiKey={NEWSAPI_KEY}"
38
  response = requests.get(api_url)
39
  if response.status_code == 200:
 
43
  return [{"error": "Unable to fetch news."}]
44
 
45
  def calculate_moving_average(ticker, window=5):
 
46
  stock = yf.Ticker(ticker)
47
  hist = stock.history(period="1mo")
48
  hist[f"{window}-day MA"] = hist["Close"].rolling(window=window).mean()
 
53
  stock_data_tool = Tool(
54
  name="Stock Data Fetcher",
55
  func=fetch_stock_data,
56
+ description="Fetch recent stock data for a valid stock ticker (e.g., AAPL for Apple)."
57
  )
58
 
59
  stock_news_tool = Tool(
 
75
  llm=llm,
76
  agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
77
  verbose=True,
78
+ handle_parsing_errors=True # Enables automatic handling of parsing issues
79
  )
80
 
81
  print(fetch_stock_data("AAPL"))
 
89
 
90
  if st.button("Submit"):
91
  if query:
92
+ st.write("Debug: User Query ->", query)
93
  with st.spinner("Processing..."):
94
  try:
95
  response = agent.run(query)
96
+ st.write("Debug: Agent Response ->", response)
97
  st.success("Response:")
98
  st.write(response)
99
  except Exception as e: