Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ except Exception as e:
|
|
| 24 |
tokenizer = None
|
| 25 |
peft_model = None
|
| 26 |
|
| 27 |
-
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 28 |
"""
|
| 29 |
Generates a response based on the user message and history using the provided PEFT model.
|
| 30 |
Args:
|
|
@@ -56,7 +56,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 56 |
|
| 57 |
# Generate the output
|
| 58 |
try:
|
| 59 |
-
outputs = peft_model.generate(
|
| 60 |
**inputs,
|
| 61 |
max_new_tokens=max_tokens,
|
| 62 |
temperature=temperature,
|
|
@@ -73,22 +73,23 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 73 |
# Extract content between <user>...</user> tags
|
| 74 |
def extract_user_content(text):
|
| 75 |
"""
|
| 76 |
-
Extracts and returns content between <user>...</user> tags
|
| 77 |
If multiple such sections exist, their contents are concatenated.
|
| 78 |
"""
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# Find all matches in the text
|
| 83 |
-
matches = pattern.findall(text)
|
| 84 |
-
|
| 85 |
-
# Extract and concatenate the matched content
|
| 86 |
-
extracted_content = '\n'.join(
|
| 87 |
-
(match[0] or match[1]).strip() for match in matches if match
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
return extracted_content
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
# Gradio interface setup
|
| 93 |
demo = gr.ChatInterface(
|
| 94 |
respond,
|
|
@@ -105,4 +106,3 @@ demo = gr.ChatInterface(
|
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
demo.launch()
|
| 108 |
-
|
|
|
|
| 24 |
tokenizer = None
|
| 25 |
peft_model = None
|
| 26 |
|
| 27 |
+
async def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 28 |
"""
|
| 29 |
Generates a response based on the user message and history using the provided PEFT model.
|
| 30 |
Args:
|
|
|
|
| 56 |
|
| 57 |
# Generate the output
|
| 58 |
try:
|
| 59 |
+
outputs = await peft_model.generate(
|
| 60 |
**inputs,
|
| 61 |
max_new_tokens=max_tokens,
|
| 62 |
temperature=temperature,
|
|
|
|
| 73 |
# Extract content between <user>...</user> tags
|
| 74 |
def extract_user_content(text):
|
| 75 |
"""
|
| 76 |
+
Extracts and returns content between <user>...</user> tags in the given text.
|
| 77 |
If multiple such sections exist, their contents are concatenated.
|
| 78 |
"""
|
| 79 |
+
pattern = re.compile(r'<user>(.*?)</user>|output:', re.IGNORECASE)
|
| 80 |
+
matches = re.findall(pattern, text, re.DOTALL)
|
| 81 |
+
extracted_content = '\n'.join(match.strip() for match in matches)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
return extracted_content
|
| 83 |
|
| 84 |
+
# Extract the normalized text
|
| 85 |
+
normalized_text = extract_user_content(generated_text)
|
| 86 |
+
|
| 87 |
+
# Stream the response token by token
|
| 88 |
+
response = ""
|
| 89 |
+
for token in normalized_text.split():
|
| 90 |
+
response += token + " "
|
| 91 |
+
yield response.strip()
|
| 92 |
+
|
| 93 |
# Gradio interface setup
|
| 94 |
demo = gr.ChatInterface(
|
| 95 |
respond,
|
|
|
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
demo.launch()
|
|
|