cryptogold-prime / sentiment_analyzer.py
omniverse1's picture
Update Gradio app with multiple files
a469221 verified
raw
history blame
3.33 kB
import random
from datetime import datetime
class SentimentAnalyzer:
def __init__(self):
self.gold_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"
]
self.crypto_sources = [
"Fed rate hike fear drives BTC sell-off",
"Institutional adoption pushes Bitcoin price up",
"Whale wallets show large accumulation activity",
"Regulatory uncertainty weighs on crypto market",
"New protocol launch fuels altcoin rally",
"ETF approval anticipation creates bullish momentum",
"High funding rates suggest market overheating",
"Tether minting correlates with short-term pumps"
]
def analyze_market_sentiment(self, ticker):
"""Analyze sentiment for a given market (Simulated)"""
try:
# PENTING: Untuk sentimen riil, Anda harus mengintegrasikan API Berita Finansial (misalnya Finnhub, MarketAux)
# dan model NLP (misalnya BERT/Transformer) untuk menganalisis berita secara aktual.
# Memilih sumber data dan warna berdasarkan Ticker
if ticker == "BTC-USD":
base_sentiment = random.uniform(-0.3, 0.7)
sources = self.crypto_sources
title_color = "#FFA500"
else: # GC=F
base_sentiment = random.uniform(-0.5, 0.5)
sources = self.gold_sources
title_color = "#FFD700"
# Simulasi analisis sentimen
sentiment = base_sentiment + random.uniform(-0.2, 0.2)
sentiment = max(-1, min(1, sentiment))
# Generate news summary
num_news = random.randint(3, 5)
selected_news = random.sample(sources, num_news)
# Tampilan News (menggunakan background terang #E0E0E0 agar terlihat di tema putih)
news_html = "<div style='max-height: 200px; overflow-y: auto; color: black;'>"
news_html += f"<h4 style='color: {title_color};'>Latest {ticker} News (Simulated)</h4>"
for news in selected_news:
sentiment_label = "🟢" if "positive" in news or "rising" in news or "support" in news or "bullish" in news or "accumulation" in news else \
"🔴" if "sell-off" in news or "weighs" in news or "outflows" in news or "Profit-taking" in news or "fear" in news else \
"🟡"
news_html += f"<p style='margin: 10px 0; padding: 10px; background: #E0E0E0; border-radius: 5px; color: black;'>{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>"