DeepLearning101 commited on
Commit
62a9d23
·
verified ·
1 Parent(s): f160dc8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -0
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import mimetypes
4
+ import json, os
5
+ import asyncio
6
+ 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 = {
14
+ "inputs": {},
15
+ "query": user_input,
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):
64
+ return f"Error: File {file_path} not found"
65
+ mime_type, _ = mimetypes.guess_type(file_path)
66
+ with open(file_path, 'rb') as f:
67
+ async with aiohttp.ClientSession() as session:
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. @ 2024/11 </a><br></h2>"""
110
+ LINKS = """
111
+ <a href='https://github.com/Deep-Learning-101' target='_blank'>Deep Learning 101 Github</a> | <a href='http://deeplearning101.twman.org' target='_blank'>Deep Learning 101</a> | <a href='https://www.facebook.com/groups/525579498272187/' target='_blank'>台灣人工智慧社團 FB</a> | <a href='https://www.youtube.com/c/DeepLearning101' target='_blank'>YouTube</a><br>
112
+ <a href='https://reurl.cc/g6GlZX' target='_blank'>手把手帶你一起踩AI坑</a> | <a href='https://blog.twman.org/2024/11/diffusion.html' target='_blank'>ComfyUI + Stable Diffuision</a><br>
113
+ <a href='https://blog.twman.org/2024/08/LLM.html' target='_blank'>白話文手把手帶你科普 GenAI</a> | <a href='https://blog.twman.org/2024/09/LLM.html' target='_blank'>大型語言模型直接就打完收工?</a><br>
114
+ <a href='https://blog.twman.org/2023/04/GPT.html' target='_blank'>什麼是大語言模型,它是什麼?想要嗎?</a> | <a href='https://blog.twman.org/2024/07/RAG.html' target='_blank'>那些檢索增強生成要踩的坑 </a><br>
115
+ <a href='https://blog.twman.org/2021/04/ASR.html' target='_blank'>那些語音處理 (Speech Processing) 踩的坑</a> | <a href='https://blog.twman.org/2021/04/NLP.html' target='_blank'>那些自然語言處理 (Natural Language Processing, NLP) 踩的坑</a><br>
116
+ <a href='https://blog.twman.org/2024/02/asr-tts.html' target='_blank'>那些ASR和TTS可能會踩的坑</a> | <a href='https://blog.twman.org/2024/02/LLM.html' target='_blank'>那些大模型開發會踩的坑</a><br>
117
+ <a href='https://blog.twman.org/2023/07/wsl.html' target='_blank'>用PPOCRLabel來幫PaddleOCR做OCR的微調和標註</a> | <a href='https://blog.twman.org/2023/07/HugIE.html' target='_blank'>基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析</a><br>
118
+ """
119
+
120
+ # Define Gradio interface
121
+ file_input = gr.Image(label='圖片上傳', type='filepath')
122
+ user_input = gr.Textbox(label='輸入問題描述', placeholder="請輸入您的問題描述...")
123
+ output_text = gr.Textbox(label="結果輸出", lines=4)
124
+
125
+ # 範例資料
126
+ examples = [
127
+ ['DEMO/0004.jpg', '0004-51'],
128
+ ['DEMO/0005.jpg', '0005-92'],
129
+ ['DEMO/0006.jpg', '0006-281'],
130
+ ['DEMO/0008.jpg', '0008-281'],
131
+ ['DEMO/0011.jpg', '0011-108'],
132
+ ['DEMO/0013.jpg', '0013-108'],
133
+ ['DEMO/0014.jpg', '0014-108'],
134
+ ['DEMO/0015.jpg', '0015-108'],
135
+ ]
136
+
137
+ with gr.Blocks() as iface:
138
+ gr.HTML(TITLE)
139
+ gr.HTML(SUBTITLE)
140
+ gr.HTML(LINKS)
141
+ gr.Interface(
142
+ fn=handle_input,
143
+ inputs=[file_input, user_input],
144
+ outputs="text",
145
+ examples=examples,
146
+ flagging_mode="never" # 更新此處
147
+ )
148
+
149
+ iface.launch()