Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,38 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
import re
|
4 |
|
5 |
-
client = InferenceClient("
|
6 |
|
7 |
def respond(message, history):
|
8 |
-
messages = [{"role": "system", "content": "
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
messages.
|
13 |
-
messages.append({"role": "assistant", "content": assistant})
|
14 |
|
15 |
-
#
|
16 |
messages.append({"role": "user", "content": message})
|
17 |
|
|
|
|
|
|
|
18 |
response = ""
|
19 |
-
|
|
|
|
|
20 |
messages,
|
21 |
-
max_tokens=
|
22 |
-
temperature
|
23 |
-
stream=True
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
cleaned_response = response
|
30 |
-
|
31 |
-
# Remove special tokens and unwanted labels more aggressively
|
32 |
-
# Added \s* to catch spaces around the tags, and made the regex more comprehensive
|
33 |
-
cleaned_response = re.sub(r"<\|endoftext\|>|<\|user\|>|<\|assistant\|>|<\|system\>|\s*\[USER\]\s*|\s*\[/ASS\]\s*|\s*\[ASS\]\s*|\s*\[/USER\]\s*|\s*\[INST\]\s*|\s*\[/INST\]\s*|\s*\[ASSIST\]\s*|\s*\[/ASSIST\]\s*", "", cleaned_response)
|
34 |
-
|
35 |
-
# Remove any remaining HTML-like tags (e.g., <p>, <div>) that might appear
|
36 |
-
cleaned_response = re.sub(r"<[^>]*>", "", cleaned_response)
|
37 |
-
|
38 |
-
# Clean up extra whitespace and newlines, ensuring single newlines between paragraphs
|
39 |
-
cleaned_response = re.sub(r"\n+", "\n", cleaned_response)
|
40 |
-
cleaned_response = cleaned_response.strip()
|
41 |
-
|
42 |
-
yield cleaned_response
|
43 |
|
44 |
chatbot = gr.ChatInterface(respond)
|
45 |
|
46 |
-
chatbot.launch()
|
|
|
1 |
+
# build on your original chatbot from the previous lesson
|
2 |
+
# a basic chatbot from the previous lesson is below -- edit it to incorporate the changes described above
|
3 |
+
|
4 |
import gradio as gr
|
5 |
+
from huggingface_hub import InferenceClient #imports huggingface models
|
|
|
6 |
|
7 |
+
client = InferenceClient("google/gemma-2-2b-it")
|
8 |
|
9 |
def respond(message, history):
|
10 |
+
messages = [{"role": "system", "content": "I am a kind chatbot."}]
|
11 |
|
12 |
+
# add all previous messages to the messages list
|
13 |
+
if history:
|
14 |
+
messages.extend(history)
|
|
|
15 |
|
16 |
+
# add the current user's message to the messages list
|
17 |
messages.append({"role": "user", "content": message})
|
18 |
|
19 |
+
# makes the chat completion API call,
|
20 |
+
# sending the messages and other parameters to the model
|
21 |
+
# implements streaming, where one word/token appears at a time
|
22 |
response = ""
|
23 |
+
|
24 |
+
# iterate through each message in the method
|
25 |
+
for message in client.chat_completion(
|
26 |
messages,
|
27 |
+
max_tokens=100,
|
28 |
+
temperature=.1,
|
29 |
+
stream=True):
|
30 |
+
|
31 |
+
# add the tokens to the output content
|
32 |
+
token = message.choices[0].delta.content # capture the most recent toke
|
33 |
+
response += token # Add it to the response
|
34 |
+
yield response # yield the response:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
chatbot = gr.ChatInterface(respond)
|
37 |
|
38 |
+
chatbot.launch()
|