Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,23 @@
|
|
| 1 |
import os, random, uuid, json, subprocess, asyncio, requests
|
| 2 |
import edge_tts
|
| 3 |
import streamlit as st
|
|
|
|
| 4 |
|
| 5 |
-
# --- CONFIGURATION
|
| 6 |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 7 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 8 |
VOIX_FR = "fr-FR-RemyNeural"
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def generer_actu_ia(joueur):
|
|
|
|
| 12 |
url = "https://groq.com"
|
| 13 |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 14 |
-
|
| 15 |
-
prompt = f"Écris une news de foot ultra-virale et sensationnelle sur {joueur}. Maximum 2 phrases. Sois percutant comme un titre de journal."
|
| 16 |
|
| 17 |
data = {
|
| 18 |
"model": "mixtral-8x7b-32768",
|
|
@@ -23,12 +28,12 @@ def generer_actu_ia(joueur):
|
|
| 23 |
try:
|
| 24 |
r = requests.post(url, headers=headers, json=data, timeout=10)
|
| 25 |
return r.json()['choices'][0]['message']['content']
|
| 26 |
-
except
|
| 27 |
-
return f"
|
| 28 |
|
| 29 |
-
# --- RECHERCHE D'IMAGE ---
|
| 30 |
def obtenir_image_fond(sujet):
|
| 31 |
if not os.path.exists("images_actu"): os.makedirs("images_actu")
|
|
|
|
| 32 |
url = f"https://pexels.com{sujet}+football&per_page=1"
|
| 33 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 34 |
try:
|
|
@@ -41,32 +46,26 @@ def obtenir_image_fond(sujet):
|
|
| 41 |
except: pass
|
| 42 |
return "color=black@0.5:s=720x1280"
|
| 43 |
|
| 44 |
-
# --- MOTEUR DE PRODUCTION ---
|
| 45 |
-
from gtts import gTTS # Ajoute cet import en haut du fichier
|
| 46 |
-
|
| 47 |
async def creer_video_expert(joueur, news_brute):
|
| 48 |
uid = uuid.uuid4().hex[:6]
|
| 49 |
audio_file = f"audio_{uid}.mp3"
|
| 50 |
video_final = f"PRISME_{uid}.mp4"
|
| 51 |
-
|
| 52 |
-
# 1. Texte de sécurité
|
| 53 |
script = f"Alerte Info ! {news_brute} Abonne-toi à Prisme Foot !"
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
tts.save(audio_file)
|
| 59 |
-
except Exception as e:
|
| 60 |
-
st.error(f"Erreur Audio : {e}")
|
| 61 |
-
return None
|
| 62 |
|
| 63 |
-
# 3. Image de fond via Pexels
|
| 64 |
image_path = obtenir_image_fond(joueur)
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
# 4. Montage FFmpeg (Version Robuste)
|
| 68 |
cmd = (
|
| 69 |
-
f'ffmpeg
|
| 70 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
| 71 |
f'zoompan=z=\'min(zoom+0.0015,1.5)\':d=125:s=720x1280,'
|
| 72 |
f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150:box=1:boxcolor=black@0.6,'
|
|
@@ -75,11 +74,10 @@ async def creer_video_expert(joueur, news_brute):
|
|
| 75 |
)
|
| 76 |
|
| 77 |
subprocess.run(cmd, shell=True)
|
| 78 |
-
|
| 79 |
-
# 5. Nettoyage
|
| 80 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 81 |
-
return video_final
|
| 82 |
-
|
|
|
|
| 83 |
st.set_page_config(page_title="Prisme Foot AI", page_icon="⚽")
|
| 84 |
st.title("⚽ Prisme Foot : Studio IA")
|
| 85 |
|
|
@@ -87,19 +85,19 @@ joueur = st.text_input("👤 Nom du Joueur")
|
|
| 87 |
col1, col2 = st.columns(2)
|
| 88 |
|
| 89 |
with col1:
|
| 90 |
-
if st.button("🤖
|
| 91 |
if joueur:
|
| 92 |
with st.spinner("L'IA réfléchit..."):
|
| 93 |
-
actu = generer_actu_ia(joueur)
|
| 94 |
-
st.session_state['actu'] = actu
|
| 95 |
else: st.warning("Mets un nom !")
|
| 96 |
|
| 97 |
-
|
| 98 |
-
actu_finale = st.text_area("📝 News (Modifier ici si besoin)", value=st.session_state.get('actu', ''))
|
| 99 |
|
| 100 |
if st.button("🎬 GÉNÉRER LA VIDÉO"):
|
| 101 |
if joueur and actu_finale:
|
| 102 |
with st.spinner("🛠️ Montage en cours..."):
|
| 103 |
fichier = asyncio.run(creer_video_expert(joueur, actu_finale))
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os, random, uuid, json, subprocess, asyncio, requests
|
| 2 |
import edge_tts
|
| 3 |
import streamlit as st
|
| 4 |
+
from gtts import gTTS
|
| 5 |
|
| 6 |
+
# --- CONFIGURATION ---
|
| 7 |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 8 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
VOIX_FR = "fr-FR-RemyNeural"
|
| 10 |
|
| 11 |
+
# Initialisation du session_state pour éviter les erreurs au premier chargement
|
| 12 |
+
if 'actu' not in st.session_state:
|
| 13 |
+
st.session_state['actu'] = ""
|
| 14 |
+
|
| 15 |
+
# --- FONCTIONS ---
|
| 16 |
def generer_actu_ia(joueur):
|
| 17 |
+
# Note: L'URL de l'API Groq est https://groq.com
|
| 18 |
url = "https://groq.com"
|
| 19 |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 20 |
+
prompt = f"Écris une news de foot ultra-virale sur {joueur}. Maximum 2 phrases."
|
|
|
|
| 21 |
|
| 22 |
data = {
|
| 23 |
"model": "mixtral-8x7b-32768",
|
|
|
|
| 28 |
try:
|
| 29 |
r = requests.post(url, headers=headers, json=data, timeout=10)
|
| 30 |
return r.json()['choices'][0]['message']['content']
|
| 31 |
+
except:
|
| 32 |
+
return f"{joueur} vient de réaliser une performance incroyable ! ⚽"
|
| 33 |
|
|
|
|
| 34 |
def obtenir_image_fond(sujet):
|
| 35 |
if not os.path.exists("images_actu"): os.makedirs("images_actu")
|
| 36 |
+
# Correction de l'URL Pexels
|
| 37 |
url = f"https://pexels.com{sujet}+football&per_page=1"
|
| 38 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 39 |
try:
|
|
|
|
| 46 |
except: pass
|
| 47 |
return "color=black@0.5:s=720x1280"
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
async def creer_video_expert(joueur, news_brute):
|
| 50 |
uid = uuid.uuid4().hex[:6]
|
| 51 |
audio_file = f"audio_{uid}.mp3"
|
| 52 |
video_final = f"PRISME_{uid}.mp4"
|
|
|
|
|
|
|
| 53 |
script = f"Alerte Info ! {news_brute} Abonne-toi à Prisme Foot !"
|
| 54 |
|
| 55 |
+
# Utilisation de edge_tts (plus qualitatif que gTTS)
|
| 56 |
+
communicate = edge_tts.Communicate(script, VOIX_FR)
|
| 57 |
+
await communicate.save(audio_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
|
|
|
| 59 |
image_path = obtenir_image_fond(joueur)
|
| 60 |
+
|
| 61 |
+
# Construction de la commande FFmpeg
|
| 62 |
+
if image_path.startswith("color"):
|
| 63 |
+
input_cmd = f"-f lavfi -i \"{image_path}\""
|
| 64 |
+
else:
|
| 65 |
+
input_cmd = f"-loop 1 -i \"{image_path}\""
|
| 66 |
|
|
|
|
| 67 |
cmd = (
|
| 68 |
+
f'ffmpeg {input_cmd} -i {audio_file} '
|
| 69 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
| 70 |
f'zoompan=z=\'min(zoom+0.0015,1.5)\':d=125:s=720x1280,'
|
| 71 |
f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150:box=1:boxcolor=black@0.6,'
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
subprocess.run(cmd, shell=True)
|
|
|
|
|
|
|
| 77 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 78 |
+
return video_final
|
| 79 |
+
|
| 80 |
+
# --- INTERFACE ---
|
| 81 |
st.set_page_config(page_title="Prisme Foot AI", page_icon="⚽")
|
| 82 |
st.title("⚽ Prisme Foot : Studio IA")
|
| 83 |
|
|
|
|
| 85 |
col1, col2 = st.columns(2)
|
| 86 |
|
| 87 |
with col1:
|
| 88 |
+
if st.button("🤖 IA : ÉCRIRE"):
|
| 89 |
if joueur:
|
| 90 |
with st.spinner("L'IA réfléchit..."):
|
| 91 |
+
st.session_state['actu'] = generer_actu_ia(joueur)
|
|
|
|
| 92 |
else: st.warning("Mets un nom !")
|
| 93 |
|
| 94 |
+
actu_finale = st.text_area("📝 News", value=st.session_state['actu'])
|
|
|
|
| 95 |
|
| 96 |
if st.button("🎬 GÉNÉRER LA VIDÉO"):
|
| 97 |
if joueur and actu_finale:
|
| 98 |
with st.spinner("🛠️ Montage en cours..."):
|
| 99 |
fichier = asyncio.run(creer_video_expert(joueur, actu_finale))
|
| 100 |
+
if fichier and os.path.exists(fichier):
|
| 101 |
+
st.video(fichier)
|
| 102 |
+
with open(fichier, "rb") as f:
|
| 103 |
+
st.download_button("📥 Télécharger", f, file_name=fichier)
|