Spaces:
Sleeping
Sleeping
File size: 2,320 Bytes
c0c69ba d2241cc b39d5d8 2902e50 c0c69ba c1c787a c0c69ba c1c787a b39d5d8 c1c787a d2241cc b39d5d8 d2241cc 2902e50 c1c787a d2241cc c0c69ba b39d5d8 2902e50 c1c787a 2902e50 1518386 b39d5d8 c0c69ba c1c787a d2241cc c1c787a c0c69ba d2241cc b39d5d8 c0c69ba d2241cc c0c69ba c1c787a c0c69ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import gradio as gr
import replicate
import os
from pytube import YouTube
from urllib.parse import urlparse, parse_qs
import requests
from PIL import Image
from io import BytesIO
# Clé API Replicate récupérée depuis Hugging Face secrets
REPLICATE_API_TOKEN = os.environ.get("REPLICATE_API_TOKEN")
# Nettoyage d'URL YouTube
def clean_youtube_url(url):
parsed_url = urlparse(url)
video_id = parse_qs(parsed_url.query).get("v", [None])[0]
return f"https://www.youtube.com/watch?v={video_id}" if video_id else None
# Fonction principale de génération
def generate_from_youtube(url, style, ambiance):
try:
clean_url = clean_youtube_url(url)
yt = YouTube(clean_url)
title = yt.title
prompt = f"Miniature YouTube pour un clip {style}, ambiance {ambiance}, titre : {title}"
except Exception as e:
prompt = f"Miniature YouTube {style}, ambiance {ambiance}"
try:
output = replicate.run(
"stability-ai/stable-diffusion",
input={
"prompt": prompt,
"image_dimensions": "512x512"
}
)
# Débogage : afficher la sortie brute
print("Résultat Replicate:", output)
if not output or not isinstance(output, list) or not output[0].startswith("http"):
return f"Erreur : le modèle n'a pas renvoyé une image valide.\nRetour : {output}"
# Télécharger l'image et la convertir en objet PIL
response = requests.get(output[0])
image = Image.open(BytesIO(response.content))
return image
except Exception as e:
return f"Erreur génération : {str(e)}"
# Choix de styles musicaux proposés
style_choices = ["Rap", "Afro", "Techno", "R&B", "Pop", "Reggae", "Drill", "Autre"]
# Interface Gradio
interface = gr.Interface(
fn=generate_from_youtube,
inputs=[
gr.Textbox(label="Lien YouTube du clip"),
gr.Dropdown(style_choices, label="Style musical"),
gr.Textbox(label="Ambiance / Détail artistique (ex : sombre, festif, futuriste...)"),
],
outputs=gr.Image(type="pil", label="Miniature générée"),
title="YT Thumbnail Generator 🎬",
description="Colle un lien YouTube et génère automatiquement une miniature stylisée."
)
# Lancement
interface.launch() |