DeepLearning101 commited on
Commit
88a0eaa
·
verified ·
1 Parent(s): c7d2968

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -63
app.py CHANGED
@@ -7,7 +7,7 @@ import aiohttp
7
 
8
  LLM_API = os.environ.get("LLM_API")
9
  LLM_URL = os.environ.get("LLM_URL")
10
- USER_ID = "HuggingFace Space" # Placeholder user ID
11
 
12
  async def send_chat_message(LLM_URL, LLM_API, user_input, file_id):
13
  payload = {
@@ -16,48 +16,32 @@ async def send_chat_message(LLM_URL, LLM_API, user_input, file_id):
16
  "response_mode": "streaming",
17
  "conversation_id": "",
18
  "user": USER_ID,
19
- "files": [
20
- {
21
- "type": "image",
22
- "transfer_method": "local_file",
23
- "upload_file_id": file_id
24
- }
25
- ]
26
  }
27
- print("Sending chat message payload:", payload) # Debug information
28
  async with aiohttp.ClientSession() as session:
29
  async with session.post(
30
  f"{LLM_URL}/chat-messages",
31
  headers={"Authorization": f"Bearer {LLM_API}"},
32
  json=payload
33
  ) as response:
34
- print("Request URL:", f"{LLM_URL}/chat-messages")
35
- print("Response status code:", response.status)
36
-
37
  if response.status == 404:
38
  return "Error: Endpoint not found (404)"
39
-
40
  last_thought = None
41
  async for line in response.content:
42
  if line:
43
  try:
44
- # 去掉前面的 "data: " 字串並解析 JSON
45
- line_data = json.loads(line.decode("utf-8").replace("data: ", ""))
46
- print("Line data:", line_data) # Debug: 輸出每行的資料內容
47
-
48
- # 提取含有 `thought` 或 `answer` 的資料
49
- if line_data.get("data", {}).get("outputs", {}).get("answer"):
50
- last_thought = line_data["data"]["outputs"]["answer"]
51
- break # 找到答案後退出迴圈
52
- except (IndexError, json.JSONDecodeError) as e:
53
- print("Error parsing line:", e) # Debug: 輸出解析錯誤訊息
54
  continue
55
-
56
- if last_thought:
57
- return last_thought.strip()
58
- else:
59
- return "Error: No thought or answer found in the response"
60
-
61
 
62
  async def upload_file(LLM_URL, LLM_API, file_path, user_id):
63
  if not os.path.exists(file_path):
@@ -68,43 +52,30 @@ async def upload_file(LLM_URL, LLM_API, file_path, user_id):
68
  form_data = aiohttp.FormData()
69
  form_data.add_field('file', f, filename=file_path, content_type=mime_type)
70
  form_data.add_field('user', user_id)
71
-
72
  async with session.post(
73
  f"{LLM_URL}/files/upload",
74
  headers={"Authorization": f"Bearer {LLM_API}"},
75
  data=form_data
76
  ) as response:
77
- print("Upload response status code:", response.status) # Debug information
78
  if response.status == 404:
79
- return "Error: Endpoint not found (404)"
80
-
81
- response_text = await response.text()
82
- print("Raw upload response text:", response_text) # Debug information
83
-
84
  try:
85
- response_json = json.loads(response_text)
86
- file_id = response_json.get("id")
87
- if file_id:
88
- return response_json
89
- else:
90
- return "Error: No file ID returned in upload response"
91
  except json.JSONDecodeError:
92
- return "Error: Invalid JSON response"
93
 
94
  async def handle_input(file_path, user_input):
95
  upload_response = await upload_file(LLM_URL, LLM_API, file_path, USER_ID)
96
- print("Upload response:", upload_response) # Debug information
97
  if isinstance(upload_response, str) and "Error" in upload_response:
98
  return upload_response
99
- file_id = upload_response.get("id") # Extract file ID from the response
100
  if not file_id:
101
- return "Error: No file ID returned from upload"
102
-
103
- chat_response = await send_chat_message(LLM_URL, LLM_API, user_input, file_id)
104
- print("Chat response:", chat_response) # Debug information
105
- return chat_response
106
 
107
- # 定義界面標題和描述
108
  TITLE = """<h1>Multimodal RAG Playground 💬 輸入工地照片,生成工地場景及相關法規和缺失描述</h1>"""
109
  SUBTITLE = """<h2><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D.</a> | <a href='https://blog.twman.org/p/deeplearning101.html' target='_blank'>手把手帶你一起踩AI坑</a><br></h2>"""
110
  LINKS = """
@@ -123,12 +94,6 @@ LINKS = """
123
  <a href='https://blog.twman.org/2023/07/HugIE.html' target='_blank'>基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析</a><br>
