Spaces:
Running
on
Zero
Running
on
Zero
Joffrey Thomas
commited on
Commit
·
02817fd
1
Parent(s):
279bdde
Blockign multiple games from being played
Browse files- app.py +21 -2
- data_manager.py +19 -6
app.py
CHANGED
|
@@ -755,7 +755,8 @@ with gr.Blocks(css=APP_CSS, title="LLM GeoGuessr") as demo:
|
|
| 755 |
profile = whoami(token=token.token)
|
| 756 |
username = profile["name"]
|
| 757 |
todays_games = data_manager.get_todays_games(token=token.token)
|
| 758 |
-
|
|
|
|
| 759 |
except Exception as e:
|
| 760 |
gr.Warning(f"Could not check your game status. Please try again. Error: {e}")
|
| 761 |
return (
|
|
@@ -870,6 +871,8 @@ with gr.Blocks(css=APP_CSS, title="LLM GeoGuessr") as demo:
|
|
| 870 |
ai_dist_km = haversine_km(rnd['lat'], rnd['lng'], ai_coords['lat'], ai_coords['lng'])
|
| 871 |
rnd['ai_distance_km'] = float(ai_dist_km)
|
| 872 |
rnd['ai_score'] = score_from_distance_km(ai_dist_km)
|
|
|
|
|
|
|
| 873 |
except Exception as e:
|
| 874 |
yield "", txt + f"\n\n[Error] {e}"
|
| 875 |
return
|
|
@@ -943,7 +946,23 @@ with gr.Blocks(css=APP_CSS, title="LLM GeoGuessr") as demo:
|
|
| 943 |
total_human = sum(float(r.get('human_score', 0.0)) for r in sess.get('rounds', []))
|
| 944 |
total_ai = sum(float(r.get('ai_score', 0.0)) for r in sess.get('rounds', []))
|
| 945 |
|
| 946 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 947 |
|
| 948 |
winner_message = "It's a tie!"
|
| 949 |
if total_human > total_ai:
|
|
|
|
| 755 |
profile = whoami(token=token.token)
|
| 756 |
username = profile["name"]
|
| 757 |
todays_games = data_manager.get_todays_games(token=token.token)
|
| 758 |
+
# Only check if user played today if BLOCK_MULTIPLE_GAMES is enabled
|
| 759 |
+
has_played = data_manager.BLOCK_MULTIPLE_GAMES and data_manager.has_user_played_today(username, todays_games)
|
| 760 |
except Exception as e:
|
| 761 |
gr.Warning(f"Could not check your game status. Please try again. Error: {e}")
|
| 762 |
return (
|
|
|
|
| 871 |
ai_dist_km = haversine_km(rnd['lat'], rnd['lng'], ai_coords['lat'], ai_coords['lng'])
|
| 872 |
rnd['ai_distance_km'] = float(ai_dist_km)
|
| 873 |
rnd['ai_score'] = score_from_distance_km(ai_dist_km)
|
| 874 |
+
# Store the AI analysis text for later recording
|
| 875 |
+
rnd['ai_analysis'] = last_text
|
| 876 |
except Exception as e:
|
| 877 |
yield "", txt + f"\n\n[Error] {e}"
|
| 878 |
return
|
|
|
|
| 946 |
total_human = sum(float(r.get('human_score', 0.0)) for r in sess.get('rounds', []))
|
| 947 |
total_ai = sum(float(r.get('ai_score', 0.0)) for r in sess.get('rounds', []))
|
| 948 |
|
| 949 |
+
# Prepare detailed rounds data for recording
|
| 950 |
+
rounds_data = []
|
| 951 |
+
for i, r in enumerate(sess.get('rounds', [])):
|
| 952 |
+
round_record = {
|
| 953 |
+
"round_number": i + 1,
|
| 954 |
+
"actual_location": {"lat": r.get('lat'), "lng": r.get('lng')},
|
| 955 |
+
"human_guess": r.get('human_guess'),
|
| 956 |
+
"human_distance_km": r.get('human_distance_km'),
|
| 957 |
+
"human_score": r.get('human_score'),
|
| 958 |
+
"ai_guess": r.get('ai_guess'),
|
| 959 |
+
"ai_distance_km": r.get('ai_distance_km'),
|
| 960 |
+
"ai_score": r.get('ai_score'),
|
| 961 |
+
"ai_analysis": r.get('ai_analysis', ''),
|
| 962 |
+
}
|
| 963 |
+
rounds_data.append(round_record)
|
| 964 |
+
|
| 965 |
+
data_manager.record_game(username, total_human, rounds_data)
|
| 966 |
|
| 967 |
winner_message = "It's a tie!"
|
| 968 |
if total_human > total_ai:
|
data_manager.py
CHANGED
|
@@ -6,7 +6,8 @@ from huggingface_hub import hf_hub_download, upload_file
|
|
| 6 |
from huggingface_hub.utils import HfHubHTTPError
|
| 7 |
|
| 8 |
# Constant for the dataset repository, configurable via environment variable
|
| 9 |
-
DATASET_REPO = os.getenv("HF_DATASET_REPO", "
|
|
|
|
| 10 |
|
| 11 |
def get_todays_records_path() -> str:
|
| 12 |
"""Gets the path for today's game records file, e.g., 'records/2025-10-03.json'."""
|
|
@@ -43,11 +44,16 @@ def has_user_played_today(username: str, todays_games: list) -> bool:
|
|
| 43 |
"""Checks if a user's record already exists in today's games."""
|
| 44 |
return any(game.get("username") == username for game in todays_games)
|
| 45 |
|
| 46 |
-
def record_game(username: str, score: float):
|
| 47 |
"""
|
| 48 |
Records a completed game to the daily records file on the HF Hub.
|
| 49 |
This function reads the existing file, appends the new record, and uploads it back.
|
| 50 |
It uses the server's write token from environment variables.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
"""
|
| 52 |
write_token = os.getenv("HF_TOKEN", "")
|
| 53 |
if not write_token:
|
|
@@ -59,15 +65,22 @@ def record_game(username: str, score: float):
|
|
| 59 |
todays_games = get_todays_games(token=write_token)
|
| 60 |
|
| 61 |
# Final check to prevent duplicate entries in case of concurrent games
|
| 62 |
-
if
|
|
|
|
| 63 |
print(f"User {username} has already played today. Skipping record.")
|
| 64 |
return
|
| 65 |
|
| 66 |
-
|
| 67 |
"username": username,
|
| 68 |
"score": score,
|
| 69 |
-
"timestamp": datetime.now(timezone.utc).isoformat()
|
| 70 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
filepath_in_repo = get_todays_records_path()
|
| 73 |
|
|
|
|
| 6 |
from huggingface_hub.utils import HfHubHTTPError
|
| 7 |
|
| 8 |
# Constant for the dataset repository, configurable via environment variable
|
| 9 |
+
DATASET_REPO = os.getenv("HF_DATASET_REPO", "Jofthomas/geoguessr_game_of_the_day")
|
| 10 |
+
BLOCK_MULTIPLE_GAMES = os.getenv("BLOCK_MULTIPLE_GAMES", "True").lower() == "true"
|
| 11 |
|
| 12 |
def get_todays_records_path() -> str:
|
| 13 |
"""Gets the path for today's game records file, e.g., 'records/2025-10-03.json'."""
|
|
|
|
| 44 |
"""Checks if a user's record already exists in today's games."""
|
| 45 |
return any(game.get("username") == username for game in todays_games)
|
| 46 |
|
| 47 |
+
def record_game(username: str, score: float, rounds_data: list = None):
|
| 48 |
"""
|
| 49 |
Records a completed game to the daily records file on the HF Hub.
|
| 50 |
This function reads the existing file, appends the new record, and uploads it back.
|
| 51 |
It uses the server's write token from environment variables.
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
username: The player's username
|
| 55 |
+
score: Total score for the game
|
| 56 |
+
rounds_data: List of round details including guesses, scores, and AI analysis
|
| 57 |
"""
|
| 58 |
write_token = os.getenv("HF_TOKEN", "")
|
| 59 |
if not write_token:
|
|
|
|
| 65 |
todays_games = get_todays_games(token=write_token)
|
| 66 |
|
| 67 |
# Final check to prevent duplicate entries in case of concurrent games
|
| 68 |
+
# Only check if BLOCK_MULTIPLE_GAMES is enabled
|
| 69 |
+
if BLOCK_MULTIPLE_GAMES and has_user_played_today(username, todays_games):
|
| 70 |
print(f"User {username} has already played today. Skipping record.")
|
| 71 |
return
|
| 72 |
|
| 73 |
+
game_record = {
|
| 74 |
"username": username,
|
| 75 |
"score": score,
|
| 76 |
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# Add detailed rounds data if provided
|
| 80 |
+
if rounds_data:
|
| 81 |
+
game_record["rounds"] = rounds_data
|
| 82 |
+
|
| 83 |
+
todays_games.append(game_record)
|
| 84 |
|
| 85 |
filepath_in_repo = get_todays_records_path()
|
| 86 |
|