from __future__ import annotations import csv import datetime as dt import math import random from dataclasses import dataclass from pathlib import Path from typing import Dict, List, Optional, Tuple import numpy as np if not hasattr(np, "bool"): # pandas<2 compatibility on numpy>=2 np.bool = bool # type: ignore[attr-defined] EXAMPLE_PROMPT = "## Input-Output Example. When you submit an answer, the following lines will display the last input and the corresponding correct answer." LAST_PROMPT = "## The input and the correct output of the last question." import pandas as pd from PIL import Image, ImageDraw import plotly.graph_objects as go def _create_plotly_map(marker_lat: Optional[float] = None, marker_lon: Optional[float] = None): """Create a Plotly map figure (OpenStreetMap) with optional visible marker. Also adds a dense transparent grid of points so plotly_click 能在任意位置触发。 """ # Dense transparent grid points to capture clicks anywhere on the map grid_lats: List[float] = [] grid_lons: List[float] = [] # 1° 网格,尽量保证任意点击都能命中到一个数据点 for lat in range(-90, 91, 1): for lon in range(-180, 181, 1): grid_lats.append(float(lat)) grid_lons.append(float(lon)) fig = go.Figure() fig.add_trace( go.Scattermap( lat=grid_lats, lon=grid_lons, mode="markers", marker=go.scattermap.Marker(size=40, color="#000"), opacity=0.01, # 透明但可点中 hoverinfo="skip", name="grid", ) ) if marker_lat is not None and marker_lon is not None: fig.add_trace( go.Scattermap( lat=[marker_lat], lon=[marker_lon], mode="markers", marker=go.scattermap.Marker(size=16, color="red"), hoverinfo="text", text=["Your guess"], name="marker", ) ) fig.update_layout( map=dict(style="open-street-map", center=dict(lat=20, lon=0), zoom=2), margin=dict(l=0, r=0, t=0, b=0), height=600, clickmode="event+select", # 明确开启点击事件 dragmode="pan", ) return fig import gradio as gr BASE_DIR = Path(__file__).resolve().parent DATASET_PATH = BASE_DIR / "data" / "dataset.csv" AUDIO_BASE_DIR = BASE_DIR / "data" / "audios" LOG_PATH = BASE_DIR / "player_runs.csv" RECENT_COLUMNS = ["timestamp", "player_id", "question_id", "distance_km"] @dataclass class Sample: question_id: str audio_path: Path longitude: float latitude: float city: str country: str continent: str description: str title: str def _load_samples() -> List[Sample]: if not DATASET_PATH.exists(): raise FileNotFoundError(f"Dataset not found at {DATASET_PATH}") df = pd.read_csv(DATASET_PATH) start_idx = int(len(df) * 0.9) test_df = df.iloc[start_idx:].reset_index(drop=True) samples: List[Sample] = [] missing_audio = 0 missing_coords = 0 for row in test_df.itertuples(): audio_path = AUDIO_BASE_DIR / getattr(row, "mp3name") longitude = getattr(row, "longitude") latitude = getattr(row, "latitude") if math.isnan(longitude) or math.isnan(latitude): missing_coords += 1 continue if not audio_path.exists(): missing_audio += 1 continue samples.append( Sample( question_id=str(getattr(row, "key")), audio_path=audio_path, longitude=float(longitude), latitude=float(latitude), city=str(getattr(row, "city", "") or ""), country=str(getattr(row, "country", "") or ""), continent=str(getattr(row, "continent", "") or ""), description=str(getattr(row, "description", "") or ""), title=str(getattr(row, "title", "") or ""), ) ) if not samples: raise RuntimeError("No playable samples were found in the test split.") if missing_audio: print(f"[game_app] Skipped {missing_audio} samples because audio files are missing.") if missing_coords: print(f"[game_app] Skipped {missing_coords} samples because coordinates are missing.") return samples def _get_example_sample() -> Optional[Sample]: """Get the example sample (aporee_45102_51242) for the first round.""" if not DATASET_PATH.exists(): return None df = pd.read_csv(DATASET_PATH) example_row = df[df["key"] == "aporee_45102_51242"] if example_row.empty: return None row = example_row.iloc[0] audio_path = AUDIO_BASE_DIR / row["mp3name"] if not audio_path.exists(): return None return Sample( question_id=str(row["key"]), audio_path=audio_path, longitude=float(row["longitude"]), latitude=float(row["latitude"]), city=str(row.get("city", "") or ""), country=str(row.get("country", "") or ""), continent=str(row.get("continent", "") or ""), description=str(row.get("description", "") or ""), title=str(row.get("title", "") or ""), ) SAMPLES: List[Sample] = _load_samples() def _random_queue() -> List[int]: queue = list(range(len(SAMPLES))) random.shuffle(queue) return queue def _latlon_to_text(lat: float, lon: float) -> str: return f"Selected latitude {lat:.3f}°, longitude {lon:.3f}°" def _haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float: r = 6371.0 phi1, phi2 = math.radians(lat1), math.radians(lat2) d_phi = math.radians(lat2 - lat1) d_lambda = math.radians(lon2 - lon1) a = math.sin(d_phi / 2.0) ** 2 + math.cos(phi1) * math.cos(phi2) * math.sin(d_lambda / 2.0) ** 2 c = 2.0 * math.atan2(math.sqrt(a), math.sqrt(1.0 - a)) return r * c def _base_map_array() -> np.ndarray: return np.array(BASE_MAP_IMAGE) def _on_coords_change(lon_text: str, lat_text: str) -> None: """Log coordinate changes to backend terminal for debugging.""" try: lon = float(lon_text) if lon_text else None lat = float(lat_text) if lat_text else None print(f"[PLOT CLICK] lon={lon} lat={lat}") except Exception as e: print(f"[PLOT CLICK] parse error: {e} | lon_text={lon_text} lat_text={lat_text}") def _prepare_clip_info(sample: Sample, round_idx: int) -> str: intro_lines = [ f"## Question ID: {sample.question_id}. Now it's your turn to play." ] return "\n\n".join(intro_lines) def _append_log(entry: Dict[str, object]) -> None: write_header = not LOG_PATH.exists() with LOG_PATH.open("a", newline="", encoding="utf-8") as f: writer = csv.DictWriter(f, fieldnames=list(entry.keys())) if write_header: writer.writeheader() writer.writerow(entry) def _load_recent_runs(limit: int = 5) -> List[Dict[str, object]]: if not LOG_PATH.exists(): return [] rows = [] with LOG_PATH.open("r", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: rows.append(row) return rows[-limit:] def _format_recent_rows(rows: List[Dict[str, object]]) -> List[List[object]]: formatted: List[List[object]] = [] for row in rows: timestamp_str = row.get("timestamp", "") if timestamp_str: try: # Parse ISO format timestamp and format as readable string dt_obj = dt.datetime.fromisoformat(str(timestamp_str).replace('Z', '+00:00')) formatted_timestamp = dt_obj.strftime("%Y-%m-%d %H:%M:%S") except (ValueError, AttributeError): formatted_timestamp = str(timestamp_str) else: formatted_timestamp = "" formatted_row = [formatted_timestamp] formatted_row.extend([row.get(col, "") for col in RECENT_COLUMNS[1:]]) formatted.append(formatted_row) return formatted def _format_previous_answer(sample: Optional[Sample], is_example: bool = False, distance_km: Optional[float] = None, guess_lat: Optional[float] = None, guess_lon: Optional[float] = None) -> str: """Format the previous answer display with location info and OpenStreetMap link.""" if sample is None: return "" lat = sample.latitude lon = sample.longitude city = sample.city or "Unknown city" country = sample.country or "Unknown country" continent = sample.continent or "Unknown continent" # Create OpenStreetMap link osm_link = f"https://www.openstreetmap.org/?mlat={lat}&mlon={lon}&zoom=15" lines = [ f"**Location:** {city}, {country}, {continent}", f"**Coordinates:** {lat:.6f}°, {lon:.6f}°", ] # Add player's prediction for non-example cases if not is_example and guess_lat is not None and guess_lon is not None: lines.append(f"**Your Prediction:** {guess_lat:.6f}°, {guess_lon:.6f}°") # Add error distance for non-example cases if not is_example and distance_km is not None: lines.append(f"**Error distance:** {distance_km:.1f} km") lines.append(f"**OpenStreetMap:** [View on map]({osm_link})") # Add example explanation if it's the example sample if is_example and sample.question_id == "aporee_45102_51242": lines.append("") lines.append("**Explanation:** I can clearly identify the rolling of polyurethane wheels on concrete, the sharp 'pop' of a skateboard's tail hitting the ground to initiate a trick. This firmly places the recording at a skatepark. The background ambiance is that of a bustling city, characterized by the constant, though somewhat distant, hum of traffic. Furthermore, there are several instances of spoken English. The speakers have a clear North American English accent. The combination points to a major city in the United States with a prominent skate culture. New York City is one of the most iconic locations for street skateboarding globally.") return "\n\n".join(lines) def initialize_round() -> Tuple[Dict[str, object], Dict[str, Optional[float]], str, str, str, str, List[List[object]], str, str, str, str, str]: queue = _random_queue() current_index = queue.pop() state = { "queue": queue, "current_index": current_index, "round": 1, "previous_sample": None, # No previous sample for first round "is_example": True, # First round shows example } current_sample = SAMPLES[current_index] audio_value = str(current_sample.audio_path) clip_md = _prepare_clip_info(current_sample, state["round"]) prompt_text = "Click once on the map to pick your guess. The marker will update to your last selection." guess_state = {"lat": None, "lon": None, "pixel": None} recent_runs = _format_recent_rows(_load_recent_runs()) # For first round, show example sample example_sample = _get_example_sample() previous_audio = str(example_sample.audio_path) if example_sample else None previous_answer = _format_previous_answer(example_sample, is_example=True) if example_sample else "" previous_title = EXAMPLE_PROMPT if example_sample else "" return state, guess_state, audio_value, clip_md, _create_plotly_map(), prompt_text, recent_runs, "", "", previous_audio or "", previous_answer, previous_title def _next_sample(state: Dict[str, object]) -> Sample: queue: List[int] = state["queue"] if not queue: queue.extend(_random_queue()) next_index = queue.pop() state["current_index"] = next_index state["round"] = state.get("round", 1) + 1 return SAMPLES[next_index] def _ensure_guess_state(state: Optional[Dict[str, Optional[float]]]) -> Dict[str, Optional[float]]: if not isinstance(state, dict): return {"lat": None, "lon": None, "pixel": None} return { "lat": state.get("lat"), "lon": state.get("lon"), "pixel": state.get("pixel"), } def submit_guess( player_id: str, game_state: Optional[Dict[str, object]], guess_state: Optional[Dict[str, Optional[float]]], longitude: str, latitude: str, ) -> Tuple[ Dict[str, object], Dict[str, Optional[float]], str, str, str, str, List[List[object]], str, str, str, str, str, ]: if game_state is None: # Re-initialize if state is lost return initialize_round() guess_state = _ensure_guess_state(guess_state) player_id = (player_id or "").strip() if not player_id: message = "Please enter your player ID before submitting." current_sample = SAMPLES[game_state["current_index"]] clip_md = _prepare_clip_info(current_sample, game_state.get("round", 1)) prompt_text = message previous_sample = game_state.get("previous_sample") previous_audio = str(previous_sample.audio_path) if previous_sample else "" previous_distance = game_state.get("previous_distance_km") previous_guess_lat = game_state.get("previous_guess_lat") previous_guess_lon = game_state.get("previous_guess_lon") previous_answer = _format_previous_answer(previous_sample, distance_km=previous_distance, guess_lat=previous_guess_lat, guess_lon=previous_guess_lon) if previous_sample else "" is_example = game_state.get("is_example", False) previous_title = EXAMPLE_PROMPT if is_example else "" return ( game_state, guess_state, str(current_sample.audio_path), clip_md, _create_plotly_map(), prompt_text, _format_recent_rows(_load_recent_runs()), longitude, latitude, previous_audio, previous_answer, previous_title, ) # Parse longitude and latitude from text inputs try: longitude = longitude.strip() if longitude else "" latitude = latitude.strip() if latitude else "" if not longitude or not latitude: message = "Please enter both longitude and latitude, or click on the map to select a location." current_sample = SAMPLES[game_state["current_index"]] clip_md = _prepare_clip_info(current_sample, game_state.get("round", 1)) prompt_text = message previous_sample = game_state.get("previous_sample") previous_audio = str(previous_sample.audio_path) if previous_sample else "" previous_distance = game_state.get("previous_distance_km") previous_answer = _format_previous_answer(previous_sample, distance_km=previous_distance) if previous_sample else "" is_example = game_state.get("is_example", False) previous_title = EXAMPLE_PROMPT if is_example else "" return ( game_state, guess_state, str(current_sample.audio_path), clip_md, gr.update(), prompt_text, _format_recent_rows(_load_recent_runs()), longitude, latitude, previous_audio, previous_answer, previous_title, ) guess_lon = float(longitude) guess_lat = float(latitude) # Validate ranges if not (-180 <= guess_lon <= 180): message = "Longitude must be between -180 and 180." current_sample = SAMPLES[game_state["current_index"]] clip_md = _prepare_clip_info(current_sample, game_state.get("round", 1)) prompt_text = message previous_sample = game_state.get("previous_sample") previous_audio = str(previous_sample.audio_path) if previous_sample else "" previous_distance = game_state.get("previous_distance_km") previous_answer = _format_previous_answer(previous_sample, distance_km=previous_distance) if previous_sample else "" is_example = game_state.get("is_example", False) previous_title = EXAMPLE_PROMPT if is_example else "" return ( game_state, guess_state, str(current_sample.audio_path), clip_md, gr.update(), prompt_text, _format_recent_rows(_load_recent_runs()), longitude, latitude, previous_audio, previous_answer, previous_title, ) if not (-90 <= guess_lat <= 90): message = "Latitude must be between -90 and 90." current_sample = SAMPLES[game_state["current_index"]] clip_md = _prepare_clip_info(current_sample, game_state.get("round", 1)) prompt_text = message previous_sample = game_state.get("previous_sample") previous_audio = str(previous_sample.audio_path) if previous_sample else "" previous_distance = game_state.get("previous_distance_km") previous_guess_lat = game_state.get("previous_guess_lat") previous_guess_lon = game_state.get("previous_guess_lon") previous_answer = _format_previous_answer(previous_sample, distance_km=previous_distance, guess_lat=previous_guess_lat, guess_lon=previous_guess_lon) if previous_sample else "" is_example = game_state.get("is_example", False) previous_title = EXAMPLE_PROMPT if is_example else LAST_PROMPT return ( game_state, guess_state, str(current_sample.audio_path), clip_md, gr.update(), prompt_text, _format_recent_rows(_load_recent_runs()), longitude, latitude, previous_audio, previous_answer, previous_title, ) except ValueError: message = "Invalid coordinates. Please enter valid numbers." current_sample = SAMPLES[game_state["current_index"]] clip_md = _prepare_clip_info(current_sample, game_state.get("round", 1)) prompt_text = message previous_sample = game_state.get("previous_sample") previous_audio = str(previous_sample.audio_path) if previous_sample else "" previous_distance = game_state.get("previous_distance_km") previous_guess_lat = game_state.get("previous_guess_lat") previous_guess_lon = game_state.get("previous_guess_lon") previous_answer = _format_previous_answer(previous_sample, distance_km=previous_distance, guess_lat=previous_guess_lat, guess_lon=previous_guess_lon) if previous_sample else "" is_example = game_state.get("is_example", False) previous_title = EXAMPLE_PROMPT if is_example else LAST_PROMPT return ( game_state, guess_state, str(current_sample.audio_path), clip_md, _create_plotly_map(), prompt_text, _format_recent_rows(_load_recent_runs()), longitude, latitude, previous_audio, previous_answer, previous_title, ) current_sample = SAMPLES[game_state["current_index"]] true_lat = current_sample.latitude true_lon = current_sample.longitude distance_km = _haversine(true_lat, true_lon, guess_lat, guess_lon) reveal_lines = [ f"Real location: {current_sample.city or 'Unknown city'}, {current_sample.country or 'Unknown country'} ({true_lat:.3f}°, {true_lon:.3f}°)", f"Your guess: ({guess_lat:.3f}°, {guess_lon:.3f}°)", f"Error distance: {distance_km:.1f} km", ] log_entry = { "timestamp": dt.datetime.utcnow().isoformat(), "player_id": player_id, "question_id": current_sample.question_id, "audio_path": str(current_sample.audio_path), "guess_latitude": guess_lat, "guess_longitude": guess_lon, "true_latitude": true_lat, "true_longitude": true_lon, "distance_km": round(distance_km, 3), "city": current_sample.city, "country": current_sample.country, "continent": current_sample.continent, "title": current_sample.title, "description": current_sample.description, } _append_log(log_entry) # Save current sample and distance as previous for next round game_state["previous_sample"] = current_sample game_state["previous_distance_km"] = distance_km game_state["previous_guess_lat"] = guess_lat game_state["previous_guess_lon"] = guess_lon game_state["is_example"] = False # After first round, it's no longer example next_sample = _next_sample(game_state) audio_value = str(next_sample.audio_path) clip_md = _prepare_clip_info(next_sample, game_state["round"]) new_guess_state = {"lat": None, "lon": None, "pixel": None} prompt_text = "Click once on the map to pick your guess. The marker will update to your last selection." recent_runs = _format_recent_rows(_load_recent_runs()) # Format previous answer (current sample becomes previous for next round) previous_audio = str(current_sample.audio_path) previous_distance = game_state.get("previous_distance_km") previous_guess_lat = game_state.get("previous_guess_lat") previous_guess_lon = game_state.get("previous_guess_lon") previous_answer = _format_previous_answer(current_sample, is_example=False, distance_km=previous_distance, guess_lat=previous_guess_lat, guess_lon=previous_guess_lon) previous_title = LAST_PROMPT return ( game_state, new_guess_state, audio_value, clip_md, gr.update(), prompt_text, recent_runs, "", # Reset longitude input "", # Reset latitude input previous_audio, previous_answer, previous_title, ) custom_css = """ h1 { font-size: 2.5rem !important; font-weight: 700 !important; margin-bottom: 1rem !important; text-align: center !important; } .intro-text { font-size: 1.1rem !important; line-height: 1.6 !important; margin-bottom: 1.5rem !important; color: #555 !important; text-align: center !important; } /* Apply size restrictions to regular (non-fullscreen) image */ .gradio-image:not([class*="fullscreen"]) { max-width: 100% !important; max-height: none !important; height: auto !important; margin: 0 auto !important; display: block !important; } .gradio-image:not([class*="fullscreen"]) > div { max-width: 100% !important; max-height: none !important; height: auto !important; margin: 0 auto !important; display: block !important; } .gradio-image:not([class*="fullscreen"]) iframe, .gradio-image:not([class*="fullscreen"]) div[id^="map-"] { max-width: 100% !important; max-height: none !important; height: 800px !important; min-height: 800px !important; width: 100% !important; margin: 0 auto !important; display: block !important; } .gradio-image:not([class*="fullscreen"]) img { max-width: 100% !important; max-height: 600px !important; width: auto !important; height: auto !important; object-fit: contain !important; margin: 0 auto !important; } /* Ensure map plot has consistent height */ .gradio-plot { min-height: 500px !important; height: 500px !important; } /* Make sure the map column aligns properly */ .gradio-row > .gradio-column:first-child { display: flex !important; flex-direction: column !important; } .gradio-row > .gradio-column:first-child > * { flex-shrink: 0 !important; } /* Allow fullscreen modal to override size restrictions - higher specificity */ /* Target common Gradio fullscreen containers */ div[class*="modal"] .gradio-image, div[class*="modal"] .gradio-image > div, div[class*="modal"] .gradio-image img, div[class*="fullscreen"] .gradio-image, div[class*="fullscreen"] .gradio-image > div, div[class*="fullscreen"] .gradio-image img, div[id*="lightbox"] .gradio-image, div[id*="lightbox"] .gradio-image img, .gradio-image[class*="fullscreen"], .gradio-image[class*="fullscreen"] > div, .gradio-image[class*="fullscreen"] img { max-width: none !important; max-height: none !important; width: 100% !important; height: 100% !important; } .selection-text { font-size: 1.15rem !important; text-align: left !important; color: #666 !important; margin: 0.5rem 0 !important; } .selection-text * { font-size: 1.15rem !important; } .feedback-box { font-size: 1.15rem !important; line-height: 1.6 !important; } .feedback-box p, .feedback-box div, .feedback-box span { font-size: 1.15rem !important; line-height: 1.6 !important; } .clip-info { font-size: 1.1rem !important; } .clip-info p, .clip-info div, .clip-info span { font-size: 1.1rem !important; } .clip-info label { font-size: 1.1rem !important; font-weight: 600 !important; } label { font-size: 1rem !important; font-weight: 500 !important; } .form-text input { font-size: 0.95rem !important; } /* Table label styling - only target the label, not table content */ .gradio-dataframe label, [data-testid="dataframe"] label, label[for*="recent"], .form-group label { font-size: 1.15rem !important; font-weight: 600 !important; } """ with gr.Blocks(title="Audio Geo-Localization Game", theme=gr.themes.Soft(), css=custom_css) as demo: gr.Markdown("# Audio Geo-Localization Game") gr.HTML('

