Spaces:
Sleeping
Sleeping
Alberto Carmona
commited on
Commit
·
f4631b5
1
Parent(s):
c28c6ad
Remove sentiment analysis and entity recognition functions from get_news implementation
Browse files
tools.py
CHANGED
@@ -40,64 +40,11 @@ def get_news(query: str) -> List[Dict]:
|
|
40 |
"index": len(last_news) + 1,
|
41 |
"title": res.get("title", "No title available"),
|
42 |
"summary": res.get("content", "No summary available"),
|
43 |
-
# "sentiment": analyze_sentiment(res.get("snippet", "")),
|
44 |
-
# "entities": recognize_entities(res.get("snippet", ""))
|
45 |
})
|
46 |
print(f"Found {len(last_news)} articles.")
|
47 |
return last_news
|
48 |
|
49 |
|
50 |
-
def analyze_sentiment(text: str) -> str:
|
51 |
-
"""
|
52 |
-
Analyzes the sentiment of the given text and returns the sentiment label.
|
53 |
-
|
54 |
-
Args:
|
55 |
-
text: The input text to analyze.
|
56 |
-
|
57 |
-
Returns:
|
58 |
-
str: The sentiment label, such as 'positive', 'negative', or 'neutral'.
|
59 |
-
"""
|
60 |
-
prompt = f"""
|
61 |
-
question: Tell me if the sentiment of this news is positive, negative, or neutral?
|
62 |
-
context: {text}"""
|
63 |
-
try:
|
64 |
-
result = llm_openai.chat(
|
65 |
-
messages=[ChatMessage(role="user", content=prompt)]
|
66 |
-
)
|
67 |
-
except Exception as e:
|
68 |
-
return f"Error analyzing sentiment: {str(e)}"
|
69 |
-
sentiment_label = result.message.content
|
70 |
-
if "positive" in sentiment_label.lower():
|
71 |
-
return "POSITIVE"
|
72 |
-
elif "negative" in sentiment_label.lower():
|
73 |
-
return "NEGATIVE"
|
74 |
-
else:
|
75 |
-
return "NEUTRAL"
|
76 |
-
|
77 |
-
|
78 |
-
def recognize_entities(text: str) -> List[str]:
|
79 |
-
"""
|
80 |
-
Recognizes named entities in the given text.
|
81 |
-
|
82 |
-
Args:
|
83 |
-
text: The input text in which to recognize entities.
|
84 |
-
|
85 |
-
Returns:
|
86 |
-
List[str]: A list of recognized entity names as strings.
|
87 |
-
"""
|
88 |
-
prompt = f"""
|
89 |
-
question: Tell me entities mentioned in this news?
|
90 |
-
context: {text}"""
|
91 |
-
try:
|
92 |
-
result = llm_openai.chat(
|
93 |
-
messages=[ChatMessage(role="user", content=prompt)]
|
94 |
-
)
|
95 |
-
except Exception as e:
|
96 |
-
return f"Error recognizing entities: {str(e)}"
|
97 |
-
entities = result.message.content.split(", ")
|
98 |
-
return entities if entities else ["No entities found."]
|
99 |
-
|
100 |
-
|
101 |
def generate_implications(article_index: int) -> str:
|
102 |
"""
|
103 |
Generates a string describing the possible implications of a news article based on its index.
|
|
|
40 |
"index": len(last_news) + 1,
|
41 |
"title": res.get("title", "No title available"),
|
42 |
"summary": res.get("content", "No summary available"),
|
|
|
|
|
43 |
})
|
44 |
print(f"Found {len(last_news)} articles.")
|
45 |
return last_news
|
46 |
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
def generate_implications(article_index: int) -> str:
|
49 |
"""
|
50 |
Generates a string describing the possible implications of a news article based on its index.
|