Nonin commited on
Commit
eacc839
·
verified ·
1 Parent(s): 9f39c86

Update app.py

Browse files

Add basic sentiment analysis tool

Files changed (1) hide show
  1. app.py +52 -1
app.py CHANGED
@@ -33,6 +33,57 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
@@ -55,7 +106,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
+ from smolagent.tools.duckduckgo_search import DuckDuckGoSearchTool
37
+ from datetime import datetime
38
+ import re
39
+
40
+ def sentiment_analysis(text: str) -> str:
41
+ """Basic economic sentiment analysis based on keywords"""
42
+ positive_keywords = ["growth", "profit", "increase", "investment", "record revenue", "expansion", "gain"]
43
+ negative_keywords = ["loss", "decline", "layoff", "decrease", "cut", "lawsuit", "scandal", "drop"]
44
+
45
+ text_lower = text.lower()
46
+ pos_hits = sum(1 for kw in positive_keywords if kw in text_lower)
47
+ neg_hits = sum(1 for kw in negative_keywords if kw in text_lower)
48
+
49
+ if pos_hits > neg_hits:
50
+ return "positive"
51
+ elif neg_hits > pos_hits:
52
+ return "negative"
53
+ else:
54
+ return "neutral"
55
+
56
+ def news_date_is_today(text: str) -> bool:
57
+ """Check if the news mention today's date (very naive)"""
58
+ today = datetime.now().strftime("%B %d, %Y") # e.g., "April 30, 2025"
59
+ return today.lower() in text.lower()
60
+
61
+ @tool
62
+ def company_news_sentiment(company_name: str, top_k: int) -> str:
63
+ """A tool that searches for the latest company news and assesses daily relevance and sentiment.
64
+
65
+ Args:
66
+ company_name: the name of the company to search news for.
67
+ top_k: the number of top search results to analyze.
68
+
69
+ Returns:
70
+ A string summarizing if there are daily news and whether the sentiment is positive or negative.
71
+ """
72
+ search_tool = DuckDuckGoSearchTool()
73
+ results = search_tool.run(f"{company_name} latest news")
74
+
75
+ if not results:
76
+ return f"No news found for {company_name}."
77
+
78
+ selected_results = results[:top_k]
79
+ combined_text = " ".join(res["body"] for res in selected_results if "body" in res)
80
+
81
+ has_today_news = any(news_date_is_today(res.get("body", "")) for res in selected_results)
82
+ sentiment = sentiment_analysis(combined_text)
83
+
84
+ daily_news_str = "There is news today" if has_today_news else "There is no news specifically from today"
85
+ return f"{daily_news_str} about {company_name}. Overall sentiment appears {sentiment} from an economic perspective."
86
+
87
 
88
  final_answer = FinalAnswerTool()
89
 
 
106
 
107
  agent = CodeAgent(
108
  model=model,
109
+ tools=[final_answer,company_news_sentiment], ## add your tools here (don't remove final answer)
110
  max_steps=6,
111
  verbosity_level=1,
112
  grammar=None,