Lyte commited on
Commit
0424e7f
Β·
verified Β·
1 Parent(s): 727d48a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -52
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"), #"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
14
  )
@@ -386,71 +386,114 @@ def create_interface():
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")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
 
411
- ai_col = game.parse_ai_move(move_str)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412
 
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
430
- winner = game.check_winner()
431
- if winner == 2:
432
- game.game_over = True
433
- return [
434
- render_board(game.board),
435
- "πŸ€– AI wins! Better luck next time!",
436
- reasoning_html
437
- ]
438
- else:
439
- return [
440
- render_board(game.board),
441
- "AI made invalid move! You win by default!",
442
- '<div id="ai-reasoning">AI made an invalid move!</div>'
443
- ]
444
- except Exception as e:
 
 
 
 
 
 
445
  game.game_over = True
446
- return [
447
  render_board(game.board),
448
- "AI error occurred! You win by default!",
449
- f'<div id="ai-reasoning">Error: {str(e)}</div>'
450
  ]
451
 
452
- game.current_player = 1
453
- return [render_board(game.board), "Your turn!", reasoning_html]
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))