Spaces:
Running
Running
Update backend/main.py
Browse files- backend/main.py +129 -117
backend/main.py
CHANGED
|
@@ -1,117 +1,129 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
import os
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
# Import our Agents
|
| 7 |
-
from agents.scenario_agent import ScenarioAgent
|
| 8 |
-
from agents.adaptive_agent import AdaptiveAgent
|
| 9 |
-
from agents.hint_agent import HintAgent
|
| 10 |
-
|
| 11 |
-
# Load environment variables
|
| 12 |
-
load_dotenv()
|
| 13 |
-
|
| 14 |
-
app = Flask(__name__)
|
| 15 |
-
CORS(app) # Enable CORS for all routes
|
| 16 |
-
|
| 17 |
-
# Initialize Agents
|
| 18 |
-
scenario_agent = ScenarioAgent()
|
| 19 |
-
adaptive_agent = AdaptiveAgent()
|
| 20 |
-
hint_agent = HintAgent()
|
| 21 |
-
|
| 22 |
-
@app.route('/
|
| 23 |
-
def
|
| 24 |
-
return jsonify({
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Import our Agents
|
| 7 |
+
from agents.scenario_agent import ScenarioAgent
|
| 8 |
+
from agents.adaptive_agent import AdaptiveAgent
|
| 9 |
+
from agents.hint_agent import HintAgent
|
| 10 |
+
|
| 11 |
+
# Load environment variables
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
app = Flask(__name__)
|
| 15 |
+
CORS(app) # Enable CORS for all routes
|
| 16 |
+
|
| 17 |
+
# Initialize Agents
|
| 18 |
+
scenario_agent = ScenarioAgent()
|
| 19 |
+
adaptive_agent = AdaptiveAgent()
|
| 20 |
+
hint_agent = HintAgent()
|
| 21 |
+
|
| 22 |
+
@app.route('/', methods=['GET'])
|
| 23 |
+
def home():
|
| 24 |
+
return jsonify({
|
| 25 |
+
"status": "online",
|
| 26 |
+
"message": "CodeCracker AI Backend is Running!",
|
| 27 |
+
"endpoints": {
|
| 28 |
+
"health_check": "/health",
|
| 29 |
+
"generate_level": "/api/level/generate",
|
| 30 |
+
"feedback": "/api/level/feedback"
|
| 31 |
+
}
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
@app.route('/health', methods=['GET'])
|
| 35 |
+
def health_check():
|
| 36 |
+
return jsonify({"status": "healthy", "service": "CodeCracker AI Backend"})
|
| 37 |
+
|
| 38 |
+
@app.route('/api/level/generate', methods=['POST'])
|
| 39 |
+
def generate_level():
|
| 40 |
+
"""
|
| 41 |
+
Generates a new level (Maze, Blockly, or Time Challenge).
|
| 42 |
+
Expected JSON: { "mode": "maze", "difficulty": "Easy", "topic": "Space" (optional) }
|
| 43 |
+
"""
|
| 44 |
+
data = request.json
|
| 45 |
+
mode = data.get('mode', 'maze')
|
| 46 |
+
difficulty = data.get('difficulty', 'Easy')
|
| 47 |
+
topic = data.get('topic')
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
level_data = scenario_agent.generate_level(mode, difficulty, topic)
|
| 51 |
+
if not level_data:
|
| 52 |
+
raise Exception("Empty level data returned")
|
| 53 |
+
return jsonify(level_data)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"CRITICAL ERROR /api/level/generate: {e}")
|
| 56 |
+
# Final Safety Net: Return a basic Maze fallback if everything else explodes
|
| 57 |
+
fallback = {
|
| 58 |
+
"type": "maze",
|
| 59 |
+
"level_id": "emergency_fallback",
|
| 60 |
+
"title": "System Practice",
|
| 61 |
+
"story": "System offline. Practice mode engaged.",
|
| 62 |
+
"grid_layout": [[2,0,3]],
|
| 63 |
+
"allowed_blocks": ["move_forward"],
|
| 64 |
+
"tutorial_text": "Move to the goal."
|
| 65 |
+
}
|
| 66 |
+
return jsonify(fallback)
|
| 67 |
+
|
| 68 |
+
@app.route('/api/level/feedback', methods=['POST'])
|
| 69 |
+
def level_feedback():
|
| 70 |
+
"""
|
| 71 |
+
Receives user feedback (1-5 stars) on a level.
|
| 72 |
+
Expected JSON: { "level_data": {...}, "rating": 5 }
|
| 73 |
+
"""
|
| 74 |
+
data = request.json or {}
|
| 75 |
+
level_data = data.get('level_data')
|
| 76 |
+
rating = data.get('rating')
|
| 77 |
+
|
| 78 |
+
if not level_data or not rating:
|
| 79 |
+
return jsonify({"message": "Invalid data ignored"}), 400
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
# Teach the agent!
|
| 83 |
+
scenario_agent.learn_from_feedback(level_data, int(rating))
|
| 84 |
+
return jsonify({"status": "learned", "message": "Thanks for the feedback!"})
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Feedback Error: {e}")
|
| 87 |
+
return jsonify({"status": "ignored"}), 200
|
| 88 |
+
|
| 89 |
+
@app.route('/api/player/evaluate', methods=['POST'])
|
| 90 |
+
def evaluate_player():
|
| 91 |
+
"""
|
| 92 |
+
Evaluates player history to adjust difficulty.
|
| 93 |
+
Expected JSON: { "history": [ { "result": "success", "time_taken": 30 }, ... ] }
|
| 94 |
+
"""
|
| 95 |
+
data = request.json or {}
|
| 96 |
+
history = data.get('history', [])
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
evaluation = adaptive_agent.evaluate_user(history)
|
| 100 |
+
return jsonify(evaluation)
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"CRITICAL ERROR /api/player/evaluate: {e}")
|
| 103 |
+
# Fallback: Default to current or Easy
|
| 104 |
+
return jsonify({"difficulty": "Easy", "reason": "System requires calibration."})
|
| 105 |
+
|
| 106 |
+
@app.route('/api/hint/generate', methods=['POST'])
|
| 107 |
+
def generate_hint():
|
| 108 |
+
"""
|
| 109 |
+
Generates a context-aware hint.
|
| 110 |
+
Expected JSON: { "level_context": {...}, "user_code": "...", "error": "..." }
|
| 111 |
+
"""
|
| 112 |
+
data = request.json or {}
|
| 113 |
+
if 'user_code' not in data:
|
| 114 |
+
return jsonify({"error": "Missing user_code"}), 400
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
hint = hint_agent.generate_hint(
|
| 118 |
+
level_context=data.get('level_context', {}),
|
| 119 |
+
user_code=data.get('user_code'),
|
| 120 |
+
error_message=data.get('error')
|
| 121 |
+
)
|
| 122 |
+
return jsonify({"hint": hint})
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"CRITICAL ERROR /api/hint/generate: {e}")
|
| 125 |
+
return jsonify({"hint": "Try checking your syntax and logic step-by-step."})
|
| 126 |
+
|
| 127 |
+
if __name__ == '__main__':
|
| 128 |
+
port = int(os.environ.get('PORT', 5000))
|
| 129 |
+
app.run(host='0.0.0.0', port=port, debug=True)
|