Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,72 @@
|
|
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 |
|
10 |
def respond(
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p
|
17 |
):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
|
|
|
|
|
43 |
"""
|
44 |
-
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
gr.Textbox(value="You are an AI assistant for missed calls. Keep responses short and specific.", label="System message"),
|
50 |
gr.Slider(minimum=1, maximum=2048, value=100, step=1, label="Max new tokens"),
|
51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.2, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.50, step=0.05, label="Top-p (nucleus sampling)",
|
53 |
-
),
|
54 |
],
|
55 |
)
|
56 |
|
57 |
-
|
58 |
if __name__ == "__main__":
|
59 |
demo.launch()
|
|
|
|
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 |
|
10 |
def respond(
|
11 |
+
message,
|
12 |
+
history: list[tuple[str, str]],
|
13 |
+
system_message,
|
14 |
+
max_tokens,
|
15 |
+
temperature,
|
16 |
+
top_p
|
17 |
):
|
18 |
+
try:
|
19 |
+
import json # Import json inside the function to avoid global import errors on spaces
|
20 |
+
data_received = {
|
21 |
+
"message": message,
|
22 |
+
"history": history,
|
23 |
+
"system_message": system_message,
|
24 |
+
"max_tokens": max_tokens,
|
25 |
+
"temperature": temperature,
|
26 |
+
"top_p": top_p,
|
27 |
+
}
|
28 |
+
print(json.dumps(data_received, indent=4)) # Pretty print it to the logs
|
29 |
|
30 |
+
messages = [{"role": "system", "content": system_message}]
|
31 |
+
for val in history:
|
32 |
+
if val[0]:
|
33 |
+
messages.append({"role": "user", "content": val[0]})
|
34 |
+
if val[1]:
|
35 |
+
messages.append({"role": "assistant", "content": val[1]})
|
36 |
|
37 |
+
messages.append({"role": "user", "content": message})
|
38 |
|
39 |
+
response = ""
|
40 |
+
for message in client.chat_completion(
|
41 |
+
messages,
|
42 |
+
max_tokens=max_tokens,
|
43 |
+
stream=True,
|
44 |
+
temperature=temperature,
|
45 |
+
top_p=top_p,
|
46 |
+
):
|
47 |
+
token = message.choices[0].delta.content
|
48 |
+
response += token
|
49 |
+
yield response
|
50 |
|
51 |
+
except Exception as e:
|
52 |
+
print(f"An error occurred: {e}") # Log any errors
|
53 |
+
return "An error occurred during processing." # Return an error message to Gradio
|
54 |
|
55 |
|
56 |
+
"""
|
57 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
58 |
"""
|
59 |
+
import gradio as gr # Import gradio here
|
|
|
60 |
demo = gr.ChatInterface(
|
61 |
respond,
|
62 |
additional_inputs=[
|
63 |
gr.Textbox(value="You are an AI assistant for missed calls. Keep responses short and specific.", label="System message"),
|
64 |
gr.Slider(minimum=1, maximum=2048, value=100, step=1, label="Max new tokens"),
|
65 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.2, step=0.1, label="Temperature"),
|
66 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.50, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
67 |
],
|
68 |
)
|
69 |
|
|
|
70 |
if __name__ == "__main__":
|
71 |
demo.launch()
|
72 |
+
|