aiforhumans
commited on
Commit
·
c0f7cf1
1
Parent(s):
4d60f4c
app.py
CHANGED
@@ -1,33 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
def respond(
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
for
|
21 |
-
if
|
22 |
-
messages.append({"role": "user", "content":
|
23 |
-
if
|
24 |
-
messages.append({"role": "assistant", "content":
|
25 |
|
26 |
-
messages.append({"role": "user", "content":
|
27 |
|
28 |
response = ""
|
29 |
|
30 |
-
for message in
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
33 |
stream=True,
|
@@ -39,20 +57,33 @@ def respond(
|
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
chatbot=gr.Chatbot(height=600),
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
gr.Slider(
|
53 |
minimum=0.1,
|
54 |
maximum=1.0,
|
55 |
-
value=
|
56 |
step=0.05,
|
57 |
label="Top-p (nucleus sampling)",
|
58 |
),
|
@@ -61,4 +92,4 @@ demo = gr.ChatInterface(
|
|
61 |
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
|
5 |
+
SYSTEM_MESSAGE_DEFAULT = "You are a friendly Chatbot."
|
6 |
+
MAX_TOKENS_DEFAULT = 512
|
7 |
+
TEMPERATURE_DEFAULT = 0.7
|
8 |
+
TOP_P_DEFAULT = 0.95
|
9 |
+
|
10 |
+
|
11 |
+
inference_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
12 |
|
13 |
|
14 |
def respond(
|
15 |
+
user_message: str,
|
16 |
+
conversation_history: list[tuple[str, str]],
|
17 |
+
system_message: str,
|
18 |
+
max_tokens: int,
|
19 |
+
temperature: float,
|
20 |
+
top_p: float,
|
21 |
+
) -> str:
|
22 |
+
"""
|
23 |
+
Respond to a user message given the conversation history and other parameters.
|
24 |
+
|
25 |
+
Args:
|
26 |
+
user_message (str): The user's message.
|
27 |
+
conversation_history (list[tuple[str, str]]): The conversation history.
|
28 |
+
system_message (str): The system message to display at the top of the chat interface.
|
29 |
+
max_tokens (int): The maximum number of tokens to generate in the response.
|
30 |
+
temperature (float): The temperature to use when generating text.
|
31 |
+
top_p (float): The top-p value to use when generating text.
|
32 |
+
|
33 |
+
Returns:
|
34 |
+
str: The response to the user's message.
|
35 |
+
"""
|
36 |
messages = [{"role": "system", "content": system_message}]
|
37 |
|
38 |
+
for user_input, assistant_response in conversation_history:
|
39 |
+
if user_input:
|
40 |
+
messages.append({"role": "user", "content": user_input})
|
41 |
+
if assistant_response:
|
42 |
+
messages.append({"role": "assistant", "content": assistant_response})
|
43 |
|
44 |
+
messages.append({"role": "user", "content": user_message})
|
45 |
|
46 |
response = ""
|
47 |
|
48 |
+
for message in inference_client.chat_completion(
|
49 |
messages,
|
50 |
max_tokens=max_tokens,
|
51 |
stream=True,
|
|
|
57 |
response += token
|
58 |
yield response
|
59 |
|
60 |
+
|
61 |
+
chatbot_interface = gr.ChatInterface(
|
62 |
+
fn=respond,
|
|
|
|
|
63 |
chatbot=gr.Chatbot(height=600),
|
64 |
additional_inputs=[
|
65 |
+
gr.Textbox(
|
66 |
+
value=SYSTEM_MESSAGE_DEFAULT,
|
67 |
+
label="System message",
|
68 |
+
),
|
69 |
+
gr.Slider(
|
70 |
+
minimum=1,
|
71 |
+
maximum=2048,
|
72 |
+
value=MAX_TOKENS_DEFAULT,
|
73 |
+
step=1,
|
74 |
+
label="Max new tokens",
|
75 |
+
),
|
76 |
+
gr.Slider(
|
77 |
+
minimum=0.1,
|
78 |
+
maximum=4.0,
|
79 |
+
value=TEMPERATURE_DEFAULT,
|
80 |
+
step=0.1,
|
81 |
+
label="Temperature",
|
82 |
+
),
|
83 |
gr.Slider(
|
84 |
minimum=0.1,
|
85 |
maximum=1.0,
|
86 |
+
value=TOP_P_DEFAULT,
|
87 |
step=0.05,
|
88 |
label="Top-p (nucleus sampling)",
|
89 |
),
|
|
|
92 |
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
+
chatbot_interface.launch()
|