Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,100 @@
|
|
| 1 |
import os, uuid, subprocess, requests, feedparser, urllib.parse, re
|
| 2 |
import streamlit as st
|
| 3 |
from gtts import gTTS
|
|
|
|
| 4 |
|
| 5 |
-
# --- 1
|
| 6 |
-
def
|
| 7 |
try:
|
| 8 |
query = urllib.parse.quote(f"{joueur} football")
|
| 9 |
url = f"https://google.com{query}&hl=fr&gl=FR&ceid=FR:fr"
|
| 10 |
flux = feedparser.parse(url)
|
| 11 |
if flux.entries:
|
| 12 |
titre = flux.entries[0].title.split(" - ")[0]
|
| 13 |
-
return f"
|
| 14 |
-
return f"INFO : {joueur} continue d'impressionner avec une performance de classe mondiale."
|
| 15 |
except:
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
# --- 2
|
| 19 |
-
def
|
| 20 |
try:
|
| 21 |
-
query = urllib.parse.quote(f"{joueur} football
|
| 22 |
-
url = f"https://bing.com{query}&qft=+filterui:
|
| 23 |
headers = {"User-Agent": "Mozilla/5.0"}
|
| 24 |
r = requests.get(url, headers=headers, timeout=10)
|
|
|
|
| 25 |
links = re.findall(r'murl":"(http.*?)"', r.text)
|
| 26 |
if links:
|
| 27 |
path = f"img_{uuid.uuid4().hex[:4]}.jpg"
|
|
|
|
| 28 |
with open(path, 'wb') as f:
|
| 29 |
-
f.write(
|
| 30 |
return path
|
| 31 |
except:
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
# --- 3
|
| 35 |
-
def
|
| 36 |
uid = uuid.uuid4().hex[:6]
|
| 37 |
audio, video = f"audio_{uid}.mp3", f"PRISME_{uid}.mp4"
|
| 38 |
-
img =
|
| 39 |
|
| 40 |
-
# Formatage texte
|
| 41 |
-
mots =
|
| 42 |
-
|
| 43 |
for i, mot in enumerate(mots):
|
| 44 |
-
|
| 45 |
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
vf = (
|
| 53 |
-
|
|
|
|
| 54 |
f"drawtext=text='PRISME FOOT':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150,"
|
| 55 |
f"drawtext=text='{joueur.upper()}':fontcolor=white:fontsize=65:x=(w-text_w)/2:y=300:box=1:boxcolor=red@0.8,"
|
| 56 |
-
f"drawtext=text='{
|
| 57 |
)
|
| 58 |
|
| 59 |
cmd = ["ffmpeg", "-y"] + source + ["-i", audio, "-vf", vf, "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest", video]
|
| 60 |
subprocess.run(cmd, check=True)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
os.remove(
|
|
|
|
|
|
|
| 64 |
return video
|
| 65 |
-
except:
|
|
|
|
| 66 |
return None
|
| 67 |
|
| 68 |
-
# --- INTERFACE
|
| 69 |
st.set_page_config(page_title="Prisme Foot Studio", page_icon="⚽")
|
| 70 |
st.title("⚽ Prisme Foot Studio - Automatique")
|
| 71 |
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
if
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# Étape 2 : Créer la vidéo
|
| 81 |
-
video_path = generer_video_finale(nom_joueur, actu)
|
| 82 |
|
| 83 |
-
if
|
| 84 |
-
st.
|
| 85 |
-
st.
|
| 86 |
-
with open(
|
| 87 |
-
st.download_button("📥 Télécharger
|
| 88 |
else:
|
| 89 |
-
st.error("
|
| 90 |
else:
|
| 91 |
-
st.warning("Veuillez
|
|
|
|
| 1 |
import os, uuid, subprocess, requests, feedparser, urllib.parse, re
|
| 2 |
import streamlit as st
|
| 3 |
from gtts import gTTS
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
|
| 6 |
+
# --- FONCTION 1 : RÉCUPÉRER L'ACTUALITÉ RÉELLE ---
|
| 7 |
+
def chercher_actu(joueur):
|
| 8 |
try:
|
| 9 |
query = urllib.parse.quote(f"{joueur} football")
|
| 10 |
url = f"https://google.com{query}&hl=fr&gl=FR&ceid=FR:fr"
|
| 11 |
flux = feedparser.parse(url)
|
| 12 |
if flux.entries:
|
| 13 |
titre = flux.entries[0].title.split(" - ")[0]
|
| 14 |
+
return f"INFO : {titre}. Une performance qui fait vibrer le monde du football aujourd'hui."
|
|
|
|
| 15 |
except:
|
| 16 |
+
pass
|
| 17 |
+
return f"ACTUALITÉ : {joueur} continue d'impressionner sur le terrain cette saison."
|
| 18 |
|
| 19 |
+
# --- FONCTION 2 : RÉCUPÉRER L'IMAGE DE FOOT ---
|
| 20 |
+
def chercher_image(joueur):
|
| 21 |
try:
|
| 22 |
+
query = urllib.parse.quote(f"{joueur} football portrait")
|
| 23 |
+
url = f"https://bing.com{query}&qft=+filterui:imagesize-large"
|
| 24 |
headers = {"User-Agent": "Mozilla/5.0"}
|
| 25 |
r = requests.get(url, headers=headers, timeout=10)
|
| 26 |
+
# Extraction du lien image
|
| 27 |
links = re.findall(r'murl":"(http.*?)"', r.text)
|
| 28 |
if links:
|
| 29 |
path = f"img_{uuid.uuid4().hex[:4]}.jpg"
|
| 30 |
+
img_data = requests.get(links[0], timeout=10).content
|
| 31 |
with open(path, 'wb') as f:
|
| 32 |
+
f.write(img_data)
|
| 33 |
return path
|
| 34 |
except:
|
| 35 |
+
pass
|
| 36 |
+
return None
|
| 37 |
|
| 38 |
+
# --- FONCTION 3 : GÉNÉRER LA VIDÉO ---
|
| 39 |
+
def fabriquer_video(joueur, texte):
|
| 40 |
uid = uuid.uuid4().hex[:6]
|
| 41 |
audio, video = f"audio_{uid}.mp3", f"PRISME_{uid}.mp4"
|
| 42 |
+
img = chercher_image(joueur)
|
| 43 |
|
| 44 |
+
# Formatage du texte (Retour à la ligne tous les 5 mots)
|
| 45 |
+
mots = texte.split()
|
| 46 |
+
texte_formate = ""
|
| 47 |
for i, mot in enumerate(mots):
|
| 48 |
+
texte_formate += mot + ("\\n" if (i+1) % 5 == 0 else " ")
|
| 49 |
|
| 50 |
try:
|
| 51 |
+
# 1. Créer l'audio
|
| 52 |
+
tts = gTTS(text=texte, lang='fr')
|
| 53 |
+
tts.save(audio)
|
| 54 |
+
|
| 55 |
+
# 2. Définir la source (Image ou Fond Noir)
|
| 56 |
+
source = ["-loop", "1", "-i", img] if img else ["-f", "lavfi", "-i", "color=c=0x101010:s=720x1280"]
|
| 57 |
+
|
| 58 |
+
# 3. Filtre FFmpeg (Portrait + Texte centré)
|
| 59 |
vf = (
|
| 60 |
+
"scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,"
|
| 61 |
+
"drawbox=y=ih/2-150:color=black@0.6:width=iw:height=400:t=fill,"
|
| 62 |
f"drawtext=text='PRISME FOOT':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150,"
|
| 63 |
f"drawtext=text='{joueur.upper()}':fontcolor=white:fontsize=65:x=(w-text_w)/2:y=300:box=1:boxcolor=red@0.8,"
|
| 64 |
+
f"drawtext=text='{texte_formate}':fontcolor=white:fontsize=32:x=(w-text_w)/2:y=(h-text_h)/2+80"
|
| 65 |
)
|
| 66 |
|
| 67 |
cmd = ["ffmpeg", "-y"] + source + ["-i", audio, "-vf", vf, "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest", video]
|
| 68 |
subprocess.run(cmd, check=True)
|
| 69 |
|
| 70 |
+
# Nettoyage
|
| 71 |
+
if img and os.path.exists(img): os.remove(img)
|
| 72 |
+
if os.path.exists(audio): os.remove(audio)
|
| 73 |
+
|
| 74 |
return video
|
| 75 |
+
except Exception as e:
|
| 76 |
+
st.error(f"Erreur technique : {e}")
|
| 77 |
return None
|
| 78 |
|
| 79 |
+
# --- INTERFACE UNIQUE ---
|
| 80 |
st.set_page_config(page_title="Prisme Foot Studio", page_icon="⚽")
|
| 81 |
st.title("⚽ Prisme Foot Studio - Automatique")
|
| 82 |
|
| 83 |
+
joueur_input = st.text_input("Nom du joueur", placeholder="Ex: Kylian Mbappé")
|
| 84 |
|
| 85 |
+
if st.button("🚀 CRÉER LA VIDÉO"):
|
| 86 |
+
if joueur_input:
|
| 87 |
+
with st.spinner("Pipeline en cours (News + Image + Vidéo)..."):
|
| 88 |
+
# Lancement automatique
|
| 89 |
+
actu = chercher_actu(joueur_input)
|
| 90 |
+
video_finale = fabriquer_video(joueur_input, actu)
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
if video_finale:
|
| 93 |
+
st.video(video_finale)
|
| 94 |
+
st.success("Vidéo prête !")
|
| 95 |
+
with open(video_finale, "rb") as f:
|
| 96 |
+
st.download_button("📥 Télécharger", f, file_name=f"actu_{joueur_input}.mp4")
|
| 97 |
else:
|
| 98 |
+
st.error("Le montage a échoué. Vérifiez vos fichiers.")
|
| 99 |
else:
|
| 100 |
+
st.warning("Veuillez entrer un nom.")
|