Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -100,8 +100,8 @@ class Room:
|
|
| 100 |
# Board representation
|
| 101 |
markdown += "```\n"
|
| 102 |
for i in range(0, 9, 3):
|
| 103 |
-
row = [self.board[i] or '
|
| 104 |
-
markdown += f"
|
| 105 |
if i < 6:
|
| 106 |
markdown += "-----------\n"
|
| 107 |
markdown += "```\n\n"
|
|
@@ -337,118 +337,9 @@ Keep responses under 50 words. Use emojis occasionally. Don't make game moves in
|
|
| 337 |
|
| 338 |
return response.choices[0].message.content
|
| 339 |
|
| 340 |
-
|
| 341 |
-
def get_ai_move():
|
| 342 |
-
try:
|
| 343 |
-
data = request.json
|
| 344 |
-
board = data.get('board', [])
|
| 345 |
-
|
| 346 |
-
# Convert board to string representation
|
| 347 |
-
board_string = ""
|
| 348 |
-
for i in range(0, 9, 3):
|
| 349 |
-
row = [board[i] or ' ', board[i+1] or ' ', board[i+2] or ' ']
|
| 350 |
-
board_string += f"{row[0]} | {row[1]} | {row[2]}\n"
|
| 351 |
-
if i < 6:
|
| 352 |
-
board_string += "---------\n"
|
| 353 |
-
|
| 354 |
-
messages = [
|
| 355 |
-
{
|
| 356 |
-
"role": "system",
|
| 357 |
-
"content": """You are a competitive Tic-Tac-Toe AI with personality. You play as 'O' and the human plays as 'X'.
|
| 358 |
-
|
| 359 |
-
Rules:
|
| 360 |
-
1. Analyze the board and choose your best move (0-8, left to right, top to bottom)
|
| 361 |
-
2. Add a short, witty comment about your move or the game state
|
| 362 |
-
3. Be competitive but fun - trash talk, celebrate good moves, react to the situation
|
| 363 |
-
4. Keep messages under 50 words
|
| 364 |
-
5. Use emojis occasionally
|
| 365 |
-
|
| 366 |
-
ALWAYS respond with valid JSON in this exact format:
|
| 367 |
-
{"move": [0-8], "message": "your witty comment"}
|
| 368 |
-
|
| 369 |
-
Board positions:
|
| 370 |
-
0 | 1 | 2
|
| 371 |
-
---------
|
| 372 |
-
3 | 4 | 5
|
| 373 |
-
---------
|
| 374 |
-
6 | 7 | 8"""
|
| 375 |
-
},
|
| 376 |
-
{
|
| 377 |
-
"role": "user",
|
| 378 |
-
"content": f"Current board:\n{board_string}\n\nBoard array: {board}"
|
| 379 |
-
}
|
| 380 |
-
]
|
| 381 |
-
|
| 382 |
-
response = client.chat.complete(
|
| 383 |
-
model="mistral-large-latest",
|
| 384 |
-
messages=messages,
|
| 385 |
-
temperature=0.1,
|
| 386 |
-
response_format={"type": "json_object"}
|
| 387 |
-
)
|
| 388 |
-
|
| 389 |
-
ai_response = json.loads(response.choices[0].message.content)
|
| 390 |
-
logger.info(f"AI response: {ai_response}")
|
| 391 |
-
|
| 392 |
-
return jsonify(ai_response)
|
| 393 |
-
|
| 394 |
-
except Exception as e:
|
| 395 |
-
logger.error(f"Error getting AI move: {str(e)}")
|
| 396 |
-
return jsonify({"error": str(e)}), 500
|
| 397 |
-
|
| 398 |
-
@app.route('/ai-chat', methods=['POST'])
|
| 399 |
-
def get_ai_chat():
|
| 400 |
-
try:
|
| 401 |
-
data = request.json
|
| 402 |
-
user_message = data.get('message', '')
|
| 403 |
-
board = data.get('board', [])
|
| 404 |
-
|
| 405 |
-
# Convert board to string representation
|
| 406 |
-
board_string = ""
|
| 407 |
-
for i in range(0, 9, 3):
|
| 408 |
-
row = [board[i] or ' ', board[i+1] or ' ', board[i+2] or ' ']
|
| 409 |
-
board_string += f"{row[0]} | {row[1]} | {row[2]}\n"
|
| 410 |
-
if i < 6:
|
| 411 |
-
board_string += "---------\n"
|
| 412 |
-
|
| 413 |
-
messages = [
|
| 414 |
-
{
|
| 415 |
-
"role": "system",
|
| 416 |
-
"content": f"""You are a competitive, witty Tic-Tac-Toe AI with personality. You're currently playing a game.
|
| 417 |
-
|
| 418 |
-
Current board state:
|
| 419 |
-
{board_string}
|
| 420 |
-
|
| 421 |
-
Respond to the human's message with personality - be competitive, funny, encouraging, or trash-talking as appropriate.
|
| 422 |
-
Keep responses under 50 words. Use emojis occasionally. Don't make game moves in chat - that happens separately."""
|
| 423 |
-
},
|
| 424 |
-
{
|
| 425 |
-
"role": "user",
|
| 426 |
-
"content": user_message
|
| 427 |
-
}
|
| 428 |
-
]
|
| 429 |
-
|
| 430 |
-
response = client.chat.complete(
|
| 431 |
-
model="mistral-large-latest",
|
| 432 |
-
messages=messages
|
| 433 |
-
)
|
| 434 |
-
|
| 435 |
-
ai_message = response.choices[0].message.content
|
| 436 |
-
logger.info(f"AI chat response: {ai_message}")
|
| 437 |
-
|
| 438 |
-
return jsonify({"message": ai_message})
|
| 439 |
-
|
| 440 |
-
except Exception as e:
|
| 441 |
-
logger.error(f"Error getting AI chat: {str(e)}")
|
| 442 |
-
return jsonify({"error": str(e)}), 500
|
| 443 |
-
|
| 444 |
-
# Serve the main game page (original)
|
| 445 |
@app.route('/')
|
| 446 |
def index():
|
| 447 |
-
return send_from_directory('.', 'simple_game.html')
|
| 448 |
-
|
| 449 |
-
# Serve the room-based game page
|
| 450 |
-
@app.route('/rooms-ui')
|
| 451 |
-
def rooms_ui():
|
| 452 |
return send_from_directory('.', 'room_game.html')
|
| 453 |
|
| 454 |
# Serve static files
|
|
@@ -457,4 +348,4 @@ def serve_static(path):
|
|
| 457 |
return send_from_directory('.', path)
|
| 458 |
|
| 459 |
if __name__ == '__main__':
|
| 460 |
-
app.run(host='0.0.0.0', port=
|
|
|
|
| 100 |
# Board representation
|
| 101 |
markdown += "```\n"
|
| 102 |
for i in range(0, 9, 3):
|
| 103 |
+
row = [self.board[i] or '路', self.board[i+1] or '路', self.board[i+2] or '路']
|
| 104 |
+
markdown += f"{row[0]} | {row[1]} | {row[2]}\n"
|
| 105 |
if i < 6:
|
| 106 |
markdown += "-----------\n"
|
| 107 |
markdown += "```\n\n"
|
|
|
|
| 337 |
|
| 338 |
return response.choices[0].message.content
|
| 339 |
|
| 340 |
+
# Serve the room-based game page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
@app.route('/')
|
| 342 |
def index():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
return send_from_directory('.', 'room_game.html')
|
| 344 |
|
| 345 |
# Serve static files
|
|
|
|
| 348 |
return send_from_directory('.', path)
|
| 349 |
|
| 350 |
if __name__ == '__main__':
|
| 351 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|