Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from typing import List
|
|
| 7 |
|
| 8 |
model = Llama(
|
| 9 |
model_path=hf_hub_download(
|
| 10 |
-
repo_id=os.environ.get("REPO_ID", "Lyte/QuadConnect2.5-1.5B-v0.
|
| 11 |
filename=os.environ.get("MODEL_FILE", "unsloth.Q8_0.gguf"), #"quadconnect.Q8_0.gguf"),
|
| 12 |
),
|
| 13 |
n_ctx=16384
|
|
@@ -47,16 +47,6 @@ def extract_xml_move(text: str) -> str:
|
|
| 47 |
return match.group(1)
|
| 48 |
return ""
|
| 49 |
|
| 50 |
-
def extract_xml_reasoning(text: str) -> str:
|
| 51 |
-
"""
|
| 52 |
-
Extracts the reasoning section from the XML format.
|
| 53 |
-
"""
|
| 54 |
-
import re
|
| 55 |
-
match = re.search(r'<reasoning>(.*?)</reasoning>', text, re.DOTALL)
|
| 56 |
-
if match:
|
| 57 |
-
return match.group(1).strip()
|
| 58 |
-
return ""
|
| 59 |
-
|
| 60 |
def convert_moves_to_coordinate_list(moves_list: List[str]) -> str:
|
| 61 |
"""
|
| 62 |
Converts a list of moves to a coordinate list representation.
|
|
@@ -301,7 +291,6 @@ def create_interface():
|
|
| 301 |
margin: 15px 0;
|
| 302 |
font-family: monospace;
|
| 303 |
min-height: 100px;
|
| 304 |
-
color: white;
|
| 305 |
}
|
| 306 |
.reasoning-box {
|
| 307 |
border-left: 4px solid #2196F3;
|
|
@@ -309,7 +298,6 @@ def create_interface():
|
|
| 309 |
margin: 10px 0;
|
| 310 |
background: #22004d;
|
| 311 |
border-radius: 0 10px 10px 0;
|
| 312 |
-
color: white;
|
| 313 |
}
|
| 314 |
#column-buttons {
|
| 315 |
display: flex;
|
|
@@ -325,14 +313,6 @@ def create_interface():
|
|
| 325 |
div.svelte-1nguped {
|
| 326 |
display: block;
|
| 327 |
}
|
| 328 |
-
.thinking-indicator {
|
| 329 |
-
color: #ffc107;
|
| 330 |
-
font-style: italic;
|
| 331 |
-
}
|
| 332 |
-
.move-highlight {
|
| 333 |
-
font-weight: bold;
|
| 334 |
-
color: #4CAF50;
|
| 335 |
-
}
|
| 336 |
"""
|
| 337 |
|
| 338 |
with gr.Blocks(css=css) as interface:
|
|
@@ -404,63 +384,26 @@ def create_interface():
|
|
| 404 |
|
| 405 |
# Use the new game state formatting
|
| 406 |
game_state = game.format_game_state()
|
|
|
|
| 407 |
|
| 408 |
-
#
|
| 409 |
-
|
| 410 |
-
yield [render_board(game.board), "AI is thinking...", reasoning_html]
|
| 411 |
-
|
| 412 |
-
# Prepare to stream AI's response
|
| 413 |
-
full_response = ""
|
| 414 |
-
current_reasoning = ""
|
| 415 |
-
|
| 416 |
-
# Get AI response with streaming
|
| 417 |
-
for chunk in model.create_chat_completion(
|
| 418 |
messages=[
|
| 419 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 420 |
{"role": "user", "content": game_state}
|
| 421 |
],
|
| 422 |
temperature=temperature,
|
| 423 |
top_p=0.95,
|
| 424 |
-
max_tokens=1024
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
if content:
|
| 430 |
-
full_response += content
|
| 431 |
-
|
| 432 |
-
# Try to extract current reasoning for display
|
| 433 |
-
try:
|
| 434 |
-
# Update the displayed reasoning as it comes in
|
| 435 |
-
current_reasoning = extract_xml_reasoning(full_response)
|
| 436 |
-
if current_reasoning:
|
| 437 |
-
# Format reasoning for display
|
| 438 |
-
reasoning_html = f'''
|
| 439 |
-
<div id="ai-reasoning">
|
| 440 |
-
<div class="reasoning-box">
|
| 441 |
-
<p><strong>🤔 Reasoning:</strong></p>
|
| 442 |
-
<p>{current_reasoning}</p>
|
| 443 |
-
<p class="thinking-indicator">Deciding on next move...</p>
|
| 444 |
-
</div>
|
| 445 |
-
</div>
|
| 446 |
-
'''
|
| 447 |
-
yield [render_board(game.board), "AI is thinking...", reasoning_html]
|
| 448 |
-
except:
|
| 449 |
-
# If we can't extract reasoning yet, just show what we have
|
| 450 |
-
reasoning_html = f'''
|
| 451 |
-
<div id="ai-reasoning">
|
| 452 |
-
<div class="reasoning-box">
|
| 453 |
-
<p><strong>🤔 Reasoning:</strong></p>
|
| 454 |
-
<p class="thinking-indicator">Analyzing the board...</p>
|
| 455 |
-
</div>
|
| 456 |
-
</div>
|
| 457 |
-
'''
|
| 458 |
-
yield [render_board(game.board), "AI is thinking...", reasoning_html]
|
| 459 |
|
| 460 |
-
#
|
| 461 |
try:
|
| 462 |
-
reasoning =
|
| 463 |
-
move_str = extract_xml_move(
|
| 464 |
|
| 465 |
if not move_str:
|
| 466 |
raise ValueError("Invalid move format from AI")
|
|
@@ -470,18 +413,17 @@ def create_interface():
|
|
| 470 |
if ai_col == -1:
|
| 471 |
raise ValueError("Invalid move format from AI")
|
| 472 |
|
| 473 |
-
# Format
|
| 474 |
reasoning_html = f'''
|
| 475 |
<div id="ai-reasoning">
|
| 476 |
<div class="reasoning-box">
|
| 477 |
<p><strong>🤔 Reasoning:</strong></p>
|
| 478 |
<p>{reasoning}</p>
|
| 479 |
-
<p><strong>📍 Move chosen:</strong>
|
| 480 |
</div>
|
| 481 |
</div>
|
| 482 |
'''
|
| 483 |
|
| 484 |
-
# Make the AI's move
|
| 485 |
success, _ = game.make_move(ai_col)
|
| 486 |
if success:
|
| 487 |
# Check for AI winner
|
|
|
|
| 7 |
|
| 8 |
model = Llama(
|
| 9 |
model_path=hf_hub_download(
|
| 10 |
+
repo_id=os.environ.get("REPO_ID", "Lyte/QuadConnect2.5-1.5B-v0.0.9b"), #"Lyte/QuadConnect2.5-0.5B-v0.0.9b"),#"Lyte/QuadConnect2.5-0.5B-v0.0.8b"), #"Lyte/QuadConnect2.5-0.5B-v0.0.6b"), #"Lyte/QuadConnect-Llama-1B-v0.0.7b"),#"
|
| 11 |
filename=os.environ.get("MODEL_FILE", "unsloth.Q8_0.gguf"), #"quadconnect.Q8_0.gguf"),
|
| 12 |
),
|
| 13 |
n_ctx=16384
|
|
|
|
| 47 |
return match.group(1)
|
| 48 |
return ""
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def convert_moves_to_coordinate_list(moves_list: List[str]) -> str:
|
| 51 |
"""
|
| 52 |
Converts a list of moves to a coordinate list representation.
|
|
|
|
| 291 |
margin: 15px 0;
|
| 292 |
font-family: monospace;
|
| 293 |
min-height: 100px;
|
|
|
|
| 294 |
}
|
| 295 |
.reasoning-box {
|
| 296 |
border-left: 4px solid #2196F3;
|
|
|
|
| 298 |
margin: 10px 0;
|
| 299 |
background: #22004d;
|
| 300 |
border-radius: 0 10px 10px 0;
|
|
|
|
| 301 |
}
|
| 302 |
#column-buttons {
|
| 303 |
display: flex;
|
|
|
|
| 313 |
div.svelte-1nguped {
|
| 314 |
display: block;
|
| 315 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
"""
|
| 317 |
|
| 318 |
with gr.Blocks(css=css) as interface:
|
|
|
|
| 384 |
|
| 385 |
# Use the new game state formatting
|
| 386 |
game_state = game.format_game_state()
|
| 387 |
+
print(game_state)
|
| 388 |
|
| 389 |
+
# Get AI response with user-defined temperature
|
| 390 |
+
response = model.create_chat_completion(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
messages=[
|
| 392 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 393 |
{"role": "user", "content": game_state}
|
| 394 |
],
|
| 395 |
temperature=temperature,
|
| 396 |
top_p=0.95,
|
| 397 |
+
max_tokens=1024
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
ai_response = response['choices'][0]['message']['content']
|
| 401 |
+
print(ai_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
+
# Extract reasoning and move
|
| 404 |
try:
|
| 405 |
+
reasoning = ai_response.split("<reasoning>")[1].split("</reasoning>")[0].strip()
|
| 406 |
+
move_str = extract_xml_move(ai_response)
|
| 407 |
|
| 408 |
if not move_str:
|
| 409 |
raise ValueError("Invalid move format from AI")
|
|
|
|
| 413 |
if ai_col == -1:
|
| 414 |
raise ValueError("Invalid move format from AI")
|
| 415 |
|
| 416 |
+
# Format reasoning for display
|
| 417 |
reasoning_html = f'''
|
| 418 |
<div id="ai-reasoning">
|
| 419 |
<div class="reasoning-box">
|
| 420 |
<p><strong>🤔 Reasoning:</strong></p>
|
| 421 |
<p>{reasoning}</p>
|
| 422 |
+
<p><strong>📍 Move chosen:</strong> Column {move_str.upper()}</p>
|
| 423 |
</div>
|
| 424 |
</div>
|
| 425 |
'''
|
| 426 |
|
|
|
|
| 427 |
success, _ = game.make_move(ai_col)
|
| 428 |
if success:
|
| 429 |
# Check for AI winner
|