Sooteemon commited on
Commit
d8db7a8
·
verified ·
1 Parent(s): 56e70fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -11
app.py CHANGED
@@ -5,7 +5,11 @@ from sentiment_analyzer import NewsAnalyzer # Import new class
5
  import time
6
  from collections import Counter # For counting themes/impacts
7
 
8
- # (ฟังก์ชัน get_sentiment_color และ get_impact_color ... ไม่ต้องแก้)
 
 
 
 
9
  def get_sentiment_color(sentiment):
10
  """กำหนดสีตาม sentiment"""
11
  colors = { "Positive": "🟢", "Negative": "🔴", "Neutral": "🟡" }
@@ -16,8 +20,6 @@ def get_impact_color(impact):
16
  colors = { "Opportunity": "🟢", "Risk": "🔴", "Neutral": "🟡" }
17
  return colors.get(impact, "⚪")
18
 
19
-
20
- # --- คัดลอกทับเฉพาะฟังก์ชันนี้ครับ ---
21
  def analyze_news(search_type, search_input, num_articles):
22
  """
23
  ฟังก์ชันหลักในการวิเคราะห์ข่าว
@@ -81,18 +83,16 @@ def analyze_news(search_type, search_input, num_articles):
81
  emoji = get_impact_color(impact)
82
  summary += f"- {emoji} **{impact}:** {count} รายการ ({count/total*100:.1f}%)\n"
83
 
84
- # --- (MODIFIED BLOCK) ---
85
- # ย้ายเส้นคั่นและหัวข้อ "รายละเอียด" มาไว้ใน summary
86
- summary += "\n\n---\n\n## 📰 รายละเอียดแต่ละข่าว\n\n"
87
 
88
- # Create detailed results (เริ่มต้นด้วยสตริงว่าง)
89
- detailed_results = ""
90
- # --- (END MODIFIED BLOCK) ---
91
 
92
  for i, result in enumerate(results, 1):
93
  s_emoji = get_sentiment_color(result['sentiment'])
94
  i_emoji = get_impact_color(result.get('impact', 'N/A'))
95
 
 
96
  detailed_results += f"""### {i}. {s_emoji} {result['title']}
97
  **Theme:** {result.get('theme', 'N/A')} | **Impact:** {i_emoji} {result.get('impact', 'N/A')}
98
  **Sentiment:** {result['sentiment']} | **Score:** {result['score']:.2f}
@@ -101,8 +101,9 @@ def analyze_news(search_type, search_input, num_articles):
101
  {result['summary'][:200]}{'...' if len(result['summary']) > 200 else ''}
102
  [🔗 อ่านต่อ]({result['link']})
103
  ---"""
 
104
 
105
- # Create DataFrame for table view
106
  df_data = []
107
  for result in results:
108
  df_data.append({
@@ -124,4 +125,79 @@ def analyze_news(search_type, search_input, num_articles):
124
  error_msg = f"❌ เกิดข้อผิดพลาด: {str(e)}"
125
  yield error_msg, None, error_msg
126
 
127
- # (โค้ดส่วน Interface (with gr.Blocks...) ... ไม่ต้องแก้)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import time
6
  from collections import Counter # For counting themes/impacts
7
 
8
+ # Initialize components
9
+ print("Initializing Yahoo Finance News Analyzer...")
10
+ scraper = YahooFinanceScraper()
11
+ analyzer = NewsAnalyzer() # Use new class
12
+
13
  def get_sentiment_color(sentiment):
14
  """กำหนดสีตาม sentiment"""
15
  colors = { "Positive": "🟢", "Negative": "🔴", "Neutral": "🟡" }
 
20
  colors = { "Opportunity": "🟢", "Risk": "🔴", "Neutral": "🟡" }
21
  return colors.get(impact, "⚪")
22
 
 
 
23
  def analyze_news(search_type, search_input, num_articles):
24
  """
25
  ฟังก์ชันหลักในการวิเคราะห์ข่าว
 
83
  emoji = get_impact_color(impact)
84
  summary += f"- {emoji} **{impact}:** {count} รายการ ({count/total*100:.1f}%)\n"
85
 
86
+ summary += "---"
 
 
87
 
88
+ # Create detailed results
89
+ detailed_results = "## 📰 รายละเอียดแต่ละข่าว\n\n"
 
90
 
91
  for i, result in enumerate(results, 1):
92
  s_emoji = get_sentiment_color(result['sentiment'])
93
  i_emoji = get_impact_color(result.get('impact', 'N/A'))
94
 
