Spaces:
Sleeping
Sleeping
| import random | |
| from datetime import datetime | |
| class SentimentAnalyzer: | |
| def __init__(self): | |
| self.sentiment_sources = [ | |
| "Federal Reserve hints at rate pause - positive for gold", | |
| "Inflation data higher than expected - gold demand rising", | |
| "Dollar strength weighs on precious metals", | |
| "Central banks continue gold accumulation", | |
| "Geopolitical tensions support safe-haven demand", | |
| "Gold ETFs see outflows amid risk-on sentiment", | |
| "Technical breakout above resistance level", | |
| "Profit-taking observed after recent rally" | |
| ] | |
| def analyze_gold_sentiment(self): | |
| """Analyze sentiment for gold market""" | |
| try: | |
| # Simulate sentiment analysis | |
| # In production, would use actual news API and NLP model | |
| # Generate random sentiment around current market conditions | |
| base_sentiment = random.uniform(-0.5, 0.5) | |
| # Add some realistic variation | |
| if random.random() > 0.7: | |
| # Strong sentiment event | |
| sentiment = base_sentiment + random.uniform(-0.5, 0.5) | |
| else: | |
| sentiment = base_sentiment | |
| # Clamp between -1 and 1 | |
| sentiment = max(-1, min(1, sentiment)) | |
| # Generate news summary | |
| num_news = random.randint(3, 6) | |
| selected_news = random.sample(self.sentiment_sources, num_news) | |
| news_html = "<div style='max-height: 300px; overflow-y: auto;'>" | |
| news_html += "<h4 style='color: #FFD700;'>Latest Gold News</h4>" | |
| for i, news in enumerate(selected_news, 1): | |
| sentiment_label = "🟢" if "positive" in news or "rising" in news or "support" in news else \ | |
| "🔴" if "weighs" in news or "outflows" in news or "Profit-taking" in news else \ | |
| "🟡" | |
| news_html += f"<p style='margin: 10px 0; padding: 10px; background: rgba(255,255,255,0.05); border-radius: 5px;'>{sentiment_label} {news}</p>" | |
| news_html += "</div>" | |
| return sentiment, news_html | |
| except Exception as e: | |
| return 0, f"<p>Error analyzing sentiment: {str(e)}</p>" |