Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,8 @@ 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.1.0b"),
|
11 |
-
filename=os.environ.get("MODEL_FILE", "unsloth.Q8_0.gguf"),
|
12 |
),
|
13 |
n_ctx=16384
|
14 |
)
|
@@ -386,71 +386,114 @@ def create_interface():
|
|
386 |
game_state = game.format_game_state()
|
387 |
print(game_state)
|
388 |
|
389 |
-
#
|
390 |
-
|
391 |
-
|
392 |
-
|
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 |
-
|
409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
410 |
|
411 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
|
413 |
-
|
414 |
-
|
|
|
|
|
|
|
|
|
415 |
|
416 |
-
|
417 |
-
|
|
|
|
|
418 |
<div id="ai-reasoning">
|
419 |
<div class="reasoning-box">
|
420 |
<p><strong>π€ Reasoning:</strong></p>
|
421 |
-
<p>{
|
422 |
-
<p><strong>π Move chosen:</strong> Column {move_str.upper()}</p>
|
423 |
</div>
|
424 |
</div>
|
425 |
'''
|
426 |
|
427 |
-
|
428 |
-
if
|
429 |
-
|
430 |
-
|
431 |
-
if
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
445 |
game.game_over = True
|
446 |
-
|
447 |
render_board(game.board),
|
448 |
-
"AI
|
449 |
-
|
450 |
]
|
451 |
|
452 |
-
|
453 |
-
return
|
454 |
|
455 |
def reset_game():
|
456 |
game.board = np.zeros((6, 7))
|
|
|
7 |
|
8 |
model = Llama(
|
9 |
model_path=hf_hub_download(
|
10 |
+
repo_id=os.environ.get("REPO_ID", "Lyte/QuadConnect2.5-1.5B-v0.1.0b"),
|
11 |
+
filename=os.environ.get("MODEL_FILE", "unsloth.Q8_0.gguf"),
|
12 |
),
|
13 |
n_ctx=16384
|
14 |
)
|
|
|
386 |
game_state = game.format_game_state()
|
387 |
print(game_state)
|
388 |
|
389 |
+
# Create streaming response generator
|
390 |
+
def stream_response():
|
391 |
+
# Initialize streaming response
|
392 |
+
reasoning_html = '<div id="ai-reasoning"><div class="reasoning-box"><p><strong>π€ Reasoning:</strong></p><p>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
|
394 |
+
# Initialize variables to store the full response
|
395 |
+
full_response = ""
|
396 |
+
reasoning_content = ""
|
397 |
+
move_str = ""
|
398 |
+
|
399 |
+
# Set up streaming
|
400 |
+
response_stream = model.create_chat_completion(
|
401 |
+
messages=[
|
402 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
403 |
+
{"role": "user", "content": game_state}
|
404 |
+
],
|
405 |
+
temperature=temperature,
|
406 |
+
top_p=0.95,
|
407 |
+
max_tokens=1024,
|
408 |
+
stream=True
|
409 |
+
)
|
410 |
|
411 |
+
# Process stream chunks
|
412 |
+
for chunk in response_stream:
|
413 |
+
if 'choices' in chunk and len(chunk['choices']) > 0:
|
414 |
+
content = chunk['choices'][0]['delta'].get('content', '')
|
415 |
+
if content:
|
416 |
+
full_response += content
|
417 |
+
|
418 |
+
# Try to extract reasoning as it comes
|
419 |
+
if "<reasoning>" in full_response and "</reasoning>" in full_response:
|
420 |
+
parts = full_response.split("<reasoning>")
|
421 |
+
if len(parts) > 1:
|
422 |
+
reasoning_parts = parts[1].split("</reasoning>")
|
423 |
+
if len(reasoning_parts) > 0:
|
424 |
+
reasoning_content = reasoning_parts[0].strip()
|
425 |
+
|
426 |
+
# Update the HTML with the current reasoning content
|
427 |
+
current_reasoning_html = reasoning_html + reasoning_content + '</p>'
|
428 |
+
|
429 |
+
# Try to extract move as it comes
|
430 |
+
temp_move_str = extract_xml_move(full_response)
|
431 |
+
if temp_move_str:
|
432 |
+
move_str = temp_move_str
|
433 |
+
current_reasoning_html += f'<p><strong>π Move chosen:</strong> Column {move_str.upper()}</p>'
|
434 |
+
|
435 |
+
current_reasoning_html += '</div></div>'
|
436 |
+
|
437 |
+
# Yield the updated HTML
|
438 |
+
yield [
|
439 |
+
render_board(game.board),
|
440 |
+
"AI is thinking...",
|
441 |
+
current_reasoning_html
|
442 |
+
]
|
443 |
|
444 |
+
# Final response with complete reasoning and move
|
445 |
+
if not reasoning_content:
|
446 |
+
try:
|
447 |
+
reasoning_content = full_response.split("<reasoning>")[1].split("</reasoning>")[0].strip()
|
448 |
+
except:
|
449 |
+
reasoning_content = "AI couldn't provide clear reasoning."
|
450 |
|
451 |
+
if not move_str:
|
452 |
+
move_str = extract_xml_move(full_response)
|
453 |
+
|
454 |
+
final_reasoning_html = f'''
|
455 |
<div id="ai-reasoning">
|
456 |
<div class="reasoning-box">
|
457 |
<p><strong>π€ Reasoning:</strong></p>
|
458 |
+
<p>{reasoning_content}</p>
|
459 |
+
<p><strong>π Move chosen:</strong> Column {move_str.upper() if move_str else "?"}</p>
|
460 |
</div>
|
461 |
</div>
|
462 |
'''
|
463 |
|
464 |
+
# Make AI move
|
465 |
+
if move_str:
|
466 |
+
ai_col = game.parse_ai_move(move_str)
|
467 |
+
|
468 |
+
if ai_col != -1:
|
469 |
+
success, _ = game.make_move(ai_col)
|
470 |
+
if success:
|
471 |
+
# Check for AI winner
|
472 |
+
winner = game.check_winner()
|
473 |
+
if winner == 2:
|
474 |
+
game.game_over = True
|
475 |
+
yield [
|
476 |
+
render_board(game.board),
|
477 |
+
"π€ AI wins! Better luck next time!",
|
478 |
+
final_reasoning_html
|
479 |
+
]
|
480 |
+
return
|
481 |
+
else:
|
482 |
+
# AI made a valid move but didn't win
|
483 |
+
game.current_player = 1
|
484 |
+
yield [render_board(game.board), "Your turn!", final_reasoning_html]
|
485 |
+
return
|
486 |
+
|
487 |
+
# If we get here, something went wrong with the AI move
|
488 |
game.game_over = True
|
489 |
+
yield [
|
490 |
render_board(game.board),
|
491 |
+
"AI made an invalid move! You win by default!",
|
492 |
+
'<div id="ai-reasoning">AI made an invalid move or provided an invalid response!</div>'
|
493 |
]
|
494 |
|
495 |
+
# Return the streaming generator
|
496 |
+
return stream_response()
|
497 |
|
498 |
def reset_game():
|
499 |
game.board = np.zeros((6, 7))
|