godsastray commited on
Commit
d21ca4d
·
verified ·
1 Parent(s): b562e5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -51
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
- message,
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
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
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 = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
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
  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")