Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
|
|
4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
5 |
|
6 |
-
|
7 |
def respond(
|
8 |
message,
|
9 |
-
history: list[
|
10 |
system_message,
|
11 |
max_tokens,
|
12 |
temperature,
|
13 |
top_p,
|
14 |
):
|
15 |
-
# Prepare messages for the API
|
16 |
messages = [{"role": "system", "content": system_message}]
|
17 |
|
18 |
for val in history:
|
19 |
-
|
20 |
-
messages.append({"role": "user", "content": val[0]})
|
21 |
-
if val[1]:
|
22 |
-
messages.append({"role": "assistant", "content": val[1]})
|
23 |
|
24 |
messages.append({"role": "user", "content": message})
|
25 |
|
26 |
-
# Initialize response variable
|
27 |
response = ""
|
28 |
-
|
29 |
-
#
|
30 |
for message in client.chat_completion(
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
@@ -38,11 +33,12 @@ def respond(
|
|
38 |
response += token
|
39 |
yield response
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
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"),
|
@@ -54,8 +50,9 @@ demo = gr.ChatInterface(
|
|
54 |
label="Top-p (nucleus sampling)",
|
55 |
),
|
56 |
],
|
|
|
57 |
)
|
58 |
|
59 |
-
# Launch the
|
60 |
if __name__ == "__main__":
|
61 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
# Initialize the InferenceClient
|
5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
6 |
|
|
|
7 |
def respond(
|
8 |
message,
|
9 |
+
history: list[dict], # Use a list of dictionaries instead of tuples
|
10 |
system_message,
|
11 |
max_tokens,
|
12 |
temperature,
|
13 |
top_p,
|
14 |
):
|
|
|
15 |
messages = [{"role": "system", "content": system_message}]
|
16 |
|
17 |
for val in history:
|
18 |
+
messages.append({"role": val['role'], "content": val['content']})
|
|
|
|
|
|
|
19 |
|
20 |
messages.append({"role": "user", "content": message})
|
21 |
|
|
|
22 |
response = ""
|
23 |
+
|
24 |
+
# Use chat_completion to get responses
|
25 |
for message in client.chat_completion(
|
26 |
messages,
|
27 |
max_tokens=max_tokens,
|
|
|
33 |
response += token
|
34 |
yield response
|
35 |
|
36 |
+
# Create the Gradio Interface for API
|
37 |
+
api_interface = gr.Interface(
|
38 |
+
fn=respond,
|
39 |
+
inputs=[
|
40 |
+
gr.Textbox(label="Message"),
|
41 |
+
gr.JSON(label="History"),
|
42 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
43 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
44 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
|
|
50 |
label="Top-p (nucleus sampling)",
|
51 |
),
|
52 |
],
|
53 |
+
outputs=gr.Textbox(label="Response"),
|
54 |
)
|
55 |
|
56 |
+
# Launch the API
|
57 |
if __name__ == "__main__":
|
58 |
+
api_interface.launch(server_name="0.0.0.0", server_port=7860, share=False) # Set share=False to avoid Hugging Face Spaces
|