DeepLearning101 commited on
Commit
8fe1c58
·
verified ·
1 Parent(s): 0231211

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -55
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
- async with aiohttp.ClientSession() as session:
41
- async with session.post(
42
- f"{LLM_URL}/chat-messages",
43
- headers={
44
- "Authorization": f"Bearer {LLM_API}",
45
- "Content-Type": "application/json"
46
- },
47
- json=payload
48
- ) as response:
49
- async for line_bytes in response.content:
50
- line = line_bytes.decode("utf-8").strip()
51
- if line.startswith("data: "):
52
- try:
53
- data = json.loads(line[6:])
54
- if "answer" in data:
55
- answer += data["answer"]
56
- except:
57
- continue
58
- return answer or "No answer returned"
59
-
60
- # async def send_chat_message(LLM_URL, LLM_API, category, file_id):
61
- # payload = {
62
- # "inputs": {},
63
- # "query": category,
64
- # "conversation_id": "",
65
- # "user": USER_ID,
66
- # "files": [
67
- # {
68
- # "type": "image",
69
- # "transfer_method": "remote_url",
70
- # "upload_file_id": file_id
71
- # }
72
- # ]
73
- # }
74
-
75
- # try:
76
- # async with aiohttp.ClientSession() as session:
77
- # async with session.post(
78
- # f"{LLM_URL}/chat-messages",
79
- # headers={"Authorization": f"Bearer {LLM_API}"},
80
- # json=payload
81
- # ) as response:
82
-
83
- # if response.status != 200:
84
- # error_text = await response.text()
85
- # return f"Error: Server returned status {response.status} - {error_text}"
86
-
87
- # try:
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):