XOFSOUNDIA commited on
Commit
b39d5d8
·
verified ·
1 Parent(s): 60f89fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -2,27 +2,35 @@ import gradio as gr
2
  import replicate
3
  import os
4
  from pytube import YouTube
 
5
 
6
  REPLICATE_API_TOKEN = os.environ.get("REPLICATE_API_TOKEN")
7
 
 
 
 
 
 
8
  def generate_from_youtube(url, style, ambiance):
9
  try:
10
- yt = YouTube(url)
 
11
  title = yt.title
12
- thumb_url = yt.thumbnail_url
13
  prompt = f"Miniature YouTube pour un clip {style}, ambiance {ambiance}, titre: {title}"
14
  except:
15
  prompt = f"Miniature YouTube {style}, ambiance {ambiance}"
16
 
17
- output = replicate.run(
18
- "stability-ai/stable-diffusion",
19
- input={
20
- "prompt": prompt,
21
- "image_dimensions": "512x512"
22
- }
23
- )
24
-
25
- return output[0]
 
 
26
 
27
  style_choices = ["Rap", "Afro", "Techno", "R&B", "Pop", "Reggae", "Drill", "Autre"]
28
 
@@ -33,7 +41,7 @@ interface = gr.Interface(
33
  gr.Dropdown(style_choices, label="Style musical"),
34
  gr.Textbox(label="Ambiance / Détail artistique (ex : sombre, festif, futuriste...)"),
35
  ],
36
- outputs=gr.Image(type="pil"),
37
  title="YT Thumbnail Generator 🎬",
38
  description="Colle un lien YouTube et génère automatiquement une miniature stylisée."
39
  )
 
2
  import replicate
3
  import os
4
  from pytube import YouTube
5
+ from urllib.parse import urlparse, parse_qs
6
 
7
  REPLICATE_API_TOKEN = os.environ.get("REPLICATE_API_TOKEN")
8
 
9
+ def clean_youtube_url(url):
10
+ parsed_url = urlparse(url)
11
+ video_id = parse_qs(parsed_url.query).get("v", [None])[0]
12
+ return f"https://www.youtube.com/watch?v={video_id}" if video_id else None
13
+
14
  def generate_from_youtube(url, style, ambiance):
15
  try:
16
+ clean_url = clean_youtube_url(url)
17
+ yt = YouTube(clean_url)
18
  title = yt.title
 
19
  prompt = f"Miniature YouTube pour un clip {style}, ambiance {ambiance}, titre: {title}"
20
  except:
21
  prompt = f"Miniature YouTube {style}, ambiance {ambiance}"
22
 
23
+ try:
24
+ output = replicate.run(
25
+ "stability-ai/stable-diffusion",
26
+ input={
27
+ "prompt": prompt,
28
+ "image_dimensions": "512x512"
29
+ }
30
+ )
31
+ return output[0]
32
+ except Exception as e:
33
+ return f"Erreur génération : {str(e)}"
34
 
35
  style_choices = ["Rap", "Afro", "Techno", "R&B", "Pop", "Reggae", "Drill", "Autre"]
36
 
 
41
  gr.Dropdown(style_choices, label="Style musical"),
42
  gr.Textbox(label="Ambiance / Détail artistique (ex : sombre, festif, futuriste...)"),
43
  ],
44
+ outputs=gr.Image(type="pil", label="Miniature générée"),
45
  title="YT Thumbnail Generator 🎬",
46
  description="Colle un lien YouTube et génère automatiquement une miniature stylisée."
47
  )