Spaces:
Sleeping
Sleeping
Jonathan Bejarano
commited on
Commit
Β·
8d0993b
1
Parent(s):
eb351d2
Adding response format for local
Browse files
app.py
CHANGED
|
@@ -221,6 +221,41 @@ RULES:
|
|
| 221 |
|
| 222 |
current_system = get_system_message_with_country()
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
def format_game_result(response):
|
| 225 |
"""Format the game result with proper styling"""
|
| 226 |
if "The country was" in response:
|
|
@@ -302,6 +337,7 @@ def respond(
|
|
| 302 |
model=MODEL_NAME,
|
| 303 |
max_tokens=max_tokens,
|
| 304 |
stream=True,
|
|
|
|
| 305 |
temperature=temperature,
|
| 306 |
top_p=top_p,
|
| 307 |
):
|
|
@@ -320,6 +356,10 @@ def respond(
|
|
| 320 |
print(f"π DEBUG - Response length: {len(response)} characters")
|
| 321 |
print(f"π DEBUG - Raw response: {response}")
|
| 322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
# Check if this is a game end response and format it nicely
|
| 324 |
if "The country was" in response:
|
| 325 |
print(f"π DEBUG - Game end detected! Country extracted: {selected_country}")
|
|
@@ -361,7 +401,7 @@ if LOCAL_MODE:
|
|
| 361 |
def custom_respond(message, history):
|
| 362 |
system_message = ""
|
| 363 |
max_tokens = 4000
|
| 364 |
-
temperature = 0.
|
| 365 |
top_p = 0.6
|
| 366 |
return respond(message, history, system_message, max_tokens, temperature, top_p, None)
|
| 367 |
|
|
@@ -383,7 +423,7 @@ else:
|
|
| 383 |
additional_inputs=[
|
| 384 |
gr.Textbox(value="", visible=False), # system_message (hidden)
|
| 385 |
gr.Slider(minimum=1, maximum=4096, value=4000, visible=False), # max_tokens (hidden)
|
| 386 |
-
gr.Slider(minimum=0.1, maximum=2.0, value=0.
|
| 387 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.6, visible=False), # top_p (hidden)
|
| 388 |
],
|
| 389 |
)
|
|
|
|
| 221 |
|
| 222 |
current_system = get_system_message_with_country()
|
| 223 |
|
| 224 |
+
def clean_response(response):
|
| 225 |
+
"""Clean up the response by removing unwanted metadata and formatting artifacts"""
|
| 226 |
+
import re
|
| 227 |
+
import json
|
| 228 |
+
|
| 229 |
+
# Remove channel/commentary tags and similar artifacts
|
| 230 |
+
response = re.sub(r'<\|channel\|>commentary to=assistant', '', response, flags=re.IGNORECASE)
|
| 231 |
+
response = re.sub(r'<\|constrain\|>json<\|message\|>', '', response, flags=re.IGNORECASE)
|
| 232 |
+
response = re.sub(r'<\|.*?\|>', '', response) # Remove any other <|...|> patterns
|
| 233 |
+
|
| 234 |
+
# Try to parse JSON response and extract the actual message
|
| 235 |
+
try:
|
| 236 |
+
# Look for JSON-like content
|
| 237 |
+
json_match = re.search(r'\{[^}]*"response"\s*:\s*"([^"]*)"[^}]*\}', response)
|
| 238 |
+
if json_match:
|
| 239 |
+
actual_response = json_match.group(1)
|
| 240 |
+
print(f"π DEBUG - Extracted from JSON: {actual_response}")
|
| 241 |
+
return actual_response
|
| 242 |
+
|
| 243 |
+
# Try to parse as complete JSON
|
| 244 |
+
parsed = json.loads(response.strip())
|
| 245 |
+
if isinstance(parsed, dict) and "response" in parsed:
|
| 246 |
+
actual_response = parsed["response"]
|
| 247 |
+
print(f"π DEBUG - Parsed JSON response: {actual_response}")
|
| 248 |
+
return actual_response
|
| 249 |
+
except (json.JSONDecodeError, AttributeError):
|
| 250 |
+
# If JSON parsing fails, continue with text cleaning
|
| 251 |
+
pass
|
| 252 |
+
|
| 253 |
+
# Clean up extra whitespace and newlines
|
| 254 |
+
response = re.sub(r'\n\s*\n', '\n', response) # Remove multiple empty lines
|
| 255 |
+
response = response.strip() # Remove leading/trailing whitespace
|
| 256 |
+
|
| 257 |
+
return response
|
| 258 |
+
|
| 259 |
def format_game_result(response):
|
| 260 |
"""Format the game result with proper styling"""
|
| 261 |
if "The country was" in response:
|
|
|
|
| 337 |
model=MODEL_NAME,
|
| 338 |
max_tokens=max_tokens,
|
| 339 |
stream=True,
|
| 340 |
+
response_format={"type": "text"},
|
| 341 |
temperature=temperature,
|
| 342 |
top_p=top_p,
|
| 343 |
):
|
|
|
|
| 356 |
print(f"π DEBUG - Response length: {len(response)} characters")
|
| 357 |
print(f"π DEBUG - Raw response: {response}")
|
| 358 |
|
| 359 |
+
# Clean the response to remove unwanted artifacts
|
| 360 |
+
response = clean_response(response)
|
| 361 |
+
print(f"π DEBUG - Cleaned response: {response}")
|
| 362 |
+
|
| 363 |
# Check if this is a game end response and format it nicely
|
| 364 |
if "The country was" in response:
|
| 365 |
print(f"π DEBUG - Game end detected! Country extracted: {selected_country}")
|
|
|
|
| 401 |
def custom_respond(message, history):
|
| 402 |
system_message = ""
|
| 403 |
max_tokens = 4000
|
| 404 |
+
temperature = 0.3
|
| 405 |
top_p = 0.6
|
| 406 |
return respond(message, history, system_message, max_tokens, temperature, top_p, None)
|
| 407 |
|
|
|
|
| 423 |
additional_inputs=[
|
| 424 |
gr.Textbox(value="", visible=False), # system_message (hidden)
|
| 425 |
gr.Slider(minimum=1, maximum=4096, value=4000, visible=False), # max_tokens (hidden)
|
| 426 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.3, visible=False), # temperature (hidden)
|
| 427 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.6, visible=False), # top_p (hidden)
|
| 428 |
],
|
| 429 |
)
|