Spaces:
Sleeping
Sleeping
aguinaldomavenda0
commited on
Commit
•
c4ae273
1
Parent(s):
b6685c3
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
|
5 |
+
"""
|
6 |
+
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
|
7 |
+
"""
|
8 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
9 |
+
#client = InferenceClient("unsloth/Llama-3.2-1B-Instruct")
|
10 |
+
|
11 |
+
|
12 |
+
def respond(
|
13 |
+
message,
|
14 |
+
history: list[tuple[str, str]],
|
15 |
+
system_message,
|
16 |
+
max_tokens,
|
17 |
+
temperature,
|
18 |
+
top_p,
|
19 |
+
):
|
20 |
+
messages = [{"role": "system", "content": system_message}]
|
21 |
+
|
22 |
+
for val in history:
|
23 |
+
if val[0]:
|
24 |
+
messages.append({"role": "user", "content": val[0]})
|
25 |
+
if val[1]:
|
26 |
+
messages.append({"role": "assistant", "content": val[1]})
|
27 |
+
|
28 |
+
messages.append({"role": "user", "content": message})
|
29 |
+
|
30 |
+
response = ""
|
31 |
+
|
32 |
+
|
33 |
+
try:
|
34 |
+
for message in client.chat_completion(
|
35 |
+
messages,
|
36 |
+
max_tokens=max_tokens,
|
37 |
+
stream=True,
|
38 |
+
temperature=temperature,
|
39 |
+
top_p=top_p,
|
40 |
+
):
|
41 |
+
# Ensure the message has a valid structure
|
42 |
+
if not message or not isinstance(message, dict):
|
43 |
+
continue
|
44 |
+
|
45 |
+
try:
|
46 |
+
# Extract content and finish reason
|
47 |
+
content = message.choices[0].delta.content
|
48 |
+
finish_reason = message.choices[0].finish_reason
|
49 |
+
|
50 |
+
# Check if the content is empty
|
51 |
+
if content.strip() == "":
|
52 |
+
# If the finish reason is 'stop', it's expected and we can break the loop
|
53 |
+
if finish_reason == "stop":
|
54 |
+
print("Stream ended normally.")
|
55 |
+
break
|
56 |
+
else:
|
57 |
+
print("Received unexpected empty content, skipping...")
|
58 |
+
continue
|
59 |
+
|
60 |
+
response += content
|
61 |
+
yield response
|
62 |
+
|
63 |
+
except (AttributeError, IndexError, KeyError) as e:
|
64 |
+
print(f"Error processing message: {e}")
|
65 |
+
continue
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
print(f"Unexpected error: {e}")
|
69 |
+
yield "An error occurred while generating the response."
|
70 |
+
|
71 |
+
# Final check if the response is empty
|
72 |
+
if response.strip() == "":
|
73 |
+
yield "No response generated. Please try again or adjust the settings."
|
74 |
+
|
75 |
+
|
76 |
+
"""
|
77 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
78 |
+
"""
|
79 |
+
demo = gr.ChatInterface(
|
80 |
+
respond,
|
81 |
+
additional_inputs=[
|
82 |
+
gr.Textbox(value="You are a friendly Chatbot. Your name is Juninho.", label="System message"),
|
83 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
84 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
85 |
+
gr.Slider(
|
86 |
+
minimum=0.1,
|
87 |
+
maximum=1.0,
|
88 |
+
value=0.95,
|
89 |
+
step=0.05,
|
90 |
+
label="Top-p (nucleus sampling)",
|
91 |
+
),
|
92 |
+
],
|
93 |
+
)
|
94 |
+
|
95 |
+
|
96 |
+
if __name__ == "__main__":
|
97 |
+
demo.launch()
|