Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,58 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
|
|
|
| 36 |
):
|
| 37 |
-
token =
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.
|
| 50 |
-
gr.Slider(
|
| 51 |
-
gr.Slider(
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Load danh sách nhân vật
|
| 6 |
+
with open("characters.json", "r", encoding="utf-8") as f:
|
| 7 |
+
characters = json.load(f)["characters"]
|
|
|
|
| 8 |
|
| 9 |
+
# Tạo dict tra cứu theo ID
|
| 10 |
+
character_dict = {c["id"]: c for c in characters}
|
| 11 |
|
| 12 |
+
# Hugging Face Inference API
|
| 13 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Hàm phản hồi hội thoại
|
| 16 |
+
def respond(message, history, character_id, max_tokens, temperature, top_p):
|
| 17 |
+
char = character_dict[character_id]
|
| 18 |
+
system_message = char["persona_prompt"]
|
|
|
|
| 19 |
|
| 20 |
+
messages = [{"role": "system", "content": system_message}]
|
| 21 |
+
for u, a in history:
|
| 22 |
+
if u: messages.append({"role": "user", "content": u})
|
| 23 |
+
if a: messages.append({"role": "assistant", "content": a})
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
|
| 26 |
response = ""
|
| 27 |
+
for msg in client.chat_completion(
|
|
|
|
| 28 |
messages,
|
| 29 |
max_tokens=max_tokens,
|
|
|
|
| 30 |
temperature=temperature,
|
| 31 |
top_p=top_p,
|
| 32 |
+
stream=True,
|
| 33 |
):
|
| 34 |
+
token = msg.choices[0].delta.content or ""
|
|
|
|
| 35 |
response += token
|
| 36 |
yield response
|
| 37 |
|
| 38 |
+
# Tạo danh sách lựa chọn nhân vật
|
| 39 |
+
def format_label(c):
|
| 40 |
+
return f"{c['name']} ({c['personality']}, {c['appearance']}, {c['voice']})"
|
| 41 |
|
| 42 |
+
char_choices = [(format_label(c), c["id"]) for c in characters]
|
| 43 |
+
|
| 44 |
+
# Giao diện Gradio
|
| 45 |
demo = gr.ChatInterface(
|
| 46 |
+
fn=respond,
|
| 47 |
additional_inputs=[
|
| 48 |
+
gr.Dropdown(choices=char_choices, value=characters[0]["id"], label="Chọn nhân vật"),
|
| 49 |
+
gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
+
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
],
|
| 53 |
+
title="🧠 Trợ lý ảo hoạt hình",
|
| 54 |
+
description="Chọn nhân vật hoạt hình lý tưởng để trò chuyện!",
|
| 55 |
)
|
| 56 |
|
|
|
|
| 57 |
if __name__ == "__main__":
|
| 58 |
demo.launch()
|