Welcome to the Audio Geo-Localization Game. Listen to an ambient audio clip, then guess where it was recorded by clicking on the world map. Submit to see the true location and how close you came.

') game_state = gr.State() guess_state = gr.State() # New row for previous question audio and answer with title previous_title_display = gr.Markdown(value="", elem_classes=["clip-info"]) with gr.Row(): with gr.Column(scale=1): previous_audio_player = gr.Audio( label="Previous audio clip", autoplay=False, interactive=False, streaming=False, visible=True ) with gr.Column(scale=1): previous_answer_display = gr.Markdown( label="Previous answer", value="", elem_classes=["clip-info"] ) # Add separator/divider between previous and current round gr.Markdown("---") clip_info = gr.Markdown(label="Clip details", elem_classes=["clip-info"]) with gr.Row(): with gr.Column(scale=2): map_component = gr.Plot( value=_create_plotly_map(), label="World map (click to set your guess)", elem_classes=["gradio-image"] ) with gr.Column(scale=1): player_id = gr.Textbox(label="Player ID", placeholder="Enter an identifier so scores can be tracked", elem_classes=["form-text"]) audio_player = gr.Audio(label="Mystery audio clip", autoplay=False, interactive=False, streaming=False) selection_text = gr.Markdown("Click once on the map to pick your guess. The marker will update to your last selection.", elem_classes=["selection-text"]) with gr.Row(): longitude_input = gr.Textbox( label="Longitude", placeholder="Enter longitude or click map", elem_classes=["form-text"], elem_id="lon_input" ) latitude_input = gr.Textbox( label="Latitude", placeholder="Enter latitude or click map", elem_classes=["form-text"], elem_id="lat_input" ) submit_button = gr.Button("Submit Guess", variant="primary") recent_table = gr.Dataframe( headers=[ "timestamp", "player_id", "question_id", "distance_km", ], datatype=["str", "str", "str", "number"], value=[], interactive=False, label="Recent submissions (latest last)", wrap=True, ) demo.load( initialize_round, inputs=None, outputs=[game_state, guess_state, audio_player, clip_info, map_component, selection_text, recent_table, longitude_input, latitude_input, previous_audio_player, previous_answer_display, previous_title_display] ) # 后端监听文本框变化,在终端打印坐标(便于你观察点击数据是否到达后端) longitude_input.change(_on_coords_change, inputs=[longitude_input, latitude_input], outputs=[]) latitude_input.change(_on_coords_change, inputs=[longitude_input, latitude_input], outputs=[]) # Attach plotly_click to update the lon/lat textboxes (no Python roundtrip) plotly_click_js = r""" () => { console.log('[PLOTLY SETUP] Starting plotly click handler setup...'); const updateInputs = (lat, lon) => { console.log('[PLOTLY CLICK] Received coordinates:', {lat, lon}); // Method 1: Try Gradio component API if (window.gradio_app) { try { const components = window.gradio_app.__components || {}; Object.keys(components).forEach(key => { const comp = components[key]; if (comp && comp.props && comp.props.elem_id) { if (comp.props.elem_id === 'lon_input' && comp.component) { comp.component.value = Number(lon).toFixed(6); comp.component.dispatch('change'); console.log('[PLOTLY CLICK] Updated longitude via Gradio API'); } if (comp.props.elem_id === 'lat_input' && comp.component) { comp.component.value = Number(lat).toFixed(6); comp.component.dispatch('change'); console.log('[PLOTLY CLICK] Updated latitude via Gradio API'); } } }); } catch (e) { console.log('[PLOTLY CLICK] Gradio API method failed:', e); } } // Method 2: Find by label text, then find input nearby const allLabels = Array.from(document.querySelectorAll('label')); allLabels.forEach(label => { const labelText = label.textContent.toLowerCase(); if (labelText.includes('longitude')) { const container = label.closest('div, form, .gr-box'); if (container) { const input = container.querySelector('input, textarea'); if (input) { input.value = Number(lon).toFixed(6); input.dispatchEvent(new Event('input', { bubbles: true })); input.dispatchEvent(new Event('change', { bubbles: true })); console.log('[PLOTLY CLICK] Updated longitude via label:', input.value); } } } if (labelText.includes('latitude')) { const container = label.closest('div, form, .gr-box'); if (container) { const input = container.querySelector('input, textarea'); if (input) { input.value = Number(lat).toFixed(6); input.dispatchEvent(new Event('input', { bubbles: true })); input.dispatchEvent(new Event('change', { bubbles: true })); console.log('[PLOTLY CLICK] Updated latitude via label:', input.value); } } } }); // Method 3: Try finding by placeholder in all possible elements const allElements = document.querySelectorAll('input, textarea, [contenteditable="true"]'); allElements.forEach(el => { const ph = (el.placeholder || el.getAttribute('placeholder') || '').toLowerCase(); if (ph.includes('longitude') && !ph.includes('latitude')) { if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') { el.value = Number(lon).toFixed(6); el.dispatchEvent(new Event('input', { bubbles: true })); el.dispatchEvent(new Event('change', { bubbles: true })); console.log('[PLOTLY CLICK] Updated longitude via placeholder:', el.value); } } if (ph.includes('latitude') && !ph.includes('longitude')) { if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') { el.value = Number(lat).toFixed(6); el.dispatchEvent(new Event('input', { bubbles: true })); el.dispatchEvent(new Event('change', { bubbles: true })); console.log('[PLOTLY CLICK] Updated latitude via placeholder:', el.value); } } }); console.log('[PLOTLY CLICK] Update attempts completed'); }; const wirePlotly = () => { const plots = document.querySelectorAll('.js-plotly-plot'); console.log('[PLOTLY SETUP] Found', plots.length, 'plotly plots'); plots.forEach((plotDiv, idx) => { if (plotDiv.dataset._wired === '1') { console.log('[PLOTLY SETUP] Plot', idx, 'already wired'); return; } console.log('[PLOTLY SETUP] Wiring plot', idx); plotDiv.dataset._wired = '1'; // Listen for plotly_click DOM event (Plotly fires this as a custom event) plotDiv.addEventListener('plotly_click', (e) => { console.log('[PLOTLY CLICK] Event received:', e); console.log('[PLOTLY CLICK] e.points:', e.points); // Plotly click event data is directly on the event object let points = e.points; if (!points && e.detail) { points = e.detail.points; } if (points && points.length > 0) { const pt = points[0]; console.log('[PLOTLY CLICK] Point data:', pt); const lat = pt.lat; const lon = pt.lon; console.log('[PLOTLY CLICK] Extracted lat:', lat, 'lon:', lon); if (lat !== undefined && lon !== undefined && !isNaN(lat) && !isNaN(lon)) { updateInputs(lat, lon); } else { console.error('[PLOTLY CLICK] Invalid coordinates:', {lat, lon, point: pt}); } } else { console.error('[PLOTLY CLICK] No points found in event'); } }); // Fallback: Listen for raw click events and calculate coordinates from map config plotDiv.addEventListener('click', (e) => { console.log('[PLOTLY CLICK] Raw click event:', e); try { if (!plotDiv._fullLayout || !plotDiv._fullLayout.map) { console.error('[PLOTLY CLICK] map layout not available'); return; } const mapConfig = plotDiv._fullLayout.map; const center = mapConfig.center || {lat: 0, lon: 0}; const zoom = mapConfig.zoom || 2; console.log('[PLOTLY CLICK] Map config:', {center, zoom}); // Get click position relative to plot div const rect = plotDiv.getBoundingClientRect(); const width = rect.width; const height = rect.height; const x = e.clientX - rect.left; const y = e.clientY - rect.top; console.log('[PLOTLY CLICK] Click position:', {x, y, width, height}); // Convert pixel coordinates to lat/lon using Web Mercator projection // Simplified calculation based on map center and zoom const normalizedX = (x / width) - 0.5; // -0.5 to 0.5 const normalizedY = 0.5 - (y / height); // 0.5 to -0.5 (flipped Y) // Calculate pixel-to-degree conversion at current zoom // At zoom level z, one tile = 256 pixels, world = 2^z tiles wide const tilesAtZoom = Math.pow(2, zoom); const pixelsPerTile = 256; const worldWidthPixels = tilesAtZoom * pixelsPerTile; const worldWidthDegrees = 360; const degreesPerPixel = worldWidthDegrees / worldWidthPixels; // Longitude: linear mapping const lon = center.lon + (normalizedX * width * degreesPerPixel); // Latitude: Web Mercator inverse // Mercator Y at center: y_center = ln(tan(π/4 + center_lat/2)) // Click Y offset in pixels: dy = normalizedY * height // Convert to Mercator units and then to latitude const mercatorYCenter = Math.log(Math.tan(Math.PI / 4 + (center.lat * Math.PI / 180) / 2)); const dyMercator = normalizedY * height * degreesPerPixel / (180 / Math.PI); const mercatorYClick = mercatorYCenter + dyMercator; const latRad = 2 * Math.atan(Math.exp(mercatorYClick)) - Math.PI / 2; const lat = latRad * 180 / Math.PI; console.log('[PLOTLY CLICK] Calculated coordinates - lat:', lat, 'lon:', lon); if (!isNaN(lat) && !isNaN(lon) && lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180) { updateInputs(lat, lon); } else { console.error('[PLOTLY CLICK] Invalid calculated coordinates:', {lat, lon}); } } catch (err) { console.error('[PLOTLY CLICK] Error in click handler:', err); console.error('[PLOTLY CLICK] Error stack:', err.stack); } }); }); }; // Initial wire with retry logic let retryCount = 0; const maxRetries = 10; const tryWire = () => { const plots = document.querySelectorAll('.js-plotly-plot'); if (plots.length === 0 && retryCount < maxRetries) { retryCount++; console.log('[PLOTLY SETUP] No plots found, retry', retryCount, '/', maxRetries); setTimeout(tryWire, 500); return; } wirePlotly(); console.log('[PLOTLY SETUP] Initial wire complete, found', plots.length, 'plots'); }; // Wait for Plotly library to load if (window.Plotly) { console.log('[PLOTLY SETUP] Plotly library found immediately'); setTimeout(tryWire, 1500); } else { console.warn('[PLOTLY SETUP] Plotly library not found, waiting...'); let plotlyWaitCount = 0; const waitForPlotly = () => { if (window.Plotly) { console.log('[PLOTLY SETUP] Plotly library loaded after', plotlyWaitCount * 200, 'ms'); setTimeout(tryWire, 1000); } else if (plotlyWaitCount < 20) { plotlyWaitCount++; setTimeout(waitForPlotly, 200); } else { console.error('[PLOTLY SETUP] Plotly library not found after 4 seconds, proceeding anyway'); setTimeout(tryWire, 1000); } }; waitForPlotly(); } // Watch for new plots const obs = new MutationObserver(() => { setTimeout(wirePlotly, 500); }); obs.observe(document.body, { childList: true, subtree: true }); } """ demo.load(None, js=plotly_click_js) submit_button.click( submit_guess, inputs=[player_id, game_state, guess_state, longitude_input, latitude_input], outputs=[game_state, guess_state, audio_player, clip_info, map_component, selection_text, recent_table, longitude_input, latitude_input, previous_audio_player, previous_answer_display, previous_title_display], ) if __name__ == "__main__": # Allow Gradio to serve audio files that live outside the CWD by whitelisting the project dir demo.launch()