Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 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 |
|
|
@@ -26,7 +23,6 @@ def respond(
|
|
| 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,
|
|
@@ -35,29 +31,44 @@ def respond(
|
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
| 6 |
|
|
|
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
response = ""
|
|
|
|
| 26 |
for message in client.chat_completion(
|
| 27 |
messages,
|
| 28 |
max_tokens=max_tokens,
|
|
|
|
| 31 |
top_p=top_p,
|
| 32 |
):
|
| 33 |
token = message.choices[0].delta.content
|
|
|
|
| 34 |
response += token
|
| 35 |
yield response
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
system_message = gr.Textbox(
|
| 40 |
+
label="System Message",
|
| 41 |
+
value="You are a helpful assistant.",
|
| 42 |
+
lines=2,
|
| 43 |
+
)
|
| 44 |
+
chat_history = gr.State([])
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column(scale=0.8):
|
| 48 |
+
chatbot = gr.Chatbot()
|
| 49 |
+
with gr.Column(scale=0.2):
|
| 50 |
+
max_tokens = gr.Slider(
|
| 51 |
+
minimum=1, maximum=512, step=1, value=128, label="Max Tokens"
|
| 52 |
+
)
|
| 53 |
+
temperature = gr.Slider(
|
| 54 |
+
minimum=0, maximum=1, step=0.01, value=0.7, label="Temperature"
|
| 55 |
+
)
|
| 56 |
+
top_p = gr.Slider(
|
| 57 |
+
minimum=0, maximum=1, step=0.01, value=1, label="Top-p"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
| 61 |
+
|
| 62 |
+
def user_interaction(message, history, system_message, max_tokens, temperature, top_p):
|
| 63 |
+
bot_message = next(respond(message, history, system_message, max_tokens, temperature, top_p))
|
| 64 |
+
history.append((message, bot_message))
|
| 65 |
+
return history, history
|
| 66 |
+
|
| 67 |
+
user_input.submit(
|
| 68 |
+
user_interaction,
|
| 69 |
+
inputs=[user_input, chat_history, system_message, max_tokens, temperature, top_p],
|
| 70 |
+
outputs=[chatbot, chat_history],
|
| 71 |
+
)
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
+
demo.launch()
|