| import gradio as gr |
| from news_scraper import YahooFinanceScraper |
| from llm_analyzer import NewsAnalyzer |
| import os |
|
|
| |
| scraper = YahooFinanceScraper() |
| analyzer = NewsAnalyzer() |
|
|
| def analyze_news(topic, num_articles=5): |
| """ |
| Main function to scrape and analyze Yahoo Finance news |
| """ |
| try: |
| |
| status_msg = f"🔍 กำลังค้นหาข่าวเกี่ยวกับ: {topic}..." |
| yield status_msg, "", "" |
| |
| articles = scraper.get_news(topic, limit=num_articles) |
| |
| if not articles: |
| yield "❌ ไม่พบข่าวที่เกี่ยวข้อง", "", "" |
| return |
| |
| |
| articles_text = "## 📰 ข่าวที่พบ:\n\n" |
| for i, article in enumerate(articles, 1): |
| articles_text += f"### {i}. {article['title']}\n" |
| articles_text += f"**แหล่งที่มา:** {article['source']}\n" |
| articles_text += f"**เวลา:** {article['time']}\n" |
| articles_text += f"**สรุป:** {article['summary'][:200]}...\n" |
| articles_text += f"**ลิงก์:** {article['link']}\n\n" |
| |
| yield f"✅ พบข่าว {len(articles)} รายการ", articles_text, "🤖 กำลังวิเคราะห์ข่าวด้วย AI..." |
| |
| |
| analysis = analyzer.analyze_articles(articles, topic) |
| |
| yield f"✅ วิเคราะห์เสร็จสมบูรณ์", articles_text, analysis |
| |
| except Exception as e: |
| yield f"❌ เกิดข้อผิดพลาด: {str(e)}", "", "" |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft(), title="Yahoo Finance News Analyzer") as demo: |
| gr.Markdown(""" |
| # 📊 Yahoo Finance News Analyzer |
| ### วิเคราะห์ข่าวการเงินด้วย AI (Powered by Gemma-3-4b) |
| |
| ค้นหาและวิเคราะห์ข่าวล่าสุดจาก Yahoo Finance เกี่ยวกับหุ้น, บริษัท, หรือหัวข้อทางการเงินที่คุณสนใจ |
| """) |
| |
| with gr.Row(): |
| with gr.Column(scale=2): |
| topic_input = gr.Textbox( |
| label="🔍 หัวข้อที่ต้องการค้นหา", |
| placeholder="เช่น: Apple, Tesla, Bitcoin, ตลาดหุ้น", |
| value="Tesla" |
| ) |
| num_articles = gr.Slider( |
| minimum=1, |
| maximum=10, |
| value=5, |
| step=1, |
| label="จำนวนข่าวที่ต้องการ" |
| ) |
| analyze_btn = gr.Button("🚀 เริ่มวิเคราะห์", variant="primary", size="lg") |
| |
| with gr.Column(scale=1): |
| status_output = gr.Textbox( |
| label="📌 สถานะ", |
| interactive=False, |
| lines=3 |
| ) |
| |
| gr.Markdown("---") |
| |
| with gr.Row(): |
| with gr.Column(): |
| articles_output = gr.Markdown(label="📰 ข่าวที่พบ") |
| |
| with gr.Column(): |
| analysis_output = gr.Markdown(label="🤖 การวิเคราะห์จาก AI") |
| |
| |
| gr.Examples( |
| examples=[ |
| ["Apple", 5], |
| ["Tesla stock", 3], |
| ["Bitcoin", 5], |
| ["Federal Reserve", 3], |
| ["NVIDIA earnings", 5] |
| ], |
| inputs=[topic_input, num_articles] |
| ) |
| |
| |
| analyze_btn.click( |
| fn=analyze_news, |
| inputs=[topic_input, num_articles], |
| outputs=[status_output, articles_output, analysis_output] |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |