Spaces:
Sleeping
Sleeping
align data response with frontend
Browse files- app.py +24 -67
- services/game_service.py +14 -3
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
|
@@ -57,72 +58,6 @@ class JokerUse(BaseModel):
|
|
| 57 |
joker_type: str
|
| 58 |
|
| 59 |
|
| 60 |
-
# After your imports and before the main endpoints, add:
|
| 61 |
-
|
| 62 |
-
@app.get("/api/test/ping")
|
| 63 |
-
async def ping():
|
| 64 |
-
"""Basic ping endpoint to check if the service is responding"""
|
| 65 |
-
return {"status": "ok", "message": "pong"}
|
| 66 |
-
|
| 67 |
-
@app.get("/api/test/model")
|
| 68 |
-
async def test_model():
|
| 69 |
-
"""Test if the word embedding model is loaded and working"""
|
| 70 |
-
try:
|
| 71 |
-
# Test basic similarity calculation
|
| 72 |
-
similarity = word_service.calculate_similarity("bonjour", "salut")
|
| 73 |
-
return {
|
| 74 |
-
"status": "ok",
|
| 75 |
-
"message": "Model is working",
|
| 76 |
-
"test_similarity": {
|
| 77 |
-
"word1": "bonjour",
|
| 78 |
-
"word2": "salut",
|
| 79 |
-
"similarity": similarity
|
| 80 |
-
}
|
| 81 |
-
}
|
| 82 |
-
except Exception as e:
|
| 83 |
-
logger.error(f"Model test failed: {str(e)}")
|
| 84 |
-
return {
|
| 85 |
-
"status": "error",
|
| 86 |
-
"message": str(e)
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
@app.get("/api/test/env")
|
| 90 |
-
async def test_environment():
|
| 91 |
-
"""Get information about the running environment"""
|
| 92 |
-
return {
|
| 93 |
-
"status": "ok",
|
| 94 |
-
"environment": {
|
| 95 |
-
"python_version": platform.python_version(),
|
| 96 |
-
"host": socket.gethostname(),
|
| 97 |
-
"model_url": os.getenv('MODEL_URL'),
|
| 98 |
-
"services_initialized": {
|
| 99 |
-
"word_service": word_service is not None,
|
| 100 |
-
"game_service": game_service is not None,
|
| 101 |
-
"visualization_service": visualization_service is not None
|
| 102 |
-
}
|
| 103 |
-
}
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
@app.get("/api/test/model-info")
|
| 107 |
-
async def test_model_info():
|
| 108 |
-
"""Get information about the loaded model"""
|
| 109 |
-
try:
|
| 110 |
-
vocab_size = len(word_service.vocab_vectors)
|
| 111 |
-
sample_words = list(word_service.vocab_vectors.keys())[:5] # Get first 5 words
|
| 112 |
-
return {
|
| 113 |
-
"status": "ok",
|
| 114 |
-
"model_info": {
|
| 115 |
-
"vocabulary_size": vocab_size,
|
| 116 |
-
"sample_words": sample_words,
|
| 117 |
-
}
|
| 118 |
-
}
|
| 119 |
-
except Exception as e:
|
| 120 |
-
logger.error(f"Error getting model info: {str(e)}")
|
| 121 |
-
return {
|
| 122 |
-
"status": "error",
|
| 123 |
-
"message": str(e)
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
@app.get("/api/game-state")
|
| 127 |
async def get_game_state():
|
| 128 |
try:
|
|
@@ -134,7 +69,9 @@ async def get_game_state():
|
|
| 134 |
@app.post("/api/check-word")
|
| 135 |
async def check_word(word_check: WordCheck):
|
| 136 |
try:
|
| 137 |
-
|
|
|
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
logger.error(f"Error checking word: {str(e)}")
|
| 140 |
raise HTTPException(status_code=500, detail="Internal server error")
|
|
@@ -169,6 +106,26 @@ async def get_visualization():
|
|
| 169 |
raise HTTPException(status_code=500, detail="Internal server error")
|
| 170 |
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
|
| 174 |
@app.get("/")
|
|
|
|
| 1 |
+
# semantix-api/app.py
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
|
|
|
| 58 |
joker_type: str
|
| 59 |
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@app.get("/api/game-state")
|
| 62 |
async def get_game_state():
|
| 63 |
try:
|
|
|
|
| 69 |
@app.post("/api/check-word")
|
| 70 |
async def check_word(word_check: WordCheck):
|
| 71 |
try:
|
| 72 |
+
response = game_service.check_word(word_check.word)
|
| 73 |
+
print(f"Response sent: {response}")
|
| 74 |
+
return response
|
| 75 |
except Exception as e:
|
| 76 |
logger.error(f"Error checking word: {str(e)}")
|
| 77 |
raise HTTPException(status_code=500, detail="Internal server error")
|
|
|
|
| 106 |
raise HTTPException(status_code=500, detail="Internal server error")
|
| 107 |
|
| 108 |
|
| 109 |
+
# Add new health models
|
| 110 |
+
class HealthResponse(BaseModel):
|
| 111 |
+
status: str
|
| 112 |
+
version: str = "1.0.0"
|
| 113 |
+
model_loaded: bool
|
| 114 |
+
|
| 115 |
+
@app.get("/api/health")
|
| 116 |
+
async def health_check():
|
| 117 |
+
"""Health check endpoint"""
|
| 118 |
+
try:
|
| 119 |
+
# Check if model is loaded by accessing word service
|
| 120 |
+
model_loaded = word_service._model is not None
|
| 121 |
+
return {
|
| 122 |
+
"status": "healthy" if model_loaded else "degraded",
|
| 123 |
+
"version": "1.0.0",
|
| 124 |
+
"model_loaded": model_loaded
|
| 125 |
+
}
|
| 126 |
+
except Exception as e:
|
| 127 |
+
logger.exception("Health check failed")
|
| 128 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 129 |
|
| 130 |
|
| 131 |
@app.get("/")
|
services/game_service.py
CHANGED
|
@@ -197,7 +197,7 @@ class GameService:
|
|
| 197 |
def check_word(self, word: str) -> Dict:
|
| 198 |
"""
|
| 199 |
Check a word against the target word and save the attempt.
|
| 200 |
-
Returns the
|
| 201 |
"""
|
| 202 |
try:
|
| 203 |
state = self._load_state()
|
|
@@ -207,9 +207,20 @@ class GameService:
|
|
| 207 |
similarity = self.word_service.calculate_similarity(word, target_word)
|
| 208 |
logger.info(f"Word: {word}, Target: {target_word}, Similarity: {similarity:.3f}")
|
| 209 |
|
| 210 |
-
# Save attempt and
|
| 211 |
-
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
except Exception:
|
| 214 |
logger.exception(f"Error checking word: {word}")
|
| 215 |
raise
|
|
|
|
| 197 |
def check_word(self, word: str) -> Dict:
|
| 198 |
"""
|
| 199 |
Check a word against the target word and save the attempt.
|
| 200 |
+
Returns a response matching the frontend's GameResponse interface.
|
| 201 |
"""
|
| 202 |
try:
|
| 203 |
state = self._load_state()
|
|
|
|
| 207 |
similarity = self.word_service.calculate_similarity(word, target_word)
|
| 208 |
logger.info(f"Word: {word}, Target: {target_word}, Similarity: {similarity:.3f}")
|
| 209 |
|
| 210 |
+
# Save attempt and get updated state
|
| 211 |
+
updated_state = self.save_attempt(word, similarity)
|
| 212 |
|
| 213 |
+
# Format response to match frontend expectations
|
| 214 |
+
response = {
|
| 215 |
+
"similarity": similarity,
|
| 216 |
+
"history": updated_state['attempts'],
|
| 217 |
+
"word_found": updated_state['word_found'],
|
| 218 |
+
"similar_words": updated_state.get('similar_words', []) # Use get with default empty list
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
logger.info(f"Sending response: {response}")
|
| 222 |
+
return response
|
| 223 |
+
|
| 224 |
except Exception:
|
| 225 |
logger.exception(f"Error checking word: {word}")
|
| 226 |
raise
|