Sooteemon commited on
Commit
52d2674
·
verified ·
1 Parent(s): 522bdff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
app.py CHANGED
@@ -2,36 +2,36 @@
2
  import gradio as gr
3
  import pandas as pd
4
  from yahoo_finance_scraper import YahooFinanceScraper
5
- from sentiment_analyzer import SentimentAnalyzer # <--- 1. Import ตัวใหม่
6
 
7
  # --- 1. โหลดโมเดลและ Scraper ---
8
  try:
9
  scraper = YahooFinanceScraper()
10
- analyzer = SentimentAnalyzer() # <--- 2. โหลดตัวใหม่
11
  print("Scraper and Analyzer are ready.")
12
  except Exception as e:
13
  print(f"Error loading models: {e}")
14
  scraper = None
15
  analyzer = None
16
 
17
- # --- 2. ฟังก์ชันหลัก (แก้ไข) ---
18
- def process_symbol_search(symbol):
19
  """
20
- ค้นหาข่าวตาม Symbol -> วิเคราะห์ Sentiment -> สร้าง Markdown 2 ส่วน
21
  """
22
  if not scraper or not analyzer:
23
  return "Models failed to load. Check logs.", "" # คืนค่า 2 ส่วน
24
 
25
- if not symbol:
26
- return "Please enter a stock symbol.", ""
27
 
28
- print(f"Searching for symbol: {symbol}")
29
 
30
- # <--- 3. เรียget_latest_news (ที่แก้บั๊กแล้) ---
31
- news_list = scraper.get_latest_news(symbol, max_articles=10)
32
 
33
  if not news_list:
34
- return f"No recent news found for symbol: {symbol.upper()}", "" # คืนค่า 2 ส่วน
35
 
36
  # <--- 4. วิเคราะห์ Sentiment ---
37
  analyzed_list = analyzer.analyze_batch(news_list)
@@ -57,20 +57,18 @@ def process_symbol_search(symbol):
57
  markdown_output_lines.append(f"### 📰 {item['title']}")
58
  markdown_output_lines.append(f"*(Published: {date_str})*")
59
 
60
- # <--- 7. เพิ่ม Sentiment ในผลลัพธ์ ---
61
  sentiment_label = item['sentiment'].capitalize()
62
  sentiment_score = item['score'] * 100
63
  markdown_output_lines.append(f"\n**Sentiment: {sentiment_label}** ({sentiment_score:.1f}%)")
64
 
65
- if item['summary']:
66
- markdown_output_lines.append(f"\n**Summary:** {item['summary']}")
67
 
68
  markdown_output_lines.append(f"\n[Read Full Article]({item['link']})")
69
  markdown_output_lines.append("\n---")
70
 
71
  main_output_text = "\n".join(markdown_output_lines)
72
 
73
- # คืนค่า 2 ส่วน
74
  return summary_text, main_output_text
75
 
76
  # --- 3. สร้างหน้าเว็บ Gradio (แก้ไข) ---
@@ -80,21 +78,21 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
80
  gr.Markdown(
81
  """
82
  # 📈 Stock News Sentiment Analyzer
83
- Enter a stock symbol (e.g., 'AAPL', 'NVDA', 'TSLA') to get recent news
84
- and analyze its sentiment using `project-aps/finbert-finetune`.
85
  """
86
  )
87
 
88
  # --- ส่วน Input (แก้ไข) ---
89
  with gr.Row():
90
  keyword_input = gr.Textbox(
91
- label="Enter Stock Symbol",
92
- placeholder="e.g., AAPL, NVDA, TSLA...",
93
  scale=3
94
  )
95
  search_button = gr.Button("Analyze Sentiment", variant="primary", scale=1)
96
 
97
- # --- ส่วน Output (แก้ไข: ม 2 ส่วน) ---
98
  summary_output = gr.Markdown(
99
  label="Overall Sentiment Summary"
100
  )
@@ -104,7 +102,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
104
 
105
  # --- 4. เชื่อมปุ่มกับฟังก์ชัน (แก้ไข) ---
106
  search_button.click(
107
- fn=process_symbol_search,
108
  inputs=[keyword_input],
109
  outputs=[summary_output, output_markdown] # <--- ชี้ไปที่ 2 Output
110
  )
 
