DeepLearning101 commited on
Commit
aa2adce
1 Parent(s): 2c8de38

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +206 -0
app.py ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import aiohttp
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):
47
+ payload = {
48
+ "inputs": {},
49
+ "query": user_input,
50
+ "response_mode": "streaming",
51
+ "conversation_id": "",
52
+ "user": USER_ID,
53
+ }
54
+ print("Sending chat message payload:", payload) # Debug information
55
+
56
+ async with aiohttp.ClientSession() as session:
57
+ try:
58
+ async with session.post(
59
+ url=f"{LLM_URL}/chat-messages",
60
+ headers={"Authorization": f"Bearer {LLM_API}"},
61
+ json=payload,
62
+ timeout=aiohttp.ClientTimeout(total=60)
63
+ ) as response:
64
+ if response.status != 200:
65
+ print(f"Error: {response.status}")
66
+ return f"Error: {response.status}"
67
+
68
+ full_response = []
69
+ async for line in response.content:
70
+ line = line.decode('utf-8').strip()
71
+ if not line:
72
+ continue
73
+ if "data: " not in line:
74
+ continue
75
+ try:
76
+ print("Received line:", line) # Debug information
77
+ data = json.loads(line.split("data: ")[1])
78
+ if "answer" in data:
79
+ full_response.append(data["answer"])
80
+ except (IndexError, json.JSONDecodeError) as e:
81
+ print(f"Error parsing line: {line}, error: {e}") # Debug information
82
+ continue
83
+
84
+ if full_response:
85
+ return ''.join(full_response).strip()
86
+ else:
87
+ return "Error: No response found in the response"
88
+ except Exception as e:
89
+ print(f"Exception: {e}")
90
+ return f"Exception: {e}"
91
+
92
+ async def handle_input(user_input):
93
+ print(f"Handling input: {user_input}")
94
+ chat_response = await send_chat_message(LLM_URL, LLM_API, user_input)
95
+ print("Chat response:", chat_response) # Debug information
96
+ return chat_response
97
+
98
+ def run_sync(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,
105
+ "response": response,
106
+ "feedback_type": feedback_type,
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["feedback"] = 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
+ print(f"Feedbacks: {feedbacks}") # Debug information
142
+ return feedbacks
143
+ except Exception as e:
144
+ print(f"Error: {e}") # Debug information
145
+ return {"error": str(e)}
146
+
147
+ TITLE = """<h1 align="center">Large Language Model (LLM) Playground 💬 <a href='https://www.cathaylife.com.tw/cathaylifeins/faq' target='_blank'> Insurance FAQ </a></h1>"""
148
+ SUBTITLE = """<h2 align="center"><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/06 </a><br></h2>"""
149
+ 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>
150
+ <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>"""
151
+
152
+ # 添加示例
153
+ examples = [
154
+ ["實支實付解釋一下?"],
155
+ ["保險天數如何計算"],
156
+ ["CaaS是什麼?"],
157
+ ["介紹機車強制險?"]
158
+ ["什麼是微型保險?"]
159
+ ]
160
+
161
+ with gr.Blocks() as iface:
162
+ gr.HTML(TITLE)
163
+ gr.HTML(SUBTITLE)
164
+ gr.HTML(LINKS)
165
+ with gr.Row():
166
+ chatbot = gr.Chatbot()
167
+
168
+ with gr.Row():
169
+ user_input = gr.Textbox(label='輸入您的問題', placeholder="在此輸入問題...")
170
+ submit_button = gr.Button("問題輸入好,請點我送出")
171
+
172
+ gr.Examples(examples=examples, inputs=user_input)
173
+
174
+ with gr.Row():
175
+ # like_button = gr.Button(" 👍 覺得答案很棒,請按我;或者直接繼續問新問題亦可")
176
+ dislike_button = gr.Button(" 👎 覺得答案待改善,請輸入改進建議,再按我送出保存")
177
+ improvement_input = gr.Textbox(label='請輸入改進建議', placeholder='請輸入如何改進模型回應的建議')
178
+
179
+ with gr.Row():
180
+ feedback_output = gr.Textbox(label='反饋結果執行狀態', interactive=False)
181
+ with gr.Row():
182
+ show_feedback_button = gr.Button("查看目前所有反饋記錄")
183
+ feedback_display = gr.JSON(label='所有反饋記錄')
184
+
185
+ def chat(user_input, history):
186
+ response = handle_user_input(user_input)
187
+ history.append((user_input, response))
188
+ return history, history
189
+
190
+ submit_button.click(fn=chat, inputs=[user_input, chatbot], outputs=[chatbot, chatbot])
191
+
192
+ # like_button.click(
193
+ # fn=lambda response, improvement: handle_feedback(response, "like", improvement),
194
+ # inputs=[chatbot, improvement_input],
195
+ # outputs=feedback_output
196
+ # )
197
+
198
+ dislike_button.click(
199
+ fn=lambda response, improvement: handle_feedback(response, "dislike", improvement),
200
+ inputs=[chatbot, improvement_input],
201
+ outputs=feedback_output
202
+ )
203
+
204
+ show_feedback_button.click(fn=show_feedback, outputs=feedback_display)
205
+
206
+ iface.launch()