pratikshahp commited on
Commit
7ea49b0
·
verified ·
1 Parent(s): 76effaf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ import gradio as gr
4
+ from langchain_huggingface import HuggingFaceEndpoint
5
+
6
+ # Load environment variables
7
+ load_dotenv()
8
+ HF_TOKEN = os.getenv("HF_TOKEN")
9
+
10
+ # Initialize the HuggingFace inference endpoint
11
+ llm = HuggingFaceEndpoint(
12
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3",
13
+ huggingfacehub_api_token=HF_TOKEN.strip(),
14
+ temperature=0.7,
15
+ )
16
+
17
+ # Initialize the game board and state
18
+ def initialize_game():
19
+ board = [["" for _ in range(3)] for _ in range(3)]
20
+ current_player = "X"
21
+ status = "Player 1's turn (X)"
22
+ buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
23
+ return board, current_player, status, *buttons
24
+
25
+ # Check for a winner
26
+ def check_winner(board):
27
+ for i in range(3):
28
+ if board[i][0] == board[i][1] == board[i][2] and board[i][0] != "":
29
+ return board[i][0]
30
+ if board[0][i] == board[1][i] == board[2][i] and board[0][i] != "":
31
+ return board[0][i]
32
+ if board[0][0] == board[1][1] == board[2][2] and board[0][0] != "":
33
+ return board[0][0]
34
+ if board[0][2] == board[1][1] == board[2][0] and board[0][2] != "":
35
+ return board[0][2]
36
+ return None
37
+
38
+ # Check for a draw
39
+ def check_draw(board):
40
+ return all(cell != "" for row in board for cell in row)
41
+
42
+ # Use LLM for commentary
43
+ def get_llm_commentary(board, current_player):
44
+ # Convert the board to a readable format for the model
45
+ board_representation = "\n".join([" ".join(cell if cell else "_" for cell in row) for row in board])
46
+ prompt = (
47
+ f"The current Tic-Tac-Toe board is as follows:\n{board_representation}\n"
48
+ f"It's {current_player}'s turn. Analyze the board and suggest a good move or strategy. "
49
+ f"Respond with a short explanation of your analysis."
50
+ )
51
+ response = llm(prompt)
52
+ return response.strip()
53
+
54
+ # Handle a move
55
+ def handle_move(board, current_player, button_idx, game_status):
56
+ if "wins" in game_status or "draw" in game_status:
57
+ status = game_status
58
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
59
+ return board, current_player, status, *buttons
60
+
61
+ row, col = divmod(button_idx, 3)
62
+ if board[row][col] != "":
63
+ status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
64
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)]
65
+ return board, current_player, status, *buttons
66
+
67
+ board[row][col] = current_player
68
+ winner = check_winner(board)
69
+ if winner:
70
+ status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! 🎉"
71
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
72
+ return board, current_player, status, *buttons
73
+
74
+ if check_draw(board):
75
+ status = "It's a draw! 🤝"
76
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
77
+ return board, current_player, status, *buttons
78
+
79
+ current_player = "O" if current_player == "X" else "X"
80
+ status = f"Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
81
+
82
+ # Generate LLM commentary
83
+ llm_commentary = get_llm_commentary(board, current_player)
84
+ status += f"\nLLM Analysis: {llm_commentary}"
85
+
86
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)]
87
+ return board, current_player, status, *buttons
88
+
89
+ # Build the Gradio UI
90
+ with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text-align: center;}") as tic_tac_toe:
91
+ gr.Markdown("## Tic-Tac-Toe with LLM Commentary 🎮")
92
+
93
+ # Initialize states
94
+ board_state = gr.State([["" for _ in range(3)] for _ in range(3)])
95
+ current_player = gr.State("X")
96
+ game_status = gr.Textbox(value="Player 1's turn (X)", label="Game Status", interactive=False)
97
+
98
+ # Create grid buttons
99
+ buttons = []
100
+ for i in range(3):
101
+ with gr.Row():
102
+ for j in range(3):
103
+ btn = gr.Button(value="", elem_classes=["cell-btn"])
104
+ buttons.append(btn)
105
+
106
+ # Update buttons dynamically on click
107
+ for idx, btn in enumerate(buttons):
108
+ btn.click(
109
+ handle_move,
110
+ inputs=[board_state, current_player, gr.Number(idx, visible=False), game_status],
111
+ outputs=[board_state, current_player, game_status, *buttons],
112
+ )
113
+
114
+ # Reset game button
115
+ reset_button = gr.Button("Reset Game")
116
+ reset_button.click(
117
+ initialize_game,
118
+ inputs=[],
119
+ outputs=[board_state, current_player, game_status, *buttons],
120
+ )
121
+
122
+ tic_tac_toe.launch()