2
  import gradio as gr
3
  import pandas as pd
4
  from yahoo_finance_scraper import YahooFinanceScraper
5
+ from sentiment_analyzer import SentimentAnalyzer
6
 
7
  # --- 1. โหลดโมเดลและ Scraper ---
8
  try:
9
  scraper = YahooFinanceScraper()
10
+ analyzer = SentimentAnalyzer()
11
  print("Scraper and Analyzer are ready.")
12
  except Exception as e:
13
  print(f"Error loading models: {e}")
14
  scraper = None
15
  analyzer = None
16
 
17
+ # --- 2. ฟังก์ชันหลัก (แก้ไข: กลับไปใช้ search_news) ---
18
+ def process_keyword_search(keyword): # <--- เปลี่ยนชื่อกลับ
19
  """
20
+ ค้นหาข่าวด้วย Keyword -> วิเคราะห์ Sentiment -> สร้าง Markdown 2 ส่วน
21
  """
22
  if not scraper or not analyzer:
23
  return "Models failed to load. Check logs.", "" # คืนค่า 2 ส่วน
24
 
25
+ if not keyword:
26
+ return "Please enter a keyword or symbol.", ""
27
 
28
+ print(f"Searching for keyword: {keyword}")
29
 
30
+ # <--- 3. กลับไปใช้ search_news (Google) ที่หาข่าตรงประเด็น ---
31
+ news_list = scraper.search_news(keyword, max_articles=10)
32
 
33
  if not news_list:
34
+ return f"No recent news found for: {keyword}", "" # คืนค่า 2 ส่วน
35
 
36
  # <--- 4. วิเคราะห์ Sentiment ---
37
  analyzed_list = analyzer.analyze_batch(news_list)
 
57
  markdown_output_lines.append(f"### 📰 {item['title']}")
58
  markdown_output_lines.append(f"*(Published: {date_str})*")
59
 
60
+ # <--- 7. เพิ่ม Sentiment (FinBERT วิเคราะหจากหัวข้อข่าว) ---
61
  sentiment_label = item['sentiment'].capitalize()
62
  sentiment_score = item['score'] * 100
63
  markdown_output_lines.append(f"\n**Sentiment: {sentiment_label}** ({sentiment_score:.1f}%)")
64
 
65
+ # --- (เราไม่แสดง item['summary'] เพราะมันคือหัวข้อข่าวซ้ำ) ---
 
66
 
67
  markdown_output_lines.append(f"\n[Read Full Article]({item['link']})")
68
  markdown_output_lines.append("\n---")
69
 
70
  main_output_text = "\n".join(markdown_output_lines)
71
 
 
72
  return summary_text, main_output_text
73
 
74
  # --- 3. สร้างหน้าเว็บ Gradio (แก้ไข) ---
 
78
  gr.Markdown(
79
  """
80
  # 📈 Stock News Sentiment Analyzer
81
+ Enter a keyword or stock symbol (e.g., 'AAPL', 'TSLA', 'Inflation')
82
+ to get recent news and analyze its sentiment using FinBERT.
83
  """
84
  )
85
 
86
  # --- ส่วน Input (แก้ไข) ---
87
  with gr.Row():
88
  keyword_input = gr.Textbox(
89
+ label="Enter Keyword or Symbol",
90
+ placeholder="e.g., AAPL, NVIDIA, Inflation...",
91
  scale=3
92
  )
93
  search_button = gr.Button("Analyze Sentiment", variant="primary", scale=1)
94
 
95
+ # --- ส่วน Output (เหมือนเดิม 2 ส่วน) ---
96
  summary_output = gr.Markdown(
97
  label="Overall Sentiment Summary"
98
  )
 
102
 
103
  # --- 4. เชื่อมปุ่มกับฟังก์ชัน (แก้ไข) ---
104
  search_button.click(
105
+ fn=process_keyword_search, # <--- เรียกฟังก์ชัน Keyword
106
  inputs=[keyword_input],
107
  outputs=[summary_output, output_markdown] # <--- ชี้ไปที่ 2 Output
108
  )