RisingZhang commited on
Commit
83b8779
·
1 Parent(s): 5a713ec

remove image

Browse files
Files changed (1) hide show
  1. app.py +3 -103
app.py CHANGED
@@ -69,12 +69,10 @@ import gradio as gr
69
 
70
 
71
 
72
- BASE_DIR = Path(__file__).resolve().parents[1]
73
  DATASET_PATH = BASE_DIR / "data" / "dataset.csv"
74
  AUDIO_BASE_DIR = BASE_DIR / "data" / "audios"
75
- ASSETS_DIR = Path(__file__).resolve().parent / "assets"
76
- MAP_IMAGE_PATH = ASSETS_DIR / "world_map.png"
77
- LOG_PATH = Path(__file__).resolve().parent / "player_runs.csv"
78
  RECENT_COLUMNS = ["timestamp", "player_id", "question_id", "distance_km"]
79
 
80
 
@@ -172,8 +170,6 @@ def _get_example_sample() -> Optional[Sample]:
172
 
173
 
174
  SAMPLES: List[Sample] = _load_samples()
175
- BASE_MAP_IMAGE = Image.open(MAP_IMAGE_PATH).convert("RGB")
176
- MAP_WIDTH, MAP_HEIGHT = BASE_MAP_IMAGE.size
177
 
178
 
179
  def _random_queue() -> List[int]:
@@ -182,11 +178,6 @@ def _random_queue() -> List[int]:
182
  return queue
183
 
184
 
185
- def _pixel_to_latlon(x: int, y: int) -> Tuple[float, float]:
186
- lon = (x / (MAP_WIDTH - 1)) * 360.0 - 180.0
187
- lat = 90.0 - (y / (MAP_HEIGHT - 1)) * 180.0
188
- return round(lat, 6), round(lon, 6)
189
-
190
 
191
  def _latlon_to_text(lat: float, lon: float) -> str:
192
  return f"Selected latitude {lat:.3f}°, longitude {lon:.3f}°"
@@ -203,24 +194,6 @@ def _haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
203
  return r * c
204
 
205
 