124
  """
125
 
126
- # Define Gradio interface
127
- file_input = gr.Image(label='圖片上傳', type='filepath')
128
- user_input = gr.Textbox(label='輸入問題描述', value="分析一下這張工地場景照片", placeholder="請輸入您的問題描述...")
129
- output_text = gr.Textbox(label="結果輸出", lines=4)
130
-
131
- # # 範例資料
132
  examples = [
133
  ['DEMO/DEMO_0004.jpg', '0004-51'],
134
  ['DEMO/DEMO_0005.jpg', '0005-92'],
@@ -137,16 +102,30 @@ examples = [
137
  ['DEMO/DEMO_0011.jpg', '0011-108'],
138
  ]
139
 
140
- with gr.Blocks() as iface:
141
  gr.HTML(TITLE)
142
  gr.HTML(SUBTITLE)
143
  gr.HTML(LINKS)
144
- gr.Interface(
 
 
 
 
 
 
 
 
 
145
  fn=handle_input,
146
- inputs=[file_input, user_input],
147
- outputs="text",
 
 
 
148
  examples=examples,
149
- flagging_mode="never" # 更新此處
 
 
150
  )
151
 
152
- iface.launch()
 
7
 
8
  LLM_API = os.environ.get("LLM_API")
9
  LLM_URL = os.environ.get("LLM_URL")
10
+ USER_ID = "HuggingFace Space"
11
 
12
  async def send_chat_message(LLM_URL, LLM_API, user_input, file_id):
13
  payload = {
 
16
  "response_mode": "streaming",
17
  "conversation_id": "",
18
  "user": USER_ID,
19
+ "files": [{
20
+ "type": "image",
21
+ "transfer_method": "local_file",
22
+ "upload_file_id": file_id
23
+ }]
 
 
24
  }
25
+
26
  async with aiohttp.ClientSession() as session:
27
  async with session.post(
28
  f"{LLM_URL}/chat-messages",
29
  headers={"Authorization": f"Bearer {LLM_API}"},
30
  json=payload
31
  ) as response:
 
 
 
32
  if response.status == 404:
33
  return "Error: Endpoint not found (404)"
 
34
  last_thought = None
35
  async for line in response.content:
36
  if line:
37
  try:
38
+ data = json.loads(line.decode("utf-8").replace("data: ", ""))
39
+ if data.get("data", {}).get("outputs", {}).get("answer"):
40
+ last_thought = data["data"]["outputs"]["answer"]
41
+ break
42
+ except Exception:
 
 
 
 
 
43
  continue
44
+ return last_thought.strip() if last_thought else "Error: No answer found."
 
 
 
 
 
45
 
46
  async def upload_file(LLM_URL, LLM_API, file_path, user_id):
47
  if not os.path.exists(file_path):
 
52
  form_data = aiohttp.FormData()
53
  form_data.add_field('file', f, filename=file_path, content_type=mime_type)
54
  form_data.add_field('user', user_id)
 
55
  async with session.post(
56
  f"{LLM_URL}/files/upload",
57
  headers={"Authorization": f"Bearer {LLM_API}"},
58
  data=form_data
59
  ) as response:
 
60
  if response.status == 404:
61
+ return "Error: Upload endpoint not found"
62
+ text = await response.text()
 
 
 
63
  try:
64
+ json_resp = json.loads(text)
65
+ return json_resp
 
 
 
 
66
  except json.JSONDecodeError:
67
+ return "Error: Upload returned invalid JSON"
68
 
69
  async def handle_input(file_path, user_input):
70
  upload_response = await upload_file(LLM_URL, LLM_API, file_path, USER_ID)
 
71
  if isinstance(upload_response, str) and "Error" in upload_response:
72
  return upload_response
73
+ file_id = upload_response.get("id")
74
  if not file_id:
75
+ return "Error: No file ID from upload"
76
+ return await send_chat_message(LLM_URL, LLM_API, user_input, file_id)
 
 
 
77
 
78
+ # --- Gradio UI 設定 --- 定義界面標題和描述
79
  TITLE = """<h1>Multimodal RAG Playground 💬 輸入工地照片,生成工地場景及相關法規和缺失描述</h1>"""
80
  SUBTITLE = """<h2><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D.</a> | <a href='https://blog.twman.org/p/deeplearning101.html' target='_blank'>手把手帶你一起踩AI坑</a><br></h2>"""
81
  LINKS = """
 
94
  <a href='https://blog.twman.org/2023/07/HugIE.html' target='_blank'>基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析</a><br>
95
  """
96
 
 
 
 
 
 
 
97
  examples = [
98
  ['DEMO/DEMO_0004.jpg', '0004-51'],
99
  ['DEMO/DEMO_0005.jpg', '0005-92'],
 
102
  ['DEMO/DEMO_0011.jpg', '0011-108'],
103
  ]
104
 
105
+ with gr.Blocks() as demo:
106
  gr.HTML(TITLE)
107
  gr.HTML(SUBTITLE)
108
  gr.HTML(LINKS)
109
+
110
+ with gr.Row():
111
+ image_input = gr.Image(label='📷 上傳照片', type='filepath')
112
+ text_input = gr.Textbox(label='💬 輸入問題描述', value="分析一下這張工地場景照片")
113
+
114
+ output_box = gr.Textbox(label="📝 回應結果", lines=8)
115
+
116
+ submit_button = gr.Button("🚀 開始分析")
117
+
118
+ submit_button.click(
119
  fn=handle_input,
120
+ inputs=[image_input, text_input],
121
+ outputs=[output_box]
122
+ )
123
+
124
+ gr.Examples(
125
  examples=examples,
126
+ inputs=[image_input, text_input],
127
+ outputs=[output_box],
128
+ label="點擊以下範例自動帶入"
129
  )
130
 
131
+ demo.launch()