|
|
import gradio as gr |
|
|
import folium |
|
|
from folium.plugins import MousePosition |
|
|
import tempfile |
|
|
import scipy.io.wavfile |
|
|
import numpy as np |
|
|
from geopy.geocoders import Nominatim |
|
|
|
|
|
|
|
|
terrain_prompts = { |
|
|
"forest": "ambient electronic music with forest and bird sounds", |
|
|
"urban": "lo-fi chillhop with city ambience and soft beats", |
|
|
"highway": "energetic synthwave for night highway driving", |
|
|
"mountain": "cinematic orchestral music with epic strings", |
|
|
"rainy": "melancholic piano with background rain and thunder", |
|
|
"water": "relaxing ocean waves with ambient synths" |
|
|
} |
|
|
|
|
|
def classify_location(lat, lon): |
|
|
geolocator = Nominatim(user_agent="geo-audio-app") |
|
|
try: |
|
|
loc = geolocator.reverse((lat, lon), language='en', timeout=10) |
|
|
addr = loc.raw.get("address", {}) |
|
|
if "city" in addr or "town" in addr: |
|
|
return "urban" |
|
|
if "forest" in addr or "protected_area" in addr: |
|
|
return "forest" |
|
|
if "mountain" in addr: |
|
|
return "mountain" |
|
|
if "highway" in addr or "road" in addr: |
|
|
return "highway" |
|
|
if "water" in addr or "lake" in addr or "coast" in addr: |
|
|
return "water" |
|
|
except: |
|
|
pass |
|
|
return "urban" |
|
|
|
|
|
def synthesize_audio(prompt: str): |
|
|
|
|
|
sr = 16000 |
|
|
t = np.linspace(0, 5, sr * 5, endpoint=False) |
|
|
tone = 0.5 * np.sin(2 * np.pi * 440 * t).astype(np.float32) |
|
|
|
|
|
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") |
|
|
scipy.io.wavfile.write(tmp.name, rate=sr, data=(tone * 32767).astype(np.int16)) |
|
|
return tmp.name |
|
|
|
|
|
def generate_audio(lat, lon): |
|
|
terrain = classify_location(lat, lon) |
|
|
prompt = terrain_prompts.get(terrain, "ambient atmospheric music") |
|
|
audio_file = synthesize_audio(prompt) |
|
|
return terrain, prompt, audio_file |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# 🌍 Geo-Audio Synth with Gradio") |
|
|
coords = gr.Point(label="Click on map to select location", interactive=True) |
|
|
terrain_out = gr.Text(label="Detected terrain") |
|
|
prompt_out = gr.Text(label="Generated prompt") |
|
|
audio_out = gr.Audio(label="Simulated Audio") |
|
|
|
|
|
coords.change(fn=lambda pt: pt, inputs=coords, outputs=[]) |
|
|
coords.submit(generate_audio, inputs=[coords.x, coords.y], outputs=[terrain_out, prompt_out, audio_out]) |
|
|
|
|
|
demo.launch() |
|
|
|