Spaces:
Sleeping
Sleeping
Init
Browse files- app.py +106 -38
- assets/bot.png +0 -0
- assets/user.png +0 -0
- 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(
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
messages = [
|
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 |
-
|
22 |
-
for
|
23 |
-
messages,
|
24 |
-
max_tokens=
|
25 |
stream=True,
|
26 |
-
temperature=temperature,
|
27 |
-
top_p=top_p,
|
28 |
):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
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 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|