import gradio as gr import requests import os from urllib.parse import urlparse, unquote def gradio_fn(url): if not url: return None try: api_url = f"https://eolithic-daniele-nonchivalrously.ngrok-free.app/dl?url={url}" response = requests.get(api_url) response.raise_for_status() data = response.json() mp3_url = data.get('download_link') if not mp3_url: return None mp3_response = requests.get(mp3_url) mp3_response.raise_for_status() # 👇 Ambil nama file dari URL parsed_url = urlparse(mp3_url) filename = unquote(os.path.basename(parsed_url.path)) if not filename or '.' not in filename: filename = "audio.mp3" # 👉 Simpan ke /tmp — HF Spaces mengizinkan ini! filepath = os.path.join("/tmp", filename) with open(filepath, "wb") as f: f.write(mp3_response.content) return filepath except Exception as e: print(f"Error: {e}") return None with gr.Blocks(title="YT/YT-Music Downloader") as demo: gr.Markdown("### Paste YouTube / YouTube-Music link → 320 k MP3 with tags") with gr.Row(): inp = gr.Textbox(label="URL", placeholder=" https://www.youtube.com/watch?v=...") btn = gr.Button("Download", variant="primary") out = gr.File(label="MP3") btn.click(gradio_fn, inputs=inp, outputs=out) demo.launch()