95
+ # --- MODIFIED: Reverted to *exact* original format + added Theme/Impact line ---
96
  detailed_results += f"""### {i}. {s_emoji} {result['title']}
97
  **Theme:** {result.get('theme', 'N/A')} | **Impact:** {i_emoji} {result.get('impact', 'N/A')}
98
  **Sentiment:** {result['sentiment']} | **Score:** {result['score']:.2f}
 
101
  {result['summary'][:200]}{'...' if len(result['summary']) > 200 else ''}
102
  [🔗 อ่านต่อ]({result['link']})
103
  ---"""
104
+ # --- End of MODIFIED block ---
105
 
106
+ # Create DataFrame for table view (Keeping new columns as they are useful)
107
  df_data = []
108
  for result in results:
109
  df_data.append({
 
125
  error_msg = f"❌ เกิดข้อผิดพลาด: {str(e)}"
126
  yield error_msg, None, error_msg
127
 
128
+ # --- UI Interface (No changes needed, keeping original titles/markdown) ---
129
+ with gr.Blocks(title="Yahoo Finance News Analyzer", theme=gr.themes.Soft()) as demo:
130
+ gr.Markdown("""
131
+ # 📈 Yahoo Finance News Analyzer
132
+ วิเคราะห์ข่าวการเงินจาก Yahoo Finance ด้วย AI แบบครบวงจร
133
+
134
+ 🔍 **ฟีเจอร์:**
135
+ - วิเคราะห์ Sentiment (ความรู้สึก)
136
+ - วิเคราะห์ Theme (หัวข้อข่าว)
137
+ - วิเคราะห์ Impact (ผลกระทบ: โอกาส หรือ ความเสี่ยง)
138
+ - ให้คะแนนความมั่นใจและคำอธิบาย
139
+ - รองรับค้นหาตาม Stock Symbol และ Keyword
140
+ """)
141
+
142
+ with gr.Row():
143
+ with gr.Column(scale=1):
144
+ search_type = gr.Radio(
145
+ choices=["Latest News", "Stock Symbol", "Keyword"],
146
+ value="Latest News",
147
+ label="ประเภทการค้นหา"
148
+ )
149
+ search_input = gr.Textbox(
150
+ label="Ticker Symbol / Keyword",
151
+ placeholder="เช่น AAPL, TSLA, AI, cryptocurrency",
152
+ info="ใส่เฉพาะเมื่อเลือก Stock Symbol หรือ Keyword"
153
+ )
154
+ num_articles = gr.Slider(
155
+ minimum=5,
156
+ maximum=20,
157
+ value=10,
158
+ step=1,
159
+ label="จำนวนข่าว"
160
+ )
161
+ analyze_btn = gr.Button("🔍 วิเคราะห์ข่าว", variant="primary", size="lg")
162
+ status = gr.Textbox(label="สถานะ", interactive=False)
163
+
164
+ with gr.Column(scale=2):
165
+ results_md = gr.Markdown("### กดปุ่ม 'วิเคราะห์ข่าว' เพื่อเริ่มต้น")
166
+
167
+ with gr.Row():
168
+ results_table = gr.Dataframe(
169
+ label="📊 ตารางสรุปผล",
170
+ wrap=True
171
+ )
172
+
173
+ gr.Markdown("""
174
+ ---
175
+ ### 💡 วิธีใช้งาน:
176
+ 1. เลือกประเภทการค้นหา (ข่าวล่าสุด / หุ้นเฉพาะ / คำค้นหา)
177
+ 2. ใส่ ticker symbol หรือ keyword (ถ้าต้องการ)
178
+ 3. เลือกจำนวนข่าวที่ต้องการวิเคราะห์
179
+ 4. กดปุ่ม "วิเคราะห์ข่าว"
180
+
181
+ ### 🎯 ตัวอย่าง Stock Symbols:
182
+ - **AAPL** - Apple Inc.
183
+ - **TSLA** - Tesla
184
+ - **NVDA** - Nvidia
185
+ - **MSFT** - Microsoft
186
+ - **GOOGL** - Google
187
+
188
+ ### 📌 หมายเหตุ:
189
+ - **Sentiment:** Positive (ดี), Negative (แย่), Neutral (กลาง)
190
+ - **Theme:** หัวข้อหลักของข่าว (เช่น ผลประกอบการ, สินค้าใหม่)
191
+ - **Impact:** ผลกระทบต่อบริษัท (🟢 Opportunity - โอกาส, 🔴 Risk - ความเสี่ยง)
192
+ """)
193
+
194
+ analyze_btn.click(
195
+ fn=analyze_news,
196
+ inputs=[search_type, search_input, num_articles],
197
+ outputs=[results_md, results_table, status]
198
+ )
199
+
200
+ # Launch app
201
+ if __name__ == "__main__":
202
+ demo.queue()
203
+ demo.launch(share=False)