File size: 5,097 Bytes
d700023
 
d21ca4d
d700023
d21ca4d
d700023
 
d21ca4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d700023
d21ca4d
 
 
 
 
 
 
 
 
 
 
 
 
 
d700023
d21ca4d
 
 
 
 
 
 
d700023
d21ca4d
 
 
 
 
 
 
 
 
 
d700023
d21ca4d
 
d700023
d21ca4d
 
 
 
 
 
 
 
 
 
 
 
 
 
d700023
d21ca4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d700023
 
d21ca4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import gradio as gr
from huggingface_hub import InferenceClient
import time

# Khởi tạo client
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

# CSS tùy chỉnh để làm đẹp giao diện
custom_css = """
.container {
    max-width: 800px;
    margin: auto;
    padding: 20px;
}
.chat-window {
    height: 600px;
    border-radius: 10px;
    background-color: #f5f5f5;
}
.controls {
    margin-top: 20px;
    padding: 15px;
    border-radius: 8px;
    background-color: #ffffff;
}
"""

def respond(message, history: list[tuple[str, str]], 
           system_message, max_tokens, temperature, top_p,
           creativity_level, response_length):
    
    # Xử lý mức độ sáng tạo
    if creativity_level == "Conservative":
        temperature = min(temperature, 0.5)
        top_p = min(top_p, 0.8)
    elif creativity_level == "Balanced":
        temperature = 0.7
        top_p = 0.9
    else:  # Creative
        temperature = max(temperature, 0.9)
        top_p = 0.95

    # Điều chỉnh max_tokens dựa trên độ dài phản hồi
    if response_length == "Short":
        max_tokens = min(max_tokens, 256)
    elif response_length == "Medium":
        max_tokens = min(max_tokens, 512)
    else:  # Long
        max_tokens = max_tokens

    messages = [{"role": "system", "content": system_message}]
    
    # Thêm lịch sử chat
    for user_msg, assistant_msg in history:
        if user_msg:
            messages.append({"role": "user", "content": user_msg})
        if assistant_msg:
            messages.append({"role": "assistant", "content": assistant_msg})
    
    # Thêm tin nhắn hiện tại
    messages.append({"role": "user", "content": message})
    
    # Sinh phản hồi
    response = ""
    try:
        for message in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        ):
            token = message.choices[0].delta.content
            response += token
            time.sleep(0.02)  # Tạo hiệu ứng đánh máy
            yield response
    except Exception as e:
        yield f"Xin lỗi, đã có lỗi xảy ra: {str(e)}"

# Tạo giao diện
with gr.Blocks(css=custom_css) as demo:
    gr.Markdown("# 🤖 AI Chat Assistant")
    
    with gr.Row():
        with gr.Column(scale=2):
            chatbot = gr.ChatInterface(
                respond,
                chatbot=gr.Chatbot(height=500, container=True, show_share_button=True),
                textbox=gr.Textbox(placeholder="Nhập tin nhắn của bạn...", 
                                 container=True, scale=7),
                additional_inputs=[
                    gr.Textbox(
                        value="You are a friendly and helpful AI assistant.",
                        label="System Message",
                        lines=2
                    ),
                    gr.Slider(
                        minimum=1, maximum=2048, value=512, step=1,
                        label="Max Tokens"
                    ),
                    gr.Slider(
                        minimum=0.1, maximum=4.0, value=0.7, step=0.1,
                        label="Temperature"
                    ),
                    gr.Slider(
                        minimum=0.1, maximum=1.0, value=0.95, step=0.05,
                        label="Top-p (nucleus sampling)"
                    ),
                    gr.Radio(
                        ["Conservative", "Balanced", "Creative"],
                        label="Creativity Level",
                        value="Balanced"
                    ),
                    gr.Radio(
                        ["Short", "Medium", "Long"],
                        label="Response Length",
                        value="Medium"
                    )
                ],
                submit_btn="Gửi 📤",
                retry_btn="Thử lại 🔄",
                undo_btn="Hoàn tác ↩️",
                clear_btn="Xóa 🗑️",
            )
        
        with gr.Column(scale=1):
            with gr.Accordion("Hướng dẫn sử dụng", open=False):
                gr.Markdown("""
                ### Cách sử dụng:
                1. Nhập tin nhắn vào ô văn bản
                2. Điều chỉnh các thông số nếu muốn:
                   - System Message: Định hướng cho AI
                   - Max Tokens: Độ dài tối đa của câu trả lời
                   - Temperature: Độ sáng tạo
                   - Top-p: Độ đa dạng của từ vựng
                3. Chọn mức độ sáng tạo và độ dài phản hồi
                4. Nhấn 'Gửi' để chat
                
                ### Các nút chức năng:
                - 🔄 Thử lại: Tạo câu trả lời mới
                - ↩️ Hoàn tác: Quay lại tin nhắn trước
                - 🗑️ Xóa: Xóa lịch sử chat
                """)

if __name__ == "__main__":
    demo.launch(share=True, server_name="0.0.0.0")