Spaces:
Sleeping
Sleeping
import os | |
from dotenv import load_dotenv | |
import gradio as gr | |
from langchain_huggingface import HuggingFaceEndpoint | |
# Load environment variables | |
load_dotenv() | |
HF_TOKEN = os.getenv("HF_TOKEN") | |
# Initialize the HuggingFace inference endpoint | |
llm = HuggingFaceEndpoint( | |
repo_id="mistralai/Mistral-7B-Instruct-v0.3", | |
huggingfacehub_api_token=HF_TOKEN.strip(), | |
temperature=0.7, | |
) | |
# Initialize the game board and state | |
def initialize_game(): | |
board = [["" for _ in range(3)] for _ in range(3)] | |
current_player = "X" | |
status = "Player 1's turn (X)" | |
buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)] | |
return board, current_player, status, *buttons | |
# Check for a winner | |
def check_winner(board): | |
for i in range(3): | |
if board[i][0] == board[i][1] == board[i][2] and board[i][0] != "": | |
return board[i][0] | |
if board[0][i] == board[1][i] == board[2][i] and board[0][i] != "": | |
return board[0][i] | |
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != "": | |
return board[0][0] | |
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != "": | |
return board[0][2] | |
return None | |
# Check for a draw | |
def check_draw(board): | |
return all(cell != "" for row in board for cell in row) | |
# Use LLM for commentary | |
def get_llm_commentary(board, current_player): | |
# Convert the board to a readable format for the model | |
board_representation = "\n".join([" ".join(cell if cell else "_" for cell in row) for row in board]) | |
prompt = ( | |
f"The current Tic-Tac-Toe board is as follows:\n{board_representation}\n" | |
f"It's {current_player}'s turn. Analyze the board and suggest a good move or strategy. " | |
f"Respond with a short explanation of your analysis." | |
) | |
response = llm(prompt) | |
return response.strip() | |
# Handle a move | |
def handle_move(board, current_player, button_idx, game_status): | |
if "wins" in game_status or "draw" in game_status: | |
status = game_status | |
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)] | |
return board, current_player, status, *buttons | |
row, col = divmod(button_idx, 3) | |
if board[row][col] != "": | |
status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})" | |
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)] | |
return board, current_player, status, *buttons | |
board[row][col] = current_player | |
winner = check_winner(board) | |
if winner: | |
status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! ๐" | |
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)] | |
return board, current_player, status, *buttons | |
if check_draw(board): | |
status = "It's a draw! ๐ค" | |
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)] | |
return board, current_player, status, *buttons | |
current_player = "O" if current_player == "X" else "X" | |
status = f"Player {1 if current_player == 'X' else 2}'s turn ({current_player})" | |
# Generate LLM commentary | |
llm_commentary = get_llm_commentary(board, current_player) | |
status += f"\nLLM Analysis: {llm_commentary}" | |
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)] | |
return board, current_player, status, *buttons | |
# Build the Gradio UI | |
with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text-align: center;}") as tic_tac_toe: | |
gr.Markdown("## Tic-Tac-Toe with LLM Commentary ๐ฎ") | |
# Initialize states | |
board_state = gr.State([["" for _ in range(3)] for _ in range(3)]) | |
current_player = gr.State("X") | |
game_status = gr.Textbox(value="Player 1's turn (X)", label="Game Status", interactive=False) | |
# Create grid buttons | |
buttons = [] | |
for i in range(3): | |
with gr.Row(): | |
for j in range(3): | |
btn = gr.Button(value="", elem_classes=["cell-btn"]) | |
buttons.append(btn) | |
# Update buttons dynamically on click | |
for idx, btn in enumerate(buttons): | |
btn.click( | |
handle_move, | |
inputs=[board_state, current_player, gr.Number(idx, visible=False), game_status], | |
outputs=[board_state, current_player, game_status, *buttons], | |
) | |
# Reset game button | |
reset_button = gr.Button("Reset Game") | |
reset_button.click( | |
initialize_game, | |
inputs=[], | |
outputs=[board_state, current_player, game_status, *buttons], | |
) | |
tic_tac_toe.launch() | |