Spaces:
Runtime error
Runtime error
jenbenarye
commited on
Commit
·
90748c8
1
Parent(s):
fe81470
fix chatbot bug
Browse files- app/app.py +34 -3
app/app.py
CHANGED
|
@@ -272,9 +272,40 @@ def call_pipeline(messages: list, language: str):
|
|
| 272 |
"""Call the appropriate model pipeline based on configuration"""
|
| 273 |
if ZERO_GPU:
|
| 274 |
tokenizer = CLIENT["tokenizer"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
formatted_prompt = tokenizer.apply_chat_template(
|
| 276 |
-
messages
|
| 277 |
tokenize=False,
|
|
|
|
| 278 |
)
|
| 279 |
|
| 280 |
response = CLIENT["pipeline"](
|
|
@@ -702,12 +733,12 @@ def get_config(request: gr.Request):
|
|
| 702 |
config = {
|
| 703 |
"feel_consent": "false",
|
| 704 |
}
|
| 705 |
-
|
| 706 |
if request and request.cookies:
|
| 707 |
for key in config.keys():
|
| 708 |
if key in request.cookies:
|
| 709 |
config[key] = request.cookies[key]
|
| 710 |
-
|
| 711 |
return config["feel_consent"] == "true"
|
| 712 |
|
| 713 |
def initialize_consent_status(request: gr.Request):
|
|
|
|
| 272 |
"""Call the appropriate model pipeline based on configuration"""
|
| 273 |
if ZERO_GPU:
|
| 274 |
tokenizer = CLIENT["tokenizer"]
|
| 275 |
+
# Ensure messages follow the proper alternating pattern
|
| 276 |
+
formatted_messages = []
|
| 277 |
+
prev_role = None
|
| 278 |
+
|
| 279 |
+
for msg in messages:
|
| 280 |
+
role = msg.get("role", "")
|
| 281 |
+
content = msg.get("content", "")
|
| 282 |
+
|
| 283 |
+
# Skip empty messages
|
| 284 |
+
if not content.strip():
|
| 285 |
+
continue
|
| 286 |
+
|
| 287 |
+
# Enforce alternating pattern
|
| 288 |
+
if role == prev_role:
|
| 289 |
+
# If same role repeats, combine with previous message or skip
|
| 290 |
+
continue
|
| 291 |
+
|
| 292 |
+
# Only allow "user" and "assistant" roles
|
| 293 |
+
if role not in ["user", "assistant"]:
|
| 294 |
+
# Convert to proper role or skip
|
| 295 |
+
continue
|
| 296 |
+
|
| 297 |
+
formatted_messages.append(msg)
|
| 298 |
+
prev_role = role
|
| 299 |
+
|
| 300 |
+
# Ensure we start with user message
|
| 301 |
+
if formatted_messages and formatted_messages[0]["role"] != "user":
|
| 302 |
+
formatted_messages = formatted_messages[1:]
|
| 303 |
+
|
| 304 |
+
# Now use the properly formatted messages
|
| 305 |
formatted_prompt = tokenizer.apply_chat_template(
|
| 306 |
+
formatted_messages, # Use the fixed messages
|
| 307 |
tokenize=False,
|
| 308 |
+
add_generation_prompt=True
|
| 309 |
)
|
| 310 |
|
| 311 |
response = CLIENT["pipeline"](
|
|
|
|
| 733 |
config = {
|
| 734 |
"feel_consent": "false",
|
| 735 |
}
|
| 736 |
+
|
| 737 |
if request and request.cookies:
|
| 738 |
for key in config.keys():
|
| 739 |
if key in request.cookies:
|
| 740 |
config[key] = request.cookies[key]
|
| 741 |
+
|
| 742 |
return config["feel_consent"] == "true"
|
| 743 |
|
| 744 |
def initialize_consent_status(request: gr.Request):
|