Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,127 +1,114 @@
|
|
| 1 |
import os, uuid, subprocess, requests, shlex
|
| 2 |
import streamlit as st
|
| 3 |
from gtts import gTTS
|
|
|
|
| 4 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
| 5 |
|
| 6 |
-
# --- CONFIGURATION
|
|
|
|
| 7 |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 8 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
-
GROQ_URL = "https://groq.com"
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10))
|
| 13 |
def generer_actu_ia(joueur):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
headers = {
|
| 17 |
-
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 18 |
-
"Content-Type": "application/json"
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
payload = {
|
| 22 |
-
"model": "mixtral-8x7b-32768",
|
| 23 |
-
"messages": [{"role": "user", "content": f"News foot courte sur {joueur}"}]
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
# 2. L'APPEL SÉCURISÉ
|
| 27 |
-
r = requests.post(url, headers=headers, json=payload, timeout=10)
|
| 28 |
-
|
| 29 |
-
if r.status_code == 200:
|
| 30 |
-
# 3. L'INDEXATION CRUCIALE [0]
|
| 31 |
-
return r.json()['choices'][0]['message']['content']
|
| 32 |
-
else:
|
| 33 |
-
return f"Erreur {r.status_code}: {r.text}"
|
| 34 |
-
|
| 35 |
-
# Structure de réponse standard OpenAI : choices est une LISTE
|
| 36 |
-
data = response.json()
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def obtenir_image_fond(sujet):
|
| 42 |
-
"""Récupère une image
|
|
|
|
| 43 |
url = f"https://pexels.com{sujet}+football&per_page=1&orientation=portrait"
|
| 44 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 45 |
try:
|
| 46 |
r = requests.get(url, headers=headers, timeout=10)
|
| 47 |
if r.status_code == 200 and r.json().get('photos'):
|
| 48 |
img_url = r.json()['photos'][0]['src']['large2x']
|
| 49 |
-
path = f"img_{uuid.uuid4().hex[:
|
| 50 |
with open(path, 'wb') as f: f.write(requests.get(img_url).content)
|
| 51 |
return path
|
| 52 |
-
except
|
| 53 |
-
st.error(f"Erreur Pexels: {e}")
|
| 54 |
-
return None
|
| 55 |
-
|
| 56 |
-
# --- MOTEUR DE RENDU VIDÉO ---
|
| 57 |
|
| 58 |
-
def
|
| 59 |
-
"""
|
| 60 |
uid = uuid.uuid4().hex[:6]
|
| 61 |
audio_file = f"audio_{uid}.mp3"
|
| 62 |
video_final = f"PRISME_{uid}.mp4"
|
| 63 |
|
| 64 |
-
# Nettoyage
|
| 65 |
news_clean = news.replace("'", " ").replace('"', " ").replace(":", " ")
|
| 66 |
joueur_clean = joueur.upper().replace("'", " ")
|
| 67 |
|
| 68 |
try:
|
| 69 |
-
# 1.
|
| 70 |
tts = gTTS(text=f"Alerte Info ! {news_clean}", lang='fr')
|
| 71 |
tts.save(audio_file)
|
| 72 |
|
| 73 |
-
# 2. Image
|
| 74 |
img = obtenir_image_fond(joueur)
|
| 75 |
-
input_source = ["-loop", "1", "-i", img] if img else ["-f", "lavfi", "-i", "color=c=
|
| 76 |
|
| 77 |
-
# 3. Commande FFmpeg
|
| 78 |
-
|
| 79 |
-
vf_filters = (
|
| 80 |
f"scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,"
|
| 81 |
f"drawtext=text='PRISME FOOT':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150:box=1:boxcolor=black@0.6,"
|
| 82 |
f"drawtext=text='{joueur_clean}':fontcolor=white:fontsize=60:x=(w-text_w)/2:y=(h-text_h)/2:box=1:boxcolor=red@0.8,"
|
| 83 |
-
f"drawtext=text='{news_clean[:
|
| 84 |
)
|
| 85 |
|
| 86 |
cmd = [
|
| 87 |
"ffmpeg", "-y"
|
| 88 |
] + input_source + [
|
| 89 |
"-i", audio_file,
|
| 90 |
-
"-vf",
|
| 91 |
"-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest",
|
| 92 |
video_final
|
| 93 |
]
|
| 94 |
|
| 95 |
subprocess.run(cmd, check=True)
|
| 96 |
-
|
| 97 |
-
# Nettoyage
|
| 98 |
-
if img: os.remove(img)
|
| 99 |
-
os.remove(audio_file)
|
| 100 |
|
| 101 |
return video_final
|
| 102 |
except Exception as e:
|
| 103 |
-
st.error(f"Erreur rendu: {e}")
|
| 104 |
return None
|
| 105 |
|
| 106 |
# --- INTERFACE ---
|
| 107 |
-
st.set_page_config(page_title="Prisme Foot Studio
|
| 108 |
-
st.title("
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
joueur = st.text_input("👤 Nom du Joueur", placeholder="Ex: Kylian Mbappé")
|
| 111 |
|
| 112 |
-
if st.button("🤖 1. GÉNÉRER
|
| 113 |
if joueur:
|
| 114 |
-
with st.spinner("L'IA
|
| 115 |
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 116 |
-
else: st.warning("
|
| 117 |
|
| 118 |
-
actu_finale = st.text_area("📝 Script (
|
| 119 |
|
| 120 |
-
if st.button("🎬 2.
|
| 121 |
if joueur and actu_finale:
|
| 122 |
-
with st.spinner("
|
| 123 |
-
video =
|
| 124 |
if video:
|
| 125 |
st.video(video)
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
| 1 |
import os, uuid, subprocess, requests, shlex
|
| 2 |
import streamlit as st
|
| 3 |
from gtts import gTTS
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
| 6 |
|
| 7 |
+
# --- CONFIGURATION ---
|
| 8 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 9 |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Configuration Google AI
|
| 12 |
+
if GEMINI_API_KEY:
|
| 13 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
+
|
| 15 |
+
# --- SERVICES ---
|
| 16 |
+
|
| 17 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10))
|
| 18 |
def generer_actu_ia(joueur):
|
| 19 |
+
"""Génération via Gemini (plus stable que Groq sur les endpoints cloud)."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 22 |
+
prompt = f"Écris une news foot très courte, virale et fun sur {joueur}. Maximum 15 mots."
|
| 23 |
+
response = model.generate_content(prompt)
|
| 24 |
+
return response.text.strip()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"{joueur} est au cœur de toutes les discussions aujourd'hui ! ⚽"
|
| 27 |
+
|
| 28 |
def obtenir_image_fond(sujet):
|
| 29 |
+
"""Récupère une image via l'API Pexels."""
|
| 30 |
+
if not PEXELS_API_KEY: return None
|
| 31 |
url = f"https://pexels.com{sujet}+football&per_page=1&orientation=portrait"
|
| 32 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 33 |
try:
|
| 34 |
r = requests.get(url, headers=headers, timeout=10)
|
| 35 |
if r.status_code == 200 and r.json().get('photos'):
|
| 36 |
img_url = r.json()['photos'][0]['src']['large2x']
|
| 37 |
+
path = f"img_{uuid.uuid4().hex[:4]}.jpg"
|
| 38 |
with open(path, 'wb') as f: f.write(requests.get(img_url).content)
|
| 39 |
return path
|
| 40 |
+
except: return None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
def creer_video_pro(joueur, news):
|
| 43 |
+
"""Moteur de rendu vidéo sécurisé."""
|
| 44 |
uid = uuid.uuid4().hex[:6]
|
| 45 |
audio_file = f"audio_{uid}.mp3"
|
| 46 |
video_final = f"PRISME_{uid}.mp4"
|
| 47 |
|
| 48 |
+
# Nettoyage des textes
|
| 49 |
news_clean = news.replace("'", " ").replace('"', " ").replace(":", " ")
|
| 50 |
joueur_clean = joueur.upper().replace("'", " ")
|
| 51 |
|
| 52 |
try:
|
| 53 |
+
# 1. Audio
|
| 54 |
tts = gTTS(text=f"Alerte Info ! {news_clean}", lang='fr')
|
| 55 |
tts.save(audio_file)
|
| 56 |
|
| 57 |
+
# 2. Image
|
| 58 |
img = obtenir_image_fond(joueur)
|
| 59 |
+
input_source = ["-loop", "1", "-i", img] if img else ["-f", "lavfi", "-i", "color=c=101010:s=720x1280"]
|
| 60 |
|
| 61 |
+
# 3. Commande FFmpeg en liste (Protection contre injection)
|
| 62 |
+
vf = (
|
|
|
|
| 63 |
f"scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,"
|
| 64 |
f"drawtext=text='PRISME FOOT':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150:box=1:boxcolor=black@0.6,"
|
| 65 |
f"drawtext=text='{joueur_clean}':fontcolor=white:fontsize=60:x=(w-text_w)/2:y=(h-text_h)/2:box=1:boxcolor=red@0.8,"
|
| 66 |
+
f"drawtext=text='{news_clean[:45]}...':fontcolor=white:fontsize=30:x=(w-text_w)/2:y=h-250:box=1:boxcolor=black@0.7"
|
| 67 |
)
|
| 68 |
|
| 69 |
cmd = [
|
| 70 |
"ffmpeg", "-y"
|
| 71 |
] + input_source + [
|
| 72 |
"-i", audio_file,
|
| 73 |
+
"-vf", vf,
|
| 74 |
"-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest",
|
| 75 |
video_final
|
| 76 |
]
|
| 77 |
|
| 78 |
subprocess.run(cmd, check=True)
|
| 79 |
+
|
| 80 |
+
# Nettoyage temporaire
|
| 81 |
+
if img and os.path.exists(img): os.remove(img)
|
| 82 |
+
if os.path.exists(audio_file): os.remove(audio_file)
|
| 83 |
|
| 84 |
return video_final
|
| 85 |
except Exception as e:
|
| 86 |
+
st.error(f"Erreur de rendu : {e}")
|
| 87 |
return None
|
| 88 |
|
| 89 |
# --- INTERFACE ---
|
| 90 |
+
st.set_page_config(page_title="Prisme Foot Studio", page_icon="⚽")
|
| 91 |
+
st.title("⚽ Prisme Foot Studio - Pro")
|
| 92 |
+
|
| 93 |
+
if 'actu' not in st.session_state:
|
| 94 |
+
st.session_state['actu'] = ""
|
| 95 |
|
| 96 |
joueur = st.text_input("👤 Nom du Joueur", placeholder="Ex: Kylian Mbappé")
|
| 97 |
|
| 98 |
+
if st.button("🤖 1. GÉNÉRER TEXTE IA"):
|
| 99 |
if joueur:
|
| 100 |
+
with st.spinner("L'IA prépare le script..."):
|
| 101 |
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 102 |
+
else: st.warning("Veuillez entrer un nom.")
|
| 103 |
|
| 104 |
+
actu_finale = st.text_area("📝 Script (Modifiable)", value=st.session_state['actu'], height=100)
|
| 105 |
|
| 106 |
+
if st.button("🎬 2. CRÉER LA VIDÉO"):
|
| 107 |
if joueur and actu_finale:
|
| 108 |
+
with st.spinner("Génération de la vidéo en cours..."):
|
| 109 |
+
video = creer_video_pro(joueur, actu_finale)
|
| 110 |
if video:
|
| 111 |
st.video(video)
|
| 112 |
+
with open(video, "rb") as f:
|
| 113 |
+
st.download_button("📥 Télécharger la vidéo", f, file_name=video)
|
| 114 |
+
else: st.error("Remplissez tous les champs !")
|