Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,42 +12,46 @@ character_dict = {c["id"]: c for c in characters}
|
|
12 |
|
13 |
# Hugging Face Inference API
|
14 |
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Hàm phản hồi
|
18 |
def respond(message, history, character_id, max_tokens, temperature, top_p):
|
19 |
char = character_dict[character_id]
|
20 |
system_message = char["persona_prompt"]
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
for user_msg, bot_msg in history:
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
# Gọi API
|
29 |
-
response = client.
|
30 |
-
|
31 |
-
|
32 |
temperature=temperature,
|
33 |
top_p=top_p,
|
34 |
-
do_sample=True,
|
35 |
)
|
36 |
|
37 |
-
reply = response.strip()
|
38 |
history.append((message, reply))
|
39 |
return history, history
|
40 |
|
41 |
-
# Hiển thị tên nhân vật
|
42 |
def format_label(c):
|
43 |
return f"{c['name']} ({c['personality']}, {c['appearance']}, {c['voice']})"
|
44 |
|
45 |
char_choices = [(format_label(c), c["id"]) for c in characters]
|
46 |
|
47 |
-
# Gradio
|
48 |
demo = gr.ChatInterface(
|
49 |
fn=respond,
|
50 |
-
chatbot=gr.Chatbot(label="Trò chuyện", type="tuples"), #
|
51 |
additional_inputs=[
|
52 |
gr.Dropdown(choices=char_choices, value=characters[0]["id"], label="Chọn nhân vật"),
|
53 |
gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
|
|
|
12 |
|
13 |
# Hugging Face Inference API
|
14 |
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
15 |
+
if token is None:
|
16 |
+
raise ValueError("Bạn cần đặt biến môi trường HUGGINGFACEHUB_API_TOKEN để gọi API.")
|
17 |
+
|
18 |
+
# Dùng model Zephyr đã được deploy sẵn
|
19 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=token)
|
20 |
|
21 |
# Hàm phản hồi
|
22 |
def respond(message, history, character_id, max_tokens, temperature, top_p):
|
23 |
char = character_dict[character_id]
|
24 |
system_message = char["persona_prompt"]
|
25 |
|
26 |
+
# Tạo messages theo định dạng chat
|
27 |
+
messages = [{"role": "system", "content": system_message}]
|
28 |
for user_msg, bot_msg in history:
|
29 |
+
messages.append({"role": "user", "content": user_msg})
|
30 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
31 |
+
messages.append({"role": "user", "content": message})
|
32 |
|
33 |
# Gọi API
|
34 |
+
response = client.chat_completion(
|
35 |
+
messages=messages,
|
36 |
+
max_tokens=max_tokens,
|
37 |
temperature=temperature,
|
38 |
top_p=top_p,
|
|
|
39 |
)
|
40 |
|
41 |
+
reply = response.choices[0].message.content.strip()
|
42 |
history.append((message, reply))
|
43 |
return history, history
|
44 |
|
45 |
+
# Hiển thị tên nhân vật
|
46 |
def format_label(c):
|
47 |
return f"{c['name']} ({c['personality']}, {c['appearance']}, {c['voice']})"
|
48 |
|
49 |
char_choices = [(format_label(c), c["id"]) for c in characters]
|
50 |
|
51 |
+
# Giao diện Gradio
|
52 |
demo = gr.ChatInterface(
|
53 |
fn=respond,
|
54 |
+
chatbot=gr.Chatbot(label="Trò chuyện", type="tuples"), # vẫn dùng tuples để giữ format cũ
|
55 |
additional_inputs=[
|
56 |
gr.Dropdown(choices=char_choices, value=characters[0]["id"], label="Chọn nhân vật"),
|
57 |
gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
|