Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -238,11 +238,7 @@ def setup_message_handler():
|
|
| 238 |
def handle_message(event):
|
| 239 |
user_message = event.message.text.strip()
|
| 240 |
|
| 241 |
-
#
|
| 242 |
-
if user_message == "再見":
|
| 243 |
-
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="Bye!"))
|
| 244 |
-
return
|
| 245 |
-
|
| 246 |
if user_message.lower() == "podcast":
|
| 247 |
audio_url = "https://huggingface.co/spaces/tkkbbo332/stockline/resolve/main/podcast.mp3"
|
| 248 |
line_bot_api.reply_message(
|
|
@@ -253,71 +249,30 @@ def setup_message_handler():
|
|
| 253 |
|
| 254 |
response_text = None
|
| 255 |
|
| 256 |
-
#
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
model = genai.GenerativeModel(
|
| 271 |
-
model_name="models/gemini-1.5-flash",
|
| 272 |
-
generation_config=generation_config,
|
| 273 |
-
system_instruction=system_instruction
|
| 274 |
-
)
|
| 275 |
-
completion = model.generate_content(sample_doc)
|
| 276 |
-
response_text = completion.text
|
| 277 |
-
|
| 278 |
-
# 清理暫存檔
|
| 279 |
-
try:
|
| 280 |
-
os.unlink(temp_file_path)
|
| 281 |
-
except:
|
| 282 |
-
pass
|
| 283 |
-
else:
|
| 284 |
-
print(f"查無股票代號 {user_message} 的公開說明書 (HTTP Status: {doc_data.status_code})。")
|
| 285 |
-
except Exception as e:
|
| 286 |
-
print(f"Gemini 執行出錯: {e}")
|
| 287 |
-
response_text = "Gemini AI 分析時發生錯誤,請稍後再試。"
|
| 288 |
-
# 確保清理暫存檔
|
| 289 |
-
try:
|
| 290 |
-
if 'temp_file_path' in locals():
|
| 291 |
-
os.unlink(temp_file_path)
|
| 292 |
-
except:
|
| 293 |
-
pass
|
| 294 |
-
|
| 295 |
-
# 3. 如果 Gemini 未觸發或失敗,則嘗試 yfinance 技術分析
|
| 296 |
-
if response_text is None:
|
| 297 |
-
stock_symbol = None
|
| 298 |
-
stock_name = None
|
| 299 |
-
for name, symbol in TAIWAN_STOCKS.items():
|
| 300 |
-
# 檢查名稱或代號 (例如 '2330' in '2330.TW')
|
| 301 |
-
if name in user_message or symbol.split('.')[0] in user_message:
|
| 302 |
-
stock_symbol = symbol
|
| 303 |
-
stock_name = name
|
| 304 |
-
break
|
| 305 |
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
report_part = generate_analysis_report(data_with_indicators, stock_name)
|
| 311 |
-
outlook_part = generate_market_outlook(data_with_indicators, stock_name)
|
| 312 |
-
response_text = report_part + outlook_part
|
| 313 |
|
| 314 |
-
#
|
| 315 |
if response_text is None:
|
| 316 |
response_text = (
|
| 317 |
f"無法識別您的指令。\n\n"
|
| 318 |
-
f"👉 輸入台灣上市公司【股票代號】(如: 2330) 進行 AI 深度分析。\n"
|
| 319 |
f"👉 輸入【股票名稱】(如: 台積電) 查詢即時技術指標。\n\n"
|
| 320 |
-
f"您也可以試試輸入 'podcast'
|
| 321 |
f"🔗 更多功能請訪問:\nhttps://huggingface.co/spaces/AlanRex/AITEST"
|
| 322 |
)
|
| 323 |
|
|
|
|
| 238 |
def handle_message(event):
|
| 239 |
user_message = event.message.text.strip()
|
| 240 |
|
| 241 |
+
# 處理 podcast 指令
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
if user_message.lower() == "podcast":
|
| 243 |
audio_url = "https://huggingface.co/spaces/tkkbbo332/stockline/resolve/main/podcast.mp3"
|
| 244 |
line_bot_api.reply_message(
|
|
|
|
| 249 |
|
| 250 |
response_text = None
|
| 251 |
|
| 252 |
+
# 嘗試進行股票名稱分析
|
| 253 |
+
stock_symbol = None
|
| 254 |
+
stock_name = None
|
| 255 |
+
for name, symbol in TAIWAN_STOCKS.items():
|
| 256 |
+
# 檢查名稱或代號 (例如 '2330' in '2330.TW')
|
| 257 |
+
if name in user_message or symbol.split('.')[0] in user_message:
|
| 258 |
+
stock_symbol = symbol
|
| 259 |
+
stock_name = name
|
| 260 |
+
break
|
| 261 |
+
|
| 262 |
+
if stock_symbol:
|
| 263 |
+
stock_data = get_stock_data(stock_symbol, period='6mo')
|
| 264 |
+
data_with_indicators = calculate_technical_indicators(stock_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
|
| 266 |
+
report_part = generate_analysis_report(data_with_indicators, stock_name)
|
| 267 |
+
outlook_part = generate_market_outlook(data_with_indicators, stock_name)
|
| 268 |
+
response_text = report_part + outlook_part
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
+
# 如果沒有匹配,回覆預設訊息與網址
|
| 271 |
if response_text is None:
|
| 272 |
response_text = (
|
| 273 |
f"無法識別您的指令。\n\n"
|
|
|
|
| 274 |
f"👉 輸入【股票名稱】(如: 台積電) 查詢即時技術指標。\n\n"
|
| 275 |
+
f"您也可以試試輸入 'podcast'。\n\n"
|
| 276 |
f"🔗 更多功能請訪問:\nhttps://huggingface.co/spaces/AlanRex/AITEST"
|
| 277 |
)
|
| 278 |
|