Update app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,10 @@ LLM_URL = os.environ.get("LLM_URL")
|
|
| 21 |
USER_ID = "HuggingFace Space"
|
| 22 |
|
| 23 |
async def send_chat_message(LLM_URL, LLM_API, category, file_url):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
payload = {
|
| 25 |
"inputs": {},
|
| 26 |
"query": category,
|
|
@@ -37,61 +41,54 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_url):
|
|
| 37 |
}
|
| 38 |
|
| 39 |
answer = ""
|
| 40 |
-
|
| 41 |
-
async with
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# data = await response.json()
|
| 89 |
-
# return data.get("thought", "Error: No thought in response")
|
| 90 |
-
# except Exception as e:
|
| 91 |
-
# return f"Error: Failed to parse JSON - {e}"
|
| 92 |
-
|
| 93 |
-
# except Exception as e:
|
| 94 |
-
# return f"Error: Unexpected exception - {e}"
|
| 95 |
|
| 96 |
async def upload_file(LLM_URL, LLM_API, file_path, user_id):
|
| 97 |
if not os.path.exists(file_path):
|
|
|
|
| 21 |
USER_ID = "HuggingFace Space"
|
| 22 |
|
| 23 |
async def send_chat_message(LLM_URL, LLM_API, category, file_url):
|
| 24 |
+
print(f"--- 開始請求 ---")
|
| 25 |
+
print(f"URL: {LLM_URL}/chat-messages")
|
| 26 |
+
print(f"File URL: {file_url}") # 檢查這裡產生的 URL 是否真的公開可讀取
|
| 27 |
+
|
| 28 |
payload = {
|
| 29 |
"inputs": {},
|
| 30 |
"query": category,
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
answer = ""
|
| 44 |
+
try:
|
| 45 |
+
async with aiohttp.ClientSession() as session:
|
| 46 |
+
async with session.post(
|
| 47 |
+
f"{LLM_URL}/chat-messages",
|
| 48 |
+
headers={
|
| 49 |
+
"Authorization": f"Bearer {LLM_API}",
|
| 50 |
+
"Content-Type": "application/json"
|
| 51 |
+
},
|
| 52 |
+
json=payload
|
| 53 |
+
) as response:
|
| 54 |
+
|
| 55 |
+
# [DEBUG 1] 檢查狀態碼
|
| 56 |
+
print(f"HTTP Status: {response.status}")
|
| 57 |
+
|
| 58 |
+
if response.status != 200:
|
| 59 |
+
# 如果出錯,讀取錯誤訊息並回傳
|
| 60 |
+
error_text = await response.text()
|
| 61 |
+
print(f"API Error Response: {error_text}")
|
| 62 |
+
return f"API Error {response.status}: {error_text}"
|
| 63 |
+
|
| 64 |
+
# [DEBUG 2] 逐行監看回傳內容
|
| 65 |
+
async for line_bytes in response.content:
|
| 66 |
+
line = line_bytes.decode("utf-8").strip()
|
| 67 |
+
|
| 68 |
+
# 印出原始資料 (Debug 用,確認有東西回來)
|
| 69 |
+
if line:
|
| 70 |
+
print(f"Raw Line: {line}")
|
| 71 |
+
|
| 72 |
+
if line.startswith("data: "):
|
| 73 |
+
try:
|
| 74 |
+
data = json.loads(line[6:])
|
| 75 |
+
if "answer" in data:
|
| 76 |
+
answer += data["answer"]
|
| 77 |
+
# print(f"Current Answer chunk: {data['answer']}") # 選用:即時看片段
|
| 78 |
+
if "error" in data:
|
| 79 |
+
print(f"Data Error: {data}") # Dify 有時會在 data 裡回傳 error
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"JSON Parse Error: {e} | Content: {line}")
|
| 82 |
+
continue
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Request Exception: {e}")
|
| 86 |
+
return f"System Error: {str(e)}"
|
| 87 |
+
|
| 88 |
+
if not answer:
|
| 89 |
+
print("警告: 請求結束但 answer 為空")
|
| 90 |
+
|
| 91 |
+
return answer or "No answer returned (Empty Response)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
async def upload_file(LLM_URL, LLM_API, file_path, user_id):
|
| 94 |
if not os.path.exists(file_path):
|