import os import gradio as gr import aiohttp import asyncio import json import urllib.parse import traceback LLM_API = os.environ.get("LLM_API") LLM_URL = os.environ.get("LLM_URL") USER_ID = "HuggingFace Space" async def send_chat_message(LLM_URL, LLM_API, user_input): payload = { "inputs": {}, "query": user_input, "response_mode": "streaming", "conversation_id": "", "user": USER_ID, } print("Sending chat message payload:", payload) async with aiohttp.ClientSession() as session: try: async with session.post( url=f"{LLM_URL}/chat-messages", headers={"Authorization": f"Bearer {LLM_API}"}, json=payload, timeout=aiohttp.ClientTimeout(total=180) ) as response: if response.status != 200: print(f"Error: {response.status}") return f"Error: Status code {response.status}" full_response = [] async for line in response.content.iter_chunked(2048): line = line.decode('utf-8').strip() if not line or "data: " not in line: continue try: data = json.loads(line.split("data: ")[1]) if "answer" in data: decoded_answer = urllib.parse.unquote(data["answer"]) full_response.append(decoded_answer) except (IndexError, json.JSONDecodeError) as e: print(f"Skipping invalid line: {line}, error: {e}") continue if full_response: return ''.join(full_response).strip() else: return "Error: No response found in the response" except aiohttp.ClientConnectorError: return "Error: Cannot connect to the API server. Please check the URL and server status." except Exception as e: print("Exception occurred in send_chat_message:") print(traceback.format_exc()) return f"Exception: {e}" async def handle_input(user_input): print(f"Handling input: {user_input}") chat_response = await send_chat_message(LLM_URL, LLM_API, user_input) print("Chat response:", chat_response) return chat_response def run_sync(func, *args): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) result = loop.run_until_complete(func(*args)) loop.close() return result # 定義 Gradio 介面 user_input = gr.Textbox(label='請輸入您想查詢的關鍵公司名稱') examples = [ ["加密貨幣"], # ["國泰金控"], ["中華電信"], # ["台灣大哥大"], ["台積電"], # ["BlockTempo"] ] TITLE = """

Social Media Trends 💬 分析社群相關資訊,並判斷其正、負、中立等評價及趨勢 (數據大會跑很久或失敗)

""" SUBTITLE = """

TonTon Huang Ph.D. | 手把手帶你一起踩AI坑

""" LINKS = """ Deep Learning 101 Github | Deep Learning 101 | 台灣人工智慧社團 FB | YouTube
那些 AI Agent 要踩的坑:探討多種 AI 代理人工具的應用經驗與挑戰,分享實用經驗與工具推薦。
白話文手把手帶你科普 GenAI:淺顯介紹生成式人工智慧核心概念,強調硬體資源和數據的重要性。
大型語言模型直接就打完收工?:回顧 LLM 領域探索歷程,討論硬體升級對 AI 開發的重要性。
那些檢索增強生成要踩的坑:探討 RAG 技術應用與挑戰,提供實用經驗分享和工具建議。
那些大型語言模型要踩的坑:探討多種 LLM 工具的應用與挑戰,強調硬體資源的重要性。
Large Language Model,LLM:探討 LLM 的發展與應用,強調硬體資源在開發中的關鍵作用。。
ComfyUI + Stable Diffuision:深入探討影像生成與分割技術的應用,強調硬體資源的重要性。
那些ASR和TTS可能會踩的坑:探討 ASR 和 TTS 技術應用中的問題,強調數據質量的重要性。
那些自然語言處理 (Natural Language Processing, NLP) 踩的坑:分享 NLP 領域的實踐經驗,強調數據質量對模型效果的影響。
那些語音處理 (Speech Processing) 踩的坑:分享語音處理領域的實務經驗,強調資料品質對模型效果的影響。
用PPOCRLabel來幫PaddleOCR做OCR的微調和標註
基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析
""" # 使用 Gradio Blocks 設定頁面內容 with gr.Blocks() as iface: gr.HTML(TITLE) gr.HTML(SUBTITLE) gr.HTML(LINKS) gr.Interface( fn=lambda x: run_sync(handle_input, x), inputs=user_input, outputs="text", examples=examples, flagging_mode="never" # 停用範例加載 ) iface.launch()