Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdd basic sentiment analysis tool
32 unmodified lines33343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888916 unmodified lines1061071085810911011111232 unmodified lines except Exception as e: return f"Error fetching time for timezone '{timezone}': {str(e)}"
from smolagent.tools.duckduckgo_search import DuckDuckGoSearchToolfrom datetime import datetimeimport re
def sentiment_analysis(text: str) -> str: """Basic economic sentiment analysis based on keywords""" positive_keywords = ["growth", "profit", "increase", "investment", "record revenue", "expansion", "gain"] negative_keywords = ["loss", "decline", "layoff", "decrease", "cut", "lawsuit", "scandal", "drop"]
text_lower = text.lower() pos_hits = sum(1 for kw in positive_keywords if kw in text_lower) neg_hits = sum(1 for kw in negative_keywords if kw in text_lower)
if pos_hits > neg_hits: return "positive" elif neg_hits > pos_hits: return "negative" else: return "neutral"
def news_date_is_today(text: str) -> bool: """Check if the news mention today's date (very naive)""" today = datetime.now().strftime("%B %d, %Y") # e.g., "April 30, 2025" return today.lower() in text.lower()
@tooldef company_news_sentiment(company_name: str, top_k: int) -> str: """A tool that searches for the latest company news and assesses daily relevance and sentiment. Args: company_name: the name of the company to search news for. top_k: the number of top search results to analyze. Returns: A string summarizing if there are daily news and whether the sentiment is positive or negative. """ search_tool = DuckDuckGoSearchTool() results = search_tool.run(f"{company_name} latest news")
if not results: return f"No news found for {company_name}."
selected_results = results[:top_k] combined_text = " ".join(res["body"] for res in selected_results if "body" in res)
has_today_news = any(news_date_is_today(res.get("body", "")) for res in selected_results) sentiment = sentiment_analysis(combined_text)
daily_news_str = "There is news today" if has_today_news else "There is no news specifically from today" return f"{daily_news_str} about {company_name}. Overall sentiment appears {sentiment} from an economic perspective."
final_answer = FinalAnswerTool()
16 unmodified lines agent = CodeAgent( model=model, tools=[final_answer], ## add your tools here (don't remove final answer) tools=[final_answer,company_news_sentiment], ## add your tools here (don't remove final answer) max_steps=6, verbosity_level=1, grammar=None,app.py+52-1