Spaces:
Sleeping
Sleeping
update sentiment_tool.py
Browse files- tools/sentiment_tool.py +14 -6
tools/sentiment_tool.py
CHANGED
|
@@ -33,7 +33,7 @@ class SentimentTool(BaseTool):
|
|
| 33 |
news_response.raise_for_status()
|
| 34 |
news = news_response.json().get("news", [])
|
| 35 |
headlines = [n["title"] for n in news[:5]]
|
| 36 |
-
except:
|
| 37 |
pass # silent fail
|
| 38 |
|
| 39 |
#Reddit sentiment
|
|
@@ -45,18 +45,26 @@ class SentimentTool(BaseTool):
|
|
| 45 |
reddit_response.raise_for_status()
|
| 46 |
reddit_posts = reddit_response.json().get("data", {}).get("children", [])
|
| 47 |
reddit_titles = [p["data"]["title"] for p in reddit_posts[:5]]
|
| 48 |
-
except:
|
| 49 |
-
pass
|
| 50 |
|
| 51 |
combined_text = "News: " + " | ".join(headlines) + "\nReddit: " + " | ".join(reddit_titles)
|
| 52 |
|
| 53 |
#analyze sentiment
|
| 54 |
sentiment_prompt = f"""
|
| 55 |
You are a crypto sentiment analyst. Based on the following headlines and Reddit posts,
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
{combined_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
"""
|
| 61 |
completion = client.chat.completions.create(
|
| 62 |
model="gpt-4.1",
|
|
@@ -66,7 +74,7 @@ You are a crypto sentiment analyst. Based on the following headlines and Reddit
|
|
| 66 |
],
|
| 67 |
temperature=0.2
|
| 68 |
)
|
| 69 |
-
sentiment_result = completion.choices[0].message.content
|
| 70 |
return sentiment_result
|
| 71 |
|
| 72 |
except Exception as e:
|
|
|
|
| 33 |
news_response.raise_for_status()
|
| 34 |
news = news_response.json().get("news", [])
|
| 35 |
headlines = [n["title"] for n in news[:5]]
|
| 36 |
+
except Exception:
|
| 37 |
pass # silent fail
|
| 38 |
|
| 39 |
#Reddit sentiment
|
|
|
|
| 45 |
reddit_response.raise_for_status()
|
| 46 |
reddit_posts = reddit_response.json().get("data", {}).get("children", [])
|
| 47 |
reddit_titles = [p["data"]["title"] for p in reddit_posts[:5]]
|
| 48 |
+
except Exception:
|
| 49 |
+
pass
|
| 50 |
|
| 51 |
combined_text = "News: " + " | ".join(headlines) + "\nReddit: " + " | ".join(reddit_titles)
|
| 52 |
|
| 53 |
#analyze sentiment
|
| 54 |
sentiment_prompt = f"""
|
| 55 |
You are a crypto sentiment analyst. Based on the following headlines and Reddit posts,
|
| 56 |
+
intuitively determine if the overall sentiment towards "{query}" is bullish, bearish, or neutral.
|
| 57 |
+
Respond with a short summary and a one-word sentiment classification.
|
| 58 |
|
| 59 |
{combined_text}
|
| 60 |
+
|
| 61 |
+
Return ONLY valid JSON in the format:
|
| 62 |
+
{{
|
| 63 |
+
"sentiment": "bullish/bearish/neutral",
|
| 64 |
+
"reasoning": "short explanation",
|
| 65 |
+
"news_headlines": [...],
|
| 66 |
+
"reddit_titles": [...]
|
| 67 |
+
}}
|
| 68 |
"""
|
| 69 |
completion = client.chat.completions.create(
|
| 70 |
model="gpt-4.1",
|
|
|
|
| 74 |
],
|
| 75 |
temperature=0.2
|
| 76 |
)
|
| 77 |
+
sentiment_result = completion.choices[0].message.content
|
| 78 |
return sentiment_result
|
| 79 |
|
| 80 |
except Exception as e:
|