Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history:
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
|
@@ -27,26 +25,25 @@ def respond(
|
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
|
|
|
| 36 |
):
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 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
|
| 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(
|
|
@@ -59,6 +56,5 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
|
| 5 |
+
# Create inference client
|
| 6 |
+
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def respond(
|
| 9 |
+
message: str,
|
| 10 |
+
history: List[Tuple[str, str]],
|
| 11 |
+
system_message: str,
|
| 12 |
+
max_tokens: int,
|
| 13 |
+
temperature: float,
|
| 14 |
+
top_p: float,
|
| 15 |
):
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
|
|
|
|
| 25 |
|
| 26 |
response = ""
|
| 27 |
|
| 28 |
+
# Streaming inference
|
| 29 |
+
for msg in client.chat_completion(
|
| 30 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 31 |
+
messages=messages,
|
| 32 |
max_tokens=max_tokens,
|
|
|
|
| 33 |
temperature=temperature,
|
| 34 |
top_p=top_p,
|
| 35 |
+
stream=True,
|
| 36 |
):
|
| 37 |
+
delta = msg.choices[0].delta.content
|
| 38 |
+
if delta:
|
| 39 |
+
response += delta
|
| 40 |
+
yield response
|
| 41 |
|
| 42 |
+
# Gradio Chat UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
demo = gr.ChatInterface(
|
| 44 |
respond,
|
| 45 |
additional_inputs=[
|
| 46 |
+
gr.Textbox(value="You are a friendly chatbot.", label="System message"),
|
| 47 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 48 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 49 |
gr.Slider(
|
|
|
|
| 56 |
],
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
if __name__ == "__main__":
|
| 60 |
demo.launch()
|