File size: 10,985 Bytes
eaa6f1b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# app.py
"""
Yahoo Finance Sentiment Analysis with Gemma LLM
Hugging Face Space Application
"""
import gradio as gr
import pandas as pd
from datetime import datetime
from utils import YahooFinanceScraper, SentimentAnalyzer, LLMAnalyzer
from config import POPULAR_STOCKS
import plotly.graph_objects as go
# Initialize components
print("Initializing application...")
scraper = YahooFinanceScraper()
sentiment_analyzer = SentimentAnalyzer()
llm_analyzer = LLMAnalyzer()
print("Application ready!")
def analyze_stock_news(symbol: str, num_articles: int = 10):
"""
Main function to analyze stock news
Args:
symbol: Stock ticker symbol
num_articles: Number of articles to analyze
Returns:
Tuple of (summary, dataframe, chart, llm_insights)
"""
try:
# Fetch news
articles = scraper.get_stock_news(symbol, num_articles)
if not articles:
return "No news found for this symbol.", None, None, "No articles to analyze."
# Analyze sentiments
sentiments = []
for article in articles:
text = f"{article['title']}. {article.get('summary', '')}"
sentiment = sentiment_analyzer.analyze_comprehensive(text)
sentiments.append(sentiment)
# Generate LLM insights
market_summary = llm_analyzer.summarize_news(articles)
investment_insight = llm_analyzer.generate_investment_insight(symbol, articles, sentiments)
# Create dataframe
df_data = []
for article, sentiment in zip(articles, sentiments):
df_data.append({
'Title': article['title'],
'Publisher': article['publisher'],
'Sentiment': sentiment['sentiment_label'],
'Score': f"{sentiment['combined_score']:.3f}",
'Confidence': f"{sentiment['confidence']:.2%}",
'VADER': f"{sentiment['vader']['compound']:.3f}",
'FinBERT +': f"{sentiment['finbert']['positive']:.3f}",
'FinBERT -': f"{sentiment['finbert']['negative']:.3f}",
})
df = pd.DataFrame(df_data)
# Create visualization
sentiment_counts = df['Sentiment'].value_counts()
fig = go.Figure(data=[
go.Bar(
x=sentiment_counts.index,
y=sentiment_counts.values,
marker_color=['#00cc66' if x=='Positive' else '#ff6666' if x=='Negative' else '#999999'
for x in sentiment_counts.index]
)
])
fig.update_layout(
title=f"Sentiment Distribution for {symbol}",
xaxis_title="Sentiment",
yaxis_title="Number of Articles",
height=400
)
# Calculate statistics
avg_score = sum(s['combined_score'] for s in sentiments) / len(sentiments)
positive_pct = (sentiment_counts.get('Positive', 0) / len(sentiments)) * 100
negative_pct = (sentiment_counts.get('Negative', 0) / len(sentiments)) * 100
summary = f"""
## π Analysis Summary for {symbol}
**Total Articles Analyzed:** {len(articles)}
**Sentiment Distribution:**
- π’ Positive: {sentiment_counts.get('Positive', 0)} ({positive_pct:.1f}%)
- π΄ Negative: {sentiment_counts.get('Negative', 0)} ({negative_pct:.1f}%)
- βͺ Neutral: {sentiment_counts.get('Neutral', 0)} ({100-positive_pct-negative_pct:.1f}%)
**Average Sentiment Score:** {avg_score:.3f}
**Overall Sentiment:** {"π’ Positive" if avg_score > 0.05 else "π΄ Negative" if avg_score < -0.05 else "βͺ Neutral"}
"""
llm_insights = f"""
## π€ AI-Generated Insights (Powered by Gemma)
### Market Summary:
{market_summary}
### Investment Perspective:
{investment_insight}
---
*Note: These insights are generated by AI and should not be considered as financial advice.*
"""
return summary, df, fig, llm_insights
except Exception as e:
return f"Error: {str(e)}", None, None, "Error generating insights."
def analyze_single_headline(headline: str):
"""
Analyze a single headline
Args:
headline: News headline text
Returns:
Analysis results
"""
try:
sentiment = sentiment_analyzer.analyze_comprehensive(headline)
# Create a dummy article dict for LLM analysis
article = {'title': headline, 'summary': ''}
explanation = llm_analyzer.analyze_sentiment_context(article, sentiment)
result = f"""
## Sentiment Analysis Results
**Headline:** {headline}
**Overall Sentiment:** {sentiment['sentiment_label']} (Score: {sentiment['combined_score']:.3f})
**Confidence:** {sentiment['confidence']:.2%}
### Detailed Scores:
- **VADER Compound:** {sentiment['vader']['compound']:.3f}
- **FinBERT Positive:** {sentiment['finbert']['positive']:.3%}
- **FinBERT Negative:** {sentiment['finbert']['negative']:.3%}
- **FinBERT Neutral:** {sentiment['finbert']['neutral']:.3%}
### AI Explanation:
{explanation}
"""
return result
except Exception as e:
return f"Error analyzing headline: {str(e)}"
# Create Gradio Interface
with gr.Blocks(title="Yahoo Finance Sentiment Analyzer", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π Yahoo Finance Sentiment Analyzer
### Powered by FinBERT + Gemma LLM
Analyze market sentiment from Yahoo Finance news using advanced NLP and AI.
""")
with gr.Tabs():
# Tab 1: Stock Analysis
with gr.Tab("π Stock Sentiment Analysis"):
gr.Markdown("### Analyze sentiment of news for any stock symbol")
with gr.Row():
with gr.Column(scale=2):
stock_input = gr.Textbox(
label="Stock Symbol",
placeholder="e.g., AAPL, GOOGL, TSLA",
value="AAPL"
)
with gr.Column(scale=1):
num_articles = gr.Slider(
minimum=5,
maximum=20,
value=10,
step=1,
label="Number of Articles"
)
gr.Markdown("**Quick Select:**")
quick_buttons = []
with gr.Row():
for stock in POPULAR_STOCKS[:5]:
btn = gr.Button(stock, size="sm")
quick_buttons.append(btn)
with gr.Row():
for stock in POPULAR_STOCKS[5:10]:
btn = gr.Button(stock, size="sm")
quick_buttons.append(btn)
analyze_btn = gr.Button("π Analyze News", variant="primary", size="lg")
summary_output = gr.Markdown(label="Summary")
insights_output = gr.Markdown(label="AI Insights")
chart_output = gr.Plot(label="Sentiment Distribution")
table_output = gr.Dataframe(label="Detailed Results")
# Button actions
analyze_btn.click(
fn=analyze_stock_news,
inputs=[stock_input, num_articles],
outputs=[summary_output, table_output, chart_output, insights_output]
)
# Quick select buttons
for btn in quick_buttons:
btn.click(
fn=lambda x: x,
inputs=[btn],
outputs=[stock_input]
)
# Tab 2: Single Headline Analysis
with gr.Tab("π° Single Headline Analyzer"):
gr.Markdown("### Analyze sentiment of a single news headline")
headline_input = gr.Textbox(
label="News Headline",
placeholder="Enter a financial news headline...",
lines=3
)
gr.Markdown("**Example Headlines:**")
example_headlines = [
"Apple reaches all-time high as iPhone sales surge",
"Tesla stock plummets amid production concerns",
"Fed maintains interest rates, markets remain stable"
]
with gr.Row():
for example in example_headlines:
gr.Button(example[:50] + "...", size="sm").click(
fn=lambda x: x,
inputs=[gr.Textbox(value=example, visible=False)],
outputs=[headline_input]
)
analyze_headline_btn = gr.Button("π Analyze Headline", variant="primary")
headline_output = gr.Markdown(label="Analysis Results")
analyze_headline_btn.click(
fn=analyze_single_headline,
inputs=[headline_input],
outputs=[headline_output]
)
# Tab 3: About
with gr.Tab("βΉοΈ About"):
gr.Markdown("""
## About This Application
This application analyzes sentiment from Yahoo Finance news using multiple advanced techniques:
### π οΈ Technologies Used:
1. **VADER Sentiment Analysis**
- Rule-based sentiment analysis
- Good for general text sentiment
2. **FinBERT**
- BERT model fine-tuned for financial text
- Specialized in financial sentiment analysis
- Model: `ProsusAI/finbert`
3. **Gemma LLM**
- Google's Gemma language model
- Generates human-like insights and summaries
- Model: `google/gemma-2-2b-it`
### π Features:
- Real-time news scraping from Yahoo Finance
- Multi-model sentiment analysis
- AI-generated market insights
- Interactive visualizations
- Batch and single headline analysis
### π Sentiment Scores:
- **Positive**: Score > 0.05
- **Negative**: Score < -0.05
- **Neutral**: -0.05 β€ Score β€ 0.05
### β οΈ Disclaimer:
This tool is for educational and research purposes only.
The sentiment analysis and AI-generated insights should NOT be used as financial advice.
Always do your own research and consult with financial professionals before making investment decisions.
---
**Created with β€οΈ using Hugging Face Spaces**
""")
# Launch the app
if __name__ == "__main__":
demo.launch()
|