206
- def _map_with_marker(x: int, y: int) -> np.ndarray:
207
- marker_radius = max(6, MAP_WIDTH // 150)
208
- img = BASE_MAP_IMAGE.copy()
209
- draw = ImageDraw.Draw(img)
210
- draw.ellipse(
211
- (
212
- x - marker_radius,
213
- y - marker_radius,
214
- x + marker_radius,
215
- y + marker_radius,
216
- ),
217
- fill=(225, 64, 64),
218
- outline=(0, 0, 0),
219
- width=2,
220
- )
221
- return np.array(img)
222
-
223
-
224
  def _base_map_array() -> np.ndarray:
225
  return np.array(BASE_MAP_IMAGE)
226
 
@@ -371,79 +344,6 @@ def _ensure_guess_state(state: Optional[Dict[str, Optional[float]]]) -> Dict[str
371
  }
372
 
373
 
374
- def handle_map_click(
375
- evt: gr.SelectData,
376
- current_guess_state: Optional[Dict[str, Optional[float]]],
377
- ) -> Tuple[np.ndarray, str, Dict[str, Optional[float]], str, str]:
378
- """Handle Plotly map click event to get coordinates."""
379
- guess_state = _ensure_guess_state(current_guess_state)
380
-
381
- if evt is None:
382
- return _base_map_array(), "Unable to read selection. Please try again.", guess_state, "", ""
383
-
384
- # Extract coordinates from Plotly SelectData
385
- # Plotly SelectData for mapbox may have different structure
386
- try:
387
- lat = None
388
- lon = None
389
-
390
- # Try different ways to extract coordinates
391
- if hasattr(evt, 'points') and evt.points:
392
- # Check if points is a list
393
- if isinstance(evt.points, list) and len(evt.points) > 0:
394
- point = evt.points[0]
395
- if isinstance(point, dict):
396
- lat = point.get('lat') or point.get('latitude')
397
- lon = point.get('lon') or point.get('longitude')
398
-
399
- # Try index attribute
400
- if (lat is None or lon is None) and hasattr(evt, 'index'):
401
- if isinstance(evt.index, dict):
402
- lat = evt.index.get('lat') or evt.index.get('latitude')
403
- lon = evt.index.get('lon') or evt.index.get('longitude')
404
-
405
- # Try value attribute
406
- if (lat is None or lon is None) and hasattr(evt, 'value'):
407
- if isinstance(evt.value, dict):
408
- lat = evt.value.get('lat') or evt.value.get('latitude')
409
- lon = evt.value.get('lon') or evt.value.get('longitude')
410
- # Also check nested points
411
- if (lat is None or lon is None) and 'points' in evt.value:
412
- points = evt.value['points']
413
- if isinstance(points, list) and len(points) > 0:
414
- point = points[0]
415
- if isinstance(point, dict):
416
- lat = point.get('lat') or point.get('latitude')
417
- lon = point.get('lon') or point.get('longitude')
418
-
419
- # Debug: print the event structure
420
- print(f"[MAP] SelectData event structure: {type(evt)}")
421
- print(f"[MAP] Has points: {hasattr(evt, 'points')}")
422
- print(f"[MAP] Has index: {hasattr(evt, 'index')}")
423
- print(f"[MAP] Has value: {hasattr(evt, 'value')}")
424
- if hasattr(evt, 'points'):
425
- print(f"[MAP] Points: {evt.points}")
426
- if hasattr(evt, 'index'):
427
- print(f"[MAP] Index: {evt.index}")
428
- if hasattr(evt, 'value'):
429
- print(f"[MAP] Value: {evt.value}")
430
-
431
- if lat is None or lon is None:
432
- return _base_map_array(), "Unable to read coordinates. Please try clicking on the map again.", guess_state, "", ""
433
-
434
- # Update guess state
435
- guess_state = {"lat": float(lat), "lon": float(lon), "pixel": None}
436
- # Create map with marker at clicked location
437
- x = int((lon + 180.0) / 360.0 * MAP_WIDTH)
438
- y = int((90.0 - lat) / 180.0 * MAP_HEIGHT)
439
- image_with_marker = _map_with_marker(x, y)
440
- return image_with_marker, _latlon_to_text(lat, lon), guess_state, f"{lon:.6f}", f"{lat:.6f}"
441
- except Exception as e:
442
- import traceback
443
- print(f"[MAP] Error handling click: {e}")
444
- print(f"[MAP] Traceback: {traceback.format_exc()}")
445
- return _base_map_array(), f"Error: {str(e)}", guess_state, "", ""
446
-
447
 
448
  def submit_guess(
449
  player_id: str,
@@ -1147,4 +1047,4 @@ with gr.Blocks(title="Audio Geo-Localization Game", theme=gr.themes.Soft(), css=
1147
 
1148
  if __name__ == "__main__":
1149
  # Allow Gradio to serve audio files that live outside the CWD by whitelisting the project dir
1150
- demo.launch(server_name="0.0.0.0", server_port=38339, allowed_paths=[str(BASE_DIR)])
 
69
 
70
 
71
 
72
+ BASE_DIR = "."
73
  DATASET_PATH = BASE_DIR / "data" / "dataset.csv"
74
  AUDIO_BASE_DIR = BASE_DIR / "data" / "audios"
75
+ LOG_PATH = BASE_DIR / "player_runs.csv"
 
 
76
  RECENT_COLUMNS = ["timestamp", "player_id", "question_id", "distance_km"]
77
 
78
 
 
170
 
171
 
172
  SAMPLES: List[Sample] = _load_samples()
 
 
173
 
174
 
175
  def _random_queue() -> List[int]:
 
178
  return queue
179
 
180
 
 
 
 
 
 
181
 
182
  def _latlon_to_text(lat: float, lon: float) -> str:
183
  return f"Selected latitude {lat:.3f}°, longitude {lon:.3f}°"
 
194
  return r * c
195
 
196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  def _base_map_array() -> np.ndarray:
198
  return np.array(BASE_MAP_IMAGE)
199
 
 
344
  }
345
 
346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
 
348
  def submit_guess(
349
  player_id: str,
 
1047
 
1048
  if __name__ == "__main__":
1049
  # Allow Gradio to serve audio files that live outside the CWD by whitelisting the project dir
1050
+ demo.launch()