Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,146 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def respond(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
|
|
|
| 28 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import time
|
| 4 |
|
| 5 |
+
# Khởi tạo client
|
|
|
|
|
|
|
| 6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 7 |
|
| 8 |
+
# CSS tùy chỉnh để làm đẹp giao diện
|
| 9 |
+
custom_css = """
|
| 10 |
+
.container {
|
| 11 |
+
max-width: 800px;
|
| 12 |
+
margin: auto;
|
| 13 |
+
padding: 20px;
|
| 14 |
+
}
|
| 15 |
+
.chat-window {
|
| 16 |
+
height: 600px;
|
| 17 |
+
border-radius: 10px;
|
| 18 |
+
background-color: #f5f5f5;
|
| 19 |
+
}
|
| 20 |
+
.controls {
|
| 21 |
+
margin-top: 20px;
|
| 22 |
+
padding: 15px;
|
| 23 |
+
border-radius: 8px;
|
| 24 |
+
background-color: #ffffff;
|
| 25 |
+
}
|
| 26 |
+
"""
|
| 27 |
|
| 28 |
+
def respond(message, history: list[tuple[str, str]],
|
| 29 |
+
system_message, max_tokens, temperature, top_p,
|
| 30 |
+
creativity_level, response_length):
|
| 31 |
+
|
| 32 |
+
# Xử lý mức độ sáng tạo
|
| 33 |
+
if creativity_level == "Conservative":
|
| 34 |
+
temperature = min(temperature, 0.5)
|
| 35 |
+
top_p = min(top_p, 0.8)
|
| 36 |
+
elif creativity_level == "Balanced":
|
| 37 |
+
temperature = 0.7
|
| 38 |
+
top_p = 0.9
|
| 39 |
+
else: # Creative
|
| 40 |
+
temperature = max(temperature, 0.9)
|
| 41 |
+
top_p = 0.95
|
| 42 |
|
| 43 |
+
# Điều chỉnh max_tokens dựa trên độ dài phản hồi
|
| 44 |
+
if response_length == "Short":
|
| 45 |
+
max_tokens = min(max_tokens, 256)
|
| 46 |
+
elif response_length == "Medium":
|
| 47 |
+
max_tokens = min(max_tokens, 512)
|
| 48 |
+
else: # Long
|
| 49 |
+
max_tokens = max_tokens
|
| 50 |
|
| 51 |
+
messages = [{"role": "system", "content": system_message}]
|
| 52 |
+
|
| 53 |
+
# Thêm lịch sử chat
|
| 54 |
+
for user_msg, assistant_msg in history:
|
| 55 |
+
if user_msg:
|
| 56 |
+
messages.append({"role": "user", "content": user_msg})
|
| 57 |
+
if assistant_msg:
|
| 58 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 59 |
+
|
| 60 |
+
# Thêm tin nhắn hiện tại
|
| 61 |
messages.append({"role": "user", "content": message})
|
| 62 |
+
|
| 63 |
+
# Sinh phản hồi
|
| 64 |
response = ""
|
| 65 |
+
try:
|
| 66 |
+
for message in client.chat_completion(
|
| 67 |
+
messages,
|
| 68 |
+
max_tokens=max_tokens,
|
| 69 |
+
stream=True,
|
| 70 |
+
temperature=temperature,
|
| 71 |
+
top_p=top_p,
|
| 72 |
+
):
|
| 73 |
+
token = message.choices[0].delta.content
|
| 74 |
+
response += token
|
| 75 |
+
time.sleep(0.02) # Tạo hiệu ứng đánh máy
|
| 76 |
+
yield response
|
| 77 |
+
except Exception as e:
|
| 78 |
+
yield f"Xin lỗi, đã có lỗi xảy ra: {str(e)}"
|
| 79 |
|
| 80 |
+
# Tạo giao diện
|
| 81 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 82 |
+
gr.Markdown("# 🤖 AI Chat Assistant")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column(scale=2):
|
| 86 |
+
chatbot = gr.ChatInterface(
|
| 87 |
+
respond,
|
| 88 |
+
chatbot=gr.Chatbot(height=500, container=True, show_share_button=True),
|
| 89 |
+
textbox=gr.Textbox(placeholder="Nhập tin nhắn của bạn...",
|
| 90 |
+
container=True, scale=7),
|
| 91 |
+
additional_inputs=[
|
| 92 |
+
gr.Textbox(
|
| 93 |
+
value="You are a friendly and helpful AI assistant.",
|
| 94 |
+
label="System Message",
|
| 95 |
+
lines=2
|
| 96 |
+
),
|
| 97 |
+
gr.Slider(
|
| 98 |
+
minimum=1, maximum=2048, value=512, step=1,
|
| 99 |
+
label="Max Tokens"
|
| 100 |
+
),
|
| 101 |
+
gr.Slider(
|
| 102 |
+
minimum=0.1, maximum=4.0, value=0.7, step=0.1,
|
| 103 |
+
label="Temperature"
|
| 104 |
+
),
|
| 105 |
+
gr.Slider(
|
| 106 |
+
minimum=0.1, maximum=1.0, value=0.95, step=0.05,
|
| 107 |
+
label="Top-p (nucleus sampling)"
|
| 108 |
+
),
|
| 109 |
+
gr.Radio(
|
| 110 |
+
["Conservative", "Balanced", "Creative"],
|
| 111 |
+
label="Creativity Level",
|
| 112 |
+
value="Balanced"
|
| 113 |
+
),
|
| 114 |
+
gr.Radio(
|
| 115 |
+
["Short", "Medium", "Long"],
|
| 116 |
+
label="Response Length",
|
| 117 |
+
value="Medium"
|
| 118 |
+
)
|
| 119 |
+
],
|
| 120 |
+
submit_btn="Gửi 📤",
|
| 121 |
+
retry_btn="Thử lại 🔄",
|
| 122 |
+
undo_btn="Hoàn tác ↩️",
|
| 123 |
+
clear_btn="Xóa 🗑️",
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
with gr.Column(scale=1):
|
| 127 |
+
with gr.Accordion("Hướng dẫn sử dụng", open=False):
|
| 128 |
+
gr.Markdown("""
|
| 129 |
+
### Cách sử dụng:
|
| 130 |
+
1. Nhập tin nhắn vào ô văn bản
|
| 131 |
+
2. Điều chỉnh các thông số nếu muốn:
|
| 132 |
+
- System Message: Định hướng cho AI
|
| 133 |
+
- Max Tokens: Độ dài tối đa của câu trả lời
|
| 134 |
+
- Temperature: Độ sáng tạo
|
| 135 |
+
- Top-p: Độ đa dạng của từ vựng
|
| 136 |
+
3. Chọn mức độ sáng tạo và độ dài phản hồi
|
| 137 |
+
4. Nhấn 'Gửi' để chat
|
| 138 |
+
|
| 139 |
+
### Các nút chức năng:
|
| 140 |
+
- 🔄 Thử lại: Tạo câu trả lời mới
|
| 141 |
+
- ↩️ Hoàn tác: Quay lại tin nhắn trước
|
| 142 |
+
- 🗑️ Xóa: Xóa lịch sử chat
|
| 143 |
+
""")
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
+
demo.launch(share=True, server_name="0.0.0.0")
|