baobuiquang commited on
Commit
f444c18
·
verified ·
1 Parent(s): 4c6ec45
Files changed (4) hide show
  1. app.py +106 -38
  2. assets/bot.png +0 -0
  3. assets/user.png +0 -0
  4. requirements.txt +0 -0
app.py CHANGED
@@ -1,50 +1,118 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
-
6
- def respond(
7
- message,
8
- history: list[tuple[str, str]],
9
- system_message,
10
- max_tokens,
11
- temperature,
12
- top_p,
13
- ):
14
- messages = [{"role": "system", "content": system_message}]
15
  for val in history:
16
  if val[0]:
17
  messages.append({"role": "user", "content": val[0]})
18
  if val[1]:
19
  messages.append({"role": "assistant", "content": val[1]})
20
  messages.append({"role": "user", "content": message})
21
- response = ""
22
- for message in client.chat_completion(
23
- messages,
24
- max_tokens=max_tokens,
25
  stream=True,
26
- temperature=temperature,
27
- top_p=top_p,
28
  ):
29
- token = message.choices[0].delta.content
30
- response += token
31
- yield response
32
-
33
- demo = gr.ChatInterface(
34
- respond,
35
- additional_inputs=[
36
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
37
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
38
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
39
- gr.Slider(
40
- minimum=0.1,
41
- maximum=1.0,
42
- value=0.95,
43
- step=0.05,
44
- label="Top-p (nucleus sampling)",
45
- ),
46
- ],
47
- )
48
 
49
- if __name__ == "__main__":
50
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
5
+ client = InferenceClient(
6
+ model = "HuggingFaceH4/zephyr-7b-beta",
7
+ # model = "mistralai/Mistral-7B-Instruct-v0.3",
8
+ )
9
+
10
+
11
+ def respond(message, history: list[tuple[str, str]]):
12
+
13
+ output = ""
14
+
15
+ messages = []
16
  for val in history:
17
  if val[0]:
18
  messages.append({"role": "user", "content": val[0]})
19
  if val[1]:
20
  messages.append({"role": "assistant", "content": val[1]})
21
  messages.append({"role": "user", "content": message})
22
+
23
+ for word in client.chat_completion(
24
+ messages=messages,
25
+ max_tokens=512,
26
  stream=True,
 
 
27
  ):
28
+ output += word.choices[0].delta.content
29
+
30
+ for i in range(len(output)):
31
+ time.sleep(0.002)
32
+ yield output[: i+1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+
35
+ gr.ChatInterface(
36
+ fn = respond,
37
+ theme = gr.themes.Base(
38
+ primary_hue = "stone",
39
+ ),
40
+ textbox = gr.Textbox(
41
+ elem_id="custom-gr-textbox",
42
+ placeholder = "Type your message here...",
43
+ container = False,
44
+ scale = 10,
45
+ ),
46
+ chatbot = gr.Chatbot(
47
+ elem_id="custom-gr-chatbot",
48
+ height = '92%',
49
+ bubble_full_width = False,
50
+ show_copy_button = True,
51
+ likeable = True,
52
+ show_label = False,
53
+ show_share_button = False,
54
+ container = False,
55
+ render_markdown = True,
56
+ line_breaks = True,
57
+ avatar_images = (
58
+ 'assets/user.png',
59
+ 'assets/bot.png',
60
+ ),
61
+ layout = 'bubble',
62
+ # value = [
63
+ # [
64
+ # None,
65
+ # "Hello",
66
+ # ],
67
+ # ],
68
+ ),
69
+ css = '\
70
+ footer, [data-testid="block-label"] { \
71
+ display: none !important; visibility: hidden !important; opacity: 0.0 !important; \
72
+ } \
73
+ .gradio-container { \
74
+ max-width: initial !important; padding: 16px !important; \
75
+ } \
76
+ .contain { \
77
+ height: 1vh !important; \
78
+ } \
79
+ .contain > div { \
80
+ height: 100% !important; \
81
+ } \
82
+ #custom-gr-chatbot + .stretch { \
83
+ display: none !important; \
84
+ } \
85
+ #custom-gr-textbox { \
86
+ background: hsl(0 0% 98%) !important; \
87
+ } \
88
+ #custom-gr-textbox textarea { \
89
+ font-size: 16px !important; \
90
+ margin: 8px 0px 14px 10px !important; \
91
+ background: hsl(0 0% 98%) !important; \
92
+ } \
93
+ #custom-gr-textbox + button.lg.primary, #custom-gr-textbox + button.lg.stop { \
94
+ min-width: min(100px, 100%) !important; \
95
+ } \
96
+ .avatar-container { \
97
+ height: 24px !important; \
98
+ width: 24px !important; \
99
+ } \
100
+ .message { \
101
+ padding: 10px 16px !important; \
102
+ } \
103
+ .message-buttons-bot, .message-buttons-user { \
104
+ bottom: -18px !important; \
105
+ transform: scale(0.8) !important; \
106
+ } \
107
+ .user-row .avatar-container { \
108
+ display: none !important; visibility: hidden !important; opacity: 0.0 !important; \
109
+ } \
110
+ ',
111
+ retry_btn = None,
112
+ undo_btn = None,
113
+ clear_btn = None,
114
+ submit_btn = "Send",
115
+ stop_btn = "Stop",
116
+ autofocus = True,
117
+ fill_height = True,
118
+ ).launch(debug = False, share = False)
assets/bot.png ADDED
assets/user.png ADDED
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