cutechicken's picture
Update app.py
8129390 verified
raw
history blame
15.7 kB
import logging
import gradio as gr
import pandas as pd
import torch
import numpy as np
import matplotlib.pyplot as plt
from GoogleNews import GoogleNews
from transformers import pipeline
from datetime import datetime, timedelta
import matplotlib
import yfinance as yf
matplotlib.use('Agg')
# Set up logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
SENTIMENT_ANALYSIS_MODEL = (
"mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
)
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
logging.info(f"Using device: {DEVICE}")
logging.info("Initializing sentiment analysis model...")
sentiment_analyzer = pipeline(
"sentiment-analysis", model=SENTIMENT_ANALYSIS_MODEL, device=DEVICE
)
logging.info("Model initialized successfully")
# ์ƒ์žฅ ์ข…๋ชฉ ์‹ฌ๋ณผ ๋งคํ•‘์„ ์œ„ํ•œ ์ผ๋ฐ˜์ ์ธ ์ข…๋ชฉ๋ช… ์‚ฌ์ „ (ํ•„์š”์— ๋”ฐ๋ผ ํ™•์žฅ)
COMMON_TICKERS = {
"apple": "AAPL",
"microsoft": "MSFT",
"amazon": "AMZN",
"google": "GOOGL",
"alphabet": "GOOGL",
"facebook": "META",
"meta": "META",
"tesla": "TSLA",
"nvidia": "NVDA",
"bitcoin": "BTC-USD",
"ethereum": "ETH-USD",
"samsung": "005930.KS", # ํ•œ๊ตญ ์‚ผ์„ฑ์ „์ž
"hyundai": "005380.KS", # ํ˜„๋Œ€์ž๋™์ฐจ
"sk hynix": "000660.KS", # SKํ•˜์ด๋‹‰์Šค
}
def fetch_articles(query, max_articles=30):
try:
logging.info(f"Fetching up to {max_articles} articles for query: '{query}'")
googlenews = GoogleNews(lang="en")
googlenews.search(query)
# ์ฒซ ํŽ˜์ด์ง€ ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ
articles = googlenews.result()
# ๋ชฉํ‘œ ๊ธฐ์‚ฌ ์ˆ˜์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ์ถ”๊ฐ€ ํŽ˜์ด์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ
page = 2
while len(articles) < max_articles and page <= 10: # ์ตœ๋Œ€ 10ํŽ˜์ด์ง€๊นŒ์ง€๋งŒ ์‹œ๋„
logging.info(f"Fetched {len(articles)} articles so far. Getting page {page}...")
googlenews.get_page(page)
page_results = googlenews.result()
# ์ƒˆ ๊ฒฐ๊ณผ๊ฐ€ ์—†์œผ๋ฉด ์ค‘๋‹จ
if not page_results:
logging.info(f"No more results found after page {page-1}")
break
articles.extend(page_results)
page += 1
# ์ตœ๋Œ€ ๊ธฐ์‚ฌ ์ˆ˜๋กœ ์ œํ•œ
articles = articles[:max_articles]
logging.info(f"Successfully fetched {len(articles)} articles")
return articles
except Exception as e:
logging.error(
f"Error while searching articles for query: '{query}'. Error: {e}"
)
raise gr.Error(
f"Unable to search articles for query: '{query}'. Try again later...",
duration=5,
)
def analyze_article_sentiment(article):
logging.info(f"Analyzing sentiment for article: {article['title']}")
sentiment = sentiment_analyzer(article["desc"])[0]
article["sentiment"] = sentiment
return article
def calculate_time_weight(article_date_str):
"""
๊ธฐ์‚ฌ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์ค‘์น˜ ๊ณ„์‚ฐ
- 1์‹œ๊ฐ„ ๋‚ด ๊ธฐ์‚ฌ๋Š” 24% ๊ฐ€์ค‘์น˜
- ์‹œ๊ฐ„์ด ์ง€๋‚ ์ˆ˜๋ก 1%์”ฉ ๊ฐ์†Œ (์ตœ์†Œ 1%)
- ์˜ˆ: 1์‹œ๊ฐ„ ๋‚ด ๊ธฐ์‚ฌ = 24%, 10์‹œ๊ฐ„ ์ „ ๊ธฐ์‚ฌ = 15%, 24์‹œ๊ฐ„ ์ „ ๊ธฐ์‚ฌ = 1%
- 24์‹œ๊ฐ„ ์ด์ƒ์ด๋ฉด 1%๋กœ ๊ณ ์ •
"""
try:
# ๊ธฐ์‚ฌ ๋‚ ์งœ ๋ฌธ์ž์—ด ํŒŒ์‹ฑ (๋‹ค์–‘ํ•œ ํ˜•์‹ ์ฒ˜๋ฆฌ)
date_formats = [
'%a, %d %b %Y %H:%M:%S %z', # ๊ธฐ๋ณธ GoogleNews ํ˜•์‹
'%Y-%m-%d %H:%M:%S',
'%a, %d %b %Y %H:%M:%S',
'%Y-%m-%dT%H:%M:%S%z',
'%a %b %d, %Y',
'%d %b %Y'
]
parsed_date = None
for format_str in date_formats:
try:
parsed_date = datetime.strptime(article_date_str, format_str)
break
except ValueError:
continue
# ์–ด๋–ค ํ˜•์‹์œผ๋กœ๋„ ํŒŒ์‹ฑํ•  ์ˆ˜ ์—†์œผ๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„ ๊ธฐ์ค€ 24์‹œ๊ฐ„ ์ „์œผ๋กœ ๊ฐ€์ •
if parsed_date is None:
logging.warning(f"Could not parse date: {article_date_str}, using default 24h ago")
return 0.01 # ์ตœ์†Œ ๊ฐ€์ค‘์น˜ 1%
# ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ์˜ ์ฐจ์ด ๊ณ„์‚ฐ (์‹œ๊ฐ„ ๋‹จ์œ„)
now = datetime.now()
if parsed_date.tzinfo is not None:
now = now.replace(tzinfo=parsed_date.tzinfo)
hours_diff = (now - parsed_date).total_seconds() / 3600
# 24์‹œ๊ฐ„ ์ด๋‚ด์ธ ๊ฒฝ์šฐ๋งŒ ๊ณ ๋ ค
if hours_diff < 1: # 1์‹œ๊ฐ„ ์ด๋‚ด
return 0.24 # 24% ๊ฐ€์ค‘์น˜
elif hours_diff < 24: # 1~23์‹œ๊ฐ„
# 1์‹œ๊ฐ„๋‹น 1%์”ฉ ๊ฐ์†Œ (1์‹œ๊ฐ„ = 24%, 2์‹œ๊ฐ„ = 23%, ...)
return max(0.01, 0.24 - ((hours_diff - 1) * 0.01))
else:
return 0.01 # 24์‹œ๊ฐ„ ์ด์ƒ ์ง€๋‚œ ๊ธฐ์‚ฌ๋Š” 1% ๊ฐ€์ค‘์น˜
except Exception as e:
logging.error(f"Error calculating time weight: {e}")
return 0.01 # ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์ตœ์†Œ ๊ฐ€์ค‘์น˜ ์ ์šฉ
def calculate_sentiment_score(sentiment_label, time_weight):
"""
๊ฐ์„ฑ ๋ ˆ์ด๋ธ”์— ๋”ฐ๋ฅธ ๊ธฐ๋ณธ ์ ์ˆ˜ ๊ณ„์‚ฐ ๋ฐ ์‹œ๊ฐ„ ๊ฐ€์ค‘์น˜ ์ ์šฉ
- positive: +3์ 
- neutral: 0์ 
- negative: -3์ 
์‹œ๊ฐ„ ๊ฐ€์ค‘์น˜๋Š” ๋ฐฑ๋ถ„์œจ๋กœ ์ ์šฉ (๊ธฐ๋ณธ ์ ์ˆ˜์— ๊ฐ€์ค‘์น˜ % ๋งŒํผ ์ถ”๊ฐ€)
์˜ˆ:
- 1์‹œ๊ฐ„ ๋‚ด ๊ธ์ • ๊ธฐ์‚ฌ: 3์  + (3 * 24%) = 3 + 0.72 = 3.72์ 
- 10์‹œ๊ฐ„ ์ „ ๋ถ€์ • ๊ธฐ์‚ฌ: -3์  + (-3 * 15%) = -3 - 0.45 = -3.45์ 
"""
base_score = {
'positive': 3,
'neutral': 0,
'negative': -3
}.get(sentiment_label, 0)
# ๊ฐ€์ค‘์น˜๋ฅผ ์ ์šฉํ•œ ์ถ”๊ฐ€ ์ ์ˆ˜ ๊ณ„์‚ฐ
weighted_addition = base_score * time_weight
return base_score, weighted_addition
def get_stock_ticker(asset_name):
"""
์ž์‚ฐ๋ช…์œผ๋กœ๋ถ€ํ„ฐ ์ฃผ์‹ ํ‹ฐ์ปค ์‹ฌ๋ณผ์„ ์ถ”์ถœ
"""
# ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋งคํ•‘ ํ™•์ธ
asset_lower = asset_name.lower()
# ์ง์ ‘ ํ‹ฐ์ปค๋กœ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ (๋Œ€๋ฌธ์ž 3-5์ž ํ˜•ํƒœ)
if asset_name.isupper() and 3 <= len(asset_name) <= 5:
return asset_name
# ์ผ๋ฐ˜์ ์ธ ์ข…๋ชฉ๋ช… ๋งคํ•‘ ํ™•์ธ
if asset_lower in COMMON_TICKERS:
return COMMON_TICKERS[asset_lower]
# ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ yfinance๋กœ ๊ฒ€์ƒ‰ ์‹œ๋„
try:
ticker_search = yf.Ticker(asset_name)
# ๊ธฐ๋ณธ ์ •๋ณด ๊ฐ€์ ธ์™€์„œ ์œ ํšจํ•œ ํ‹ฐ์ปค์ธ์ง€ ํ™•์ธ
info = ticker_search.info
if 'symbol' in info:
return info['symbol']
except:
pass
return None
def create_stock_chart(ticker, period="1mo"):
"""
์ฃผ์‹ ํ‹ฐ์ปค์— ๋Œ€ํ•œ ์ฐจํŠธ ์ƒ์„ฑ
"""
try:
logging.info(f"Fetching stock data for {ticker}")
stock_data = yf.download(ticker, period=period)
if stock_data.empty:
logging.warning(f"No stock data found for ticker: {ticker}")
return None
fig, ax = plt.subplots(figsize=(10, 6))
# ์ข…๊ฐ€ ๊ทธ๋ž˜ํ”„
ax.plot(stock_data.index, stock_data['Close'], label='Close Price', color='blue')
# ์ด๋™ํ‰๊ท ์„  ์ถ”๊ฐ€ (20์ผ)
if len(stock_data) > 20:
stock_data['MA20'] = stock_data['Close'].rolling(window=20).mean()
ax.plot(stock_data.index, stock_data['MA20'], label='20-day MA', color='orange')
# ๊ฑฐ๋ž˜๋Ÿ‰ ์„œ๋ธŒํ”Œ๋กฏ ์ถ”๊ฐ€
ax2 = ax.twinx()
ax2.bar(stock_data.index, stock_data['Volume'], alpha=0.3, color='gray', label='Volume')
ax2.set_ylabel('Volume')
# ์ฐจํŠธ ์Šคํƒ€์ผ๋ง
ax.set_title(f"{ticker} Stock Price")
ax.set_xlabel('Date')
ax.set_ylabel('Price')
ax.grid(True, alpha=0.3)
# ๋ฒ”๋ก€ ์ถ”๊ฐ€
lines, labels = ax.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax.legend(lines + lines2, labels + labels2, loc='upper left')
plt.tight_layout()
# ์ด๋ฏธ์ง€ ์ €์žฅ
chart_path = f"stock_chart_{ticker.replace('-', '_')}.png"
plt.savefig(chart_path)
plt.close()
logging.info(f"Stock chart created: {chart_path}")
return chart_path
except Exception as e:
logging.error(f"Error creating stock chart for {ticker}: {e}")
return None
def analyze_asset_sentiment(asset_name):
logging.info(f"Starting sentiment analysis for asset: {asset_name}")
logging.info("Fetching up to 30 articles")
articles = fetch_articles(asset_name, max_articles=30)
logging.info("Analyzing sentiment of each article")
analyzed_articles = [analyze_article_sentiment(article) for article in articles]
# ๊ฐ ๊ธฐ์‚ฌ์— ๋Œ€ํ•œ ์‹œ๊ฐ„ ๊ฐ€์ค‘์น˜ ๋ฐ ๊ฐ์„ฑ ์ ์ˆ˜ ๊ณ„์‚ฐ
for article in analyzed_articles:
time_weight = calculate_time_weight(article["date"])
article["time_weight"] = time_weight
sentiment_label = article["sentiment"]["label"]
base_score, weighted_addition = calculate_sentiment_score(sentiment_label, time_weight)
article["base_score"] = base_score
article["weighted_addition"] = weighted_addition
article["total_score"] = base_score + weighted_addition
logging.info("Sentiment analysis completed")
# ์ข…ํ•ฉ ์ ์ˆ˜ ๊ณ„์‚ฐ ๋ฐ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
sentiment_summary = create_sentiment_summary(analyzed_articles, asset_name)
# ์ฃผ์‹ ํ‹ฐ์ปค ํ™•์ธ ๋ฐ ์ฐจํŠธ ์ƒ์„ฑ
stock_chart = None
ticker = get_stock_ticker(asset_name)
if ticker:
logging.info(f"Found ticker {ticker} for asset {asset_name}")
stock_chart = create_stock_chart(ticker)
return convert_to_dataframe(analyzed_articles), sentiment_summary, stock_chart, ticker
def create_sentiment_summary(analyzed_articles, asset_name):
"""
๊ฐ์„ฑ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์š”์•ฝํ•˜๊ณ  ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”
"""
total_articles = len(analyzed_articles)
positive_count = sum(1 for a in analyzed_articles if a["sentiment"]["label"] == "positive")
neutral_count = sum(1 for a in analyzed_articles if a["sentiment"]["label"] == "neutral")
negative_count = sum(1 for a in analyzed_articles if a["sentiment"]["label"] == "negative")
# ๊ธฐ๋ณธ ์ ์ˆ˜ ํ•ฉ๊ณ„
base_score_sum = sum(a["base_score"] for a in analyzed_articles)
# ๊ฐ€์ค‘์น˜ ์ ์šฉ ์ ์ˆ˜ ํ•ฉ๊ณ„
weighted_score_sum = sum(a["total_score"] for a in analyzed_articles)
# ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
# 1. ๊ฐ์„ฑ ๋ถ„ํฌ ํŒŒ์ด ์ฐจํŠธ
labels = ['Positive', 'Neutral', 'Negative']
sizes = [positive_count, neutral_count, negative_count]
colors = ['green', 'gray', 'red']
ax1.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
ax1.axis('equal')
ax1.set_title(f'Sentiment Distribution for {asset_name}')
# 2. ์‹œ๊ฐ„๋ณ„ ๊ฐ€์ค‘์น˜ ์ ์šฉ ์ ์ˆ˜ (์ •๋ ฌ)
sorted_articles = sorted(analyzed_articles, key=lambda x: x.get("date", ""), reverse=True)
# ์ตœ๋Œ€ ํ‘œ์‹œํ•  ๊ธฐ์‚ฌ ์ˆ˜ (๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด)
max_display = min(15, len(sorted_articles))
display_articles = sorted_articles[:max_display]
dates = [a.get("date", "")[:10] for a in display_articles] # ๋‚ ์งœ ๋ถ€๋ถ„๋งŒ ํ‘œ์‹œ
scores = [a.get("total_score", 0) for a in display_articles]
# ์ ์ˆ˜์— ๋”ฐ๋ฅธ ์ƒ‰์ƒ ์„ค์ •
bar_colors = ['green' if s > 0 else 'red' if s < 0 else 'gray' for s in scores]
bars = ax2.bar(range(len(dates)), scores, color=bar_colors)
ax2.set_xticks(range(len(dates)))
ax2.set_xticklabels(dates, rotation=45, ha='right')
ax2.set_ylabel('Weighted Sentiment Score')
ax2.set_title(f'Recent Article Scores for {asset_name}')
ax2.axhline(y=0, color='black', linestyle='-', alpha=0.3)
# ์š”์•ฝ ํ…์ŠคํŠธ ์ถ”๊ฐ€
summary_text = f"""
Analysis Summary for {asset_name}:
Total Articles: {total_articles}
Positive: {positive_count} ({positive_count/total_articles*100:.1f}%)
Neutral: {neutral_count} ({neutral_count/total_articles*100:.1f}%)
Negative: {negative_count} ({negative_count/total_articles*100:.1f}%)
Base Score Sum: {base_score_sum:.2f}
Weighted Score Sum: {weighted_score_sum:.2f}
"""
plt.figtext(0.5, 0.01, summary_text, ha='center', fontsize=10, bbox={"facecolor":"orange", "alpha":0.2, "pad":5})
plt.tight_layout(rect=[0, 0.1, 1, 0.95])
# ์ด๋ฏธ์ง€ ์ €์žฅ
fig_path = f"sentiment_summary_{asset_name.replace(' ', '_')}.png"
plt.savefig(fig_path)
plt.close()
return fig_path
def convert_to_dataframe(analyzed_articles):
df = pd.DataFrame(analyzed_articles)
df["Title"] = df.apply(
lambda row: f'<a href="{row["link"]}" target="_blank">{row["title"]}</a>',
axis=1,
)
df["Description"] = df["desc"]
df["Date"] = df["date"]
def sentiment_badge(sentiment):
colors = {
"negative": "red",
"neutral": "gray",
"positive": "green",
}
color = colors.get(sentiment, "grey")
return f'<span style="background-color: {color}; color: white; padding: 2px 6px; border-radius: 4px;">{sentiment}</span>'
df["Sentiment"] = df["sentiment"].apply(lambda x: sentiment_badge(x["label"]))
# ์ ์ˆ˜ ์ปฌ๋Ÿผ ์ถ”๊ฐ€
df["Base Score"] = df["base_score"]
df["Weight"] = df["time_weight"].apply(lambda x: f"{x*100:.0f}%")
df["Total Score"] = df["total_score"].apply(lambda x: f"{x:.2f}")
return df[["Sentiment", "Title", "Description", "Date", "Base Score", "Weight", "Total Score"]]
with gr.Blocks() as iface:
gr.Markdown("# Trading Asset Sentiment Analysis")
gr.Markdown(
"Enter the name of a trading asset, and I'll fetch recent articles and analyze their sentiment!"
)
with gr.Row():
input_asset = gr.Textbox(
label="Asset Name",
lines=1,
placeholder="Enter the name of the trading asset...",
)
with gr.Row():
analyze_button = gr.Button("Analyze Sentiment", size="sm")
gr.Examples(
examples=[
"Bitcoin",
"Tesla",
"Apple",
"Amazon",
],
inputs=input_asset,
)
# ์ฃผ์‹ ์ฐจํŠธ ์˜์—ญ ์ถ”๊ฐ€
with gr.Row():
with gr.Column():
with gr.Blocks():
gr.Markdown("## Stock Chart")
with gr.Row():
stock_chart = gr.Image(type="filepath", label="Stock Price Chart")
ticker_info = gr.Textbox(label="Ticker Symbol")
with gr.Row():
with gr.Column():
with gr.Blocks():
gr.Markdown("## Sentiment Summary")
sentiment_summary = gr.Image(type="filepath", label="Sentiment Analysis Summary")
with gr.Row():
with gr.Column():
with gr.Blocks():
gr.Markdown("## Articles and Sentiment Analysis")
articles_output = gr.Dataframe(
headers=["Sentiment", "Title", "Description", "Date", "Base Score", "Weight", "Total Score"],
datatype=["markdown", "html", "markdown", "markdown", "number", "markdown", "markdown"],
wrap=False,
)
analyze_button.click(
analyze_asset_sentiment,
inputs=[input_asset],
outputs=[articles_output, sentiment_summary, stock_chart, ticker_info],
)
logging.info("Launching Gradio interface")
iface.queue().launch()