Download app.py from p0000000/test: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/spaces/p0000000/test/resolve/main/app.py
- Command line
-
hf download hf://spaces/p0000000/test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/p0000000/test/resolve/main/app.py
2.54 kB
| import sys | |
| import os | |
| import subprocess | |
| import logging | |
| import asyncio | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse | |
| import chunking | |
| import extract_playlist_playwright | |
| import youtube_search | |
| # Installer Playwright et les navigateurs au démarrage | |
| try: | |
| subprocess.run(["playwright", "install"], check=True) | |
| print("Playwright browsers installed successfully.") | |
| except Exception as e: | |
| print(f"Erreur lors de l'installation des navigateurs Playwright : {e}") | |
| # Configuration de base du logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s [%(levelname)s] %(message)s", | |
| handlers=[logging.StreamHandler(sys.stdout)], | |
| ) | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI() | |
| origins = [ | |
| "http://localhost:8000", | |
| "https://ton-front-netlify-url", | |
| "file://", | |
| "*" | |
| ] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| async def extract(request: Request): | |
| data = await request.json() | |
| playlist_url = data.get("playlistUrl") | |
| logger.info(f"Requête reçue pour URL playlist : {playlist_url}") | |
| if not playlist_url: | |
| logger.warning("Playlist URL manquante dans la requête") | |
| return JSONResponse(status_code=400, content={"message": "Playlist URL manquante"}) | |
| try: | |
| logger.info("Début extraction playlist") | |
| songs = await extract_playlist_playwright.extract_playlist(playlist_url) | |
| logger.info(f"{len(songs)} morceaux extraits") | |
| batches = list(chunking.chunk_list(songs, 10)) | |
| logger.info(f"Découpage en {len(batches)} paquets") | |
| final_results = [] | |
| for i, batch in enumerate(batches, 1): | |
| logger.info(f"Traitement du paquet {i} / {len(batches)}") | |
| tasks = [youtube_search.search_youtube(song['title'], song['artist']) for song in batch] | |
| results = await asyncio.gather(*tasks) | |
| final_results.extend(results) | |
| logger.info("Fin enrichissement morceaux avec liens YouTube") | |
| return { | |
| "message": f"{len(songs)} morceaux extraits et enrichis en {len(batches)} paquets.", | |
| "playlist": final_results | |
| } | |
| except Exception as e: | |
| logger.error(f"Erreur pendant extraction / enrichissement : {e}", exc_info=True) | |
| return JSONResponse(status_code=500, content={"message": f"Erreur extraction : {str(e)}"}) | |