kaitwithkwk commited on
Commit
13e2469
·
verified ·
1 Parent(s): 8eb3105

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -1,30 +1,44 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
6
  def respond(message, history):
7
- messages = [{"role": "system", "content": "I am a kind chatbot."}]
8
 
9
  # Add all previous messages to the messages list
10
  for human, assistant in history:
11
  messages.append({"role": "user", "content": human})
12
  messages.append({"role": "assistant", "content": assistant})
13
 
14
- # Add the current user's message to the messages list
15
  messages.append({"role": "user", "content": message})
16
 
17
  response = ""
18
  for chunk in client.chat_completion(
19
  messages,
20
- max_tokens=100,
21
- temperature=0.1,
22
  stream=True
23
  ):
24
  token = chunk.choices[0].delta.content
25
  response += token
26
- # Filter out any unwanted tokens like </s> or <|endoftext|>, [USER], [/ASS]
27
- cleaned_response = response.replace("</s>", "").replace("<|endoftext|>", "").replace("[USER]", "").replace("[/ASS]", "").strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  yield cleaned_response
29
 
30
  chatbot = gr.ChatInterface(respond)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import re
4
 
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
  def respond(message, history):
8
+ messages = [{"role": "system", "content": "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe."}]
9
 
10
  # Add all previous messages to the messages list
11
  for human, assistant in history:
12
  messages.append({"role": "user", "content": human})
13
  messages.append({"role": "assistant", "content": assistant})
14
 
15
+ # Add the current user\"s message to the messages list
16
  messages.append({"role": "user", "content": message})
17
 
18
  response = ""
19
  for chunk in client.chat_completion(
20
  messages,
21
+ max_tokens=500,
22
+ temperature=0.5,
23
  stream=True
24
  ):
25
  token = chunk.choices[0].delta.content
26
  response += token
27
+
28
+ # Enhanced post-processing to handle unwanted patterns
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)