Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,43 @@ import aiohttp
|
|
4 |
import asyncio
|
5 |
import json
|
6 |
from functools import lru_cache
|
|
|
|
|
7 |
|
|
|
|
|
8 |
LLM_API = os.environ.get("LLM_API")
|
9 |
LLM_URL = os.environ.get("LLM_URL")
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
@lru_cache(maxsize=32)
|
14 |
async def send_chat_message(LLM_URL, LLM_API, user_input):
|
@@ -67,7 +99,6 @@ def run_sync(user_input):
|
|
67 |
print(f"Running sync with input: {user_input}")
|
68 |
return asyncio.run(handle_input(user_input))
|
69 |
|
70 |
-
# 定義反饋處理函數
|
71 |
def save_feedback(user_input, response, feedback_type, improvement):
|
72 |
feedback = {
|
73 |
"user_input": user_input,
|
@@ -76,12 +107,47 @@ def save_feedback(user_input, response, feedback_type, improvement):
|
|
76 |
"improvement": improvement
|
77 |
}
|
78 |
print(f"Saving feedback: {feedback}")
|
79 |
-
#
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
return "感謝您的反饋!"
|
82 |
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
examples = [
|
86 |
["MAX 帳號刪除關戶後,又重新註冊 MAX 後要怎辦?"],
|
87 |
["手機APP怎麼操作掛單交易?"],
|
@@ -89,38 +155,35 @@ examples = [
|
|
89 |
["新台幣入金要怎操作"]
|
90 |
]
|
91 |
|
92 |
-
TITLE = """<h1 align="center">Large Language Model (LLM) Playground 💬 <a href='https://support.maicoin.com/zh-TW/support/home' target='_blank'>Cryptocurrency Exchange FAQ</a></h1>"""
|
93 |
-
SUBTITLE = """<h2 align="center"><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/06 </a><br></h2>"""
|
94 |
-
LINKS = """<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> | <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> | <a href='https://blog.twman.org/2023/04/GPT.html' target='_blank'>什麼是大語言模型,它是什麼?想要嗎?</a><br>
|
95 |
-
<a href='https://blog.twman.org/2023/07/wsl.html' target='_blank'>用PaddleOCR的PPOCRLabel來微調醫療診斷書和收據</a> | <a href='https://blog.twman.org/2023/07/HugIE.html' target='_blank'>基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析</a><br>"""
|
96 |
-
|
97 |
with gr.Blocks() as iface:
|
98 |
gr.HTML(TITLE)
|
99 |
gr.HTML(SUBTITLE)
|
100 |
gr.HTML(LINKS)
|
101 |
with gr.Row():
|
102 |
chatbot = gr.Chatbot()
|
|
|
103 |
with gr.Row():
|
104 |
user_input = gr.Textbox(label='輸入您的問題', placeholder="在此輸入問題...")
|
105 |
submit_button = gr.Button("送出")
|
|
|
106 |
gr.Examples(examples=examples, inputs=user_input)
|
|
|
107 |
with gr.Row():
|
108 |
like_button = gr.Button("👍")
|
109 |
dislike_button = gr.Button("👎")
|
110 |
improvement_input = gr.Textbox(label='請輸入改進建議', placeholder='請輸入如何改進模型回應的建議')
|
|
|
111 |
with gr.Row():
|
112 |
feedback_output = gr.Textbox(label='反饋結果', interactive=False)
|
|
|
|
|
|
|
113 |
|
114 |
def chat(user_input, history):
|
115 |
-
response =
|
116 |
history.append((user_input, response))
|
117 |
return history, history
|
118 |
|
119 |
-
def handle_feedback(response, feedback_type, improvement):
|
120 |
-
global last_user_input
|
121 |
-
feedback_message = save_feedback(last_user_input, response, feedback_type, improvement)
|
122 |
-
return feedback_message
|
123 |
-
|
124 |
submit_button.click(fn=chat, inputs=[user_input, chatbot], outputs=[chatbot, chatbot])
|
125 |
|
126 |
like_button.click(
|
@@ -135,4 +198,6 @@ with gr.Blocks() as iface:
|
|
135 |
outputs=feedback_output
|
136 |
)
|
137 |
|
|
|
|
|
138 |
iface.launch()
|
|
|
4 |
import asyncio
|
5 |
import json
|
6 |
from functools import lru_cache
|
7 |
+
from datasets import Dataset, DatasetDict, load_dataset
|
8 |
+
from huggingface_hub import HfFolder
|
9 |
|
10 |
+
# 從環境變量中獲取 Hugging Face API 令牌和其他配置
|
11 |
+
HF_API_TOKEN = os.environ.get("Feedback_API_TOKEN")
|
12 |
LLM_API = os.environ.get("LLM_API")
|
13 |
LLM_URL = os.environ.get("LLM_URL")
|
14 |
+
USER_ID = "HuggingFace Space"
|
15 |
+
DATASET_NAME = os.environ.get("DATASET_NAME")
|
16 |
+
|
17 |
+
# 確保令牌不為空
|
18 |
+
if HF_API_TOKEN is None:
|
19 |
+
raise ValueError("HF_API_TOKEN 環境變量未設置。請在 Hugging Face Space 的設置中添加該環境變量。")
|
20 |
+
|
21 |
+
# 設置 Hugging Face API 令牌
|
22 |
+
HfFolder.save_token(HF_API_TOKEN)
|
23 |
+
|
24 |
+
# 定義數據集特徵
|
25 |
+
features = {
|
26 |
+
"user_input": "string",
|
27 |
+
"response": "string",
|
28 |
+
"feedback_type": "string",
|
29 |
+
"improvement": "string"
|
30 |
+
}
|
31 |
+
|
32 |
+
# 加載或創建數據集
|
33 |
+
try:
|
34 |
+
dataset = load_dataset(DATASET_NAME)
|
35 |
+
except:
|
36 |
+
dataset = DatasetDict({
|
37 |
+
"feedback": Dataset.from_dict({
|
38 |
+
"user_input": [],
|
39 |
+
"response": [],
|
40 |
+
"feedback_type": [],
|
41 |
+
"improvement": []
|
42 |
+
})
|
43 |
+
})
|
44 |
|
45 |
@lru_cache(maxsize=32)
|
46 |
async def send_chat_message(LLM_URL, LLM_API, user_input):
|
|
|
99 |
print(f"Running sync with input: {user_input}")
|
100 |
return asyncio.run(handle_input(user_input))
|
101 |
|
|
|
102 |
def save_feedback(user_input, response, feedback_type, improvement):
|
103 |
feedback = {
|
104 |
"user_input": user_input,
|
|
|
107 |
"improvement": improvement
|
108 |
}
|
109 |
print(f"Saving feedback: {feedback}")
|
110 |
+
# Append to the dataset
|
111 |
+
new_data = {
|
112 |
+
"user_input": [user_input],
|
113 |
+
"response": [response],
|
114 |
+
"feedback_type": [feedback_type],
|
115 |
+
"improvement": [improvement]
|
116 |
+
}
|
117 |
+
global dataset
|
118 |
+
dataset = Dataset.from_dict({
|
119 |
+
"user_input": dataset["feedback"]["user_input"] + [user_input],
|
120 |
+
"response": dataset["feedback"]["response"] + [response],
|
121 |
+
"feedback_type": dataset["feedback"]["feedback_type"] + [feedback_type],
|
122 |
+
"improvement": dataset["feedback"]["improvement"] + [improvement],
|
123 |
+
})
|
124 |
+
dataset.push_to_hub(DATASET_NAME)
|
125 |
+
|
126 |
+
def handle_feedback(response, feedback_type, improvement):
|
127 |
+
global last_user_input
|
128 |
+
save_feedback(last_user_input, response, feedback_type, improvement)
|
129 |
return "感謝您的反饋!"
|
130 |
|
131 |
+
def handle_user_input(user_input):
|
132 |
+
print(f"User input: {user_input}")
|
133 |
+
global last_user_input
|
134 |
+
last_user_input = user_input # 保存最新的用戶輸入
|
135 |
+
return run_sync(user_input)
|
136 |
+
|
137 |
+
# 讀取並顯示反饋內容的函數
|
138 |
+
def show_feedback():
|
139 |
+
try:
|
140 |
+
feedbacks = dataset["feedback"].to_pandas().to_dict(orient="records")
|
141 |
+
return feedbacks
|
142 |
+
except Exception as e:
|
143 |
+
return f"Error: {e}"
|
144 |
+
|
145 |
+
TITLE = """<h1 align="center">Large Language Model (LLM) Playground 💬 <a href='https://support.maicoin.com/zh-TW/support/home' target='_blank'>Cryptocurrency Exchange FAQ</a></h1>"""
|
146 |
+
SUBTITLE = """<h2 align="center"><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/06 </a><br></h2>"""
|
147 |
+
LINKS = """<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> | <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> | <a href='https://blog.twman.org/2023/04/GPT.html' target='_blank'>什麼是大語言模型,它是什麼?想要嗎?</a><br>
|
148 |
+
<a href='https://blog.twman.org/2023/07/wsl.html' target='_blank'>用PaddleOCR的PPOCRLabel來微調醫療診斷書和收據</a> | <a href='https://blog.twman.org/2023/07/HugIE.html' target='_blank'>基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析</a><br>"""
|
149 |
+
|
150 |
+
# 添加示例
|
151 |
examples = [
|
152 |
["MAX 帳號刪除關戶後,又重新註冊 MAX 後要怎辦?"],
|
153 |
["手機APP怎麼操作掛單交易?"],
|
|
|
155 |
["新台幣入金要怎操作"]
|
156 |
]
|
157 |
|
|
|
|
|
|
|
|
|
|
|
158 |
with gr.Blocks() as iface:
|
159 |
gr.HTML(TITLE)
|
160 |
gr.HTML(SUBTITLE)
|
161 |
gr.HTML(LINKS)
|
162 |
with gr.Row():
|
163 |
chatbot = gr.Chatbot()
|
164 |
+
|
165 |
with gr.Row():
|
166 |
user_input = gr.Textbox(label='輸入您的問題', placeholder="在此輸入問題...")
|
167 |
submit_button = gr.Button("送出")
|
168 |
+
|
169 |
gr.Examples(examples=examples, inputs=user_input)
|
170 |
+
|
171 |
with gr.Row():
|
172 |
like_button = gr.Button("👍")
|
173 |
dislike_button = gr.Button("👎")
|
174 |
improvement_input = gr.Textbox(label='請輸入改進建議', placeholder='請輸入如何改進模型回應的建議')
|
175 |
+
|
176 |
with gr.Row():
|
177 |
feedback_output = gr.Textbox(label='反饋結果', interactive=False)
|
178 |
+
with gr.Row():
|
179 |
+
show_feedback_button = gr.Button("查看所有反饋")
|
180 |
+
feedback_display = gr.JSON(label='所有反饋')
|
181 |
|
182 |
def chat(user_input, history):
|
183 |
+
response = handle_user_input(user_input)
|
184 |
history.append((user_input, response))
|
185 |
return history, history
|
186 |
|
|
|
|
|
|
|
|
|
|
|
187 |
submit_button.click(fn=chat, inputs=[user_input, chatbot], outputs=[chatbot, chatbot])
|
188 |
|
189 |
like_button.click(
|
|
|
198 |
outputs=feedback_output
|
199 |
)
|
200 |
|
201 |
+
show_feedback_button.click(fn=show_feedback, outputs=feedback_display)
|
202 |
+
|
203 |
iface.launch()
|