Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +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 |
-
#
|
| 13 |
url = "https://groq.com"
|
| 14 |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 15 |
-
|
| 16 |
-
prompt = f"Écris une news de foot ultra-virale et sensationnelle sur {joueur}. Maximum 2 phrases. Sois percutant comme un titre de journal."
|
| 17 |
|
| 18 |
data = {
|
| 19 |
"model": "mixtral-8x7b-32768",
|
|
@@ -23,21 +27,18 @@ def generer_actu_ia(joueur):
|
|
| 23 |
|
| 24 |
try:
|
| 25 |
r = requests.post(url, headers=headers, json=data, timeout=10)
|
| 26 |
-
# Ajout de l'index [0] manquant dans ta version initiale
|
| 27 |
return r.json()['choices'][0]['message']['content']
|
| 28 |
-
except
|
| 29 |
-
return f"{joueur} vient de réaliser une performance incroyable
|
| 30 |
|
| 31 |
-
# --- RECHERCHE D'IMAGE ---
|
| 32 |
def obtenir_image_fond(sujet):
|
| 33 |
if not os.path.exists("images_actu"): os.makedirs("images_actu")
|
| 34 |
-
# Correction de l'URL Pexels
|
| 35 |
url = f"https://pexels.com{sujet}+football&per_page=1"
|
| 36 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 37 |
try:
|
| 38 |
r = requests.get(url, headers=headers, timeout=10)
|
| 39 |
if r.status_code == 200 and r.json().get('photos'):
|
| 40 |
-
# Ajout de l'index [0] pour accéder à la première photo
|
| 41 |
img_url = r.json()['photos'][0]['src']['large2x']
|
| 42 |
nom_image = f"images_actu/fond_{uuid.uuid4().hex[:4]}.jpg"
|
| 43 |
with open(nom_image, 'wb') as f: f.write(requests.get(img_url).content)
|
|
@@ -45,51 +46,46 @@ def obtenir_image_fond(sujet):
|
|
| 45 |
except: pass
|
| 46 |
return "color=black@0.5:s=720x1280"
|
| 47 |
|
| 48 |
-
# --- MOTEUR DE PRODUCTION ---
|
| 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 |
-
|
| 54 |
script = f"Alerte Info ! {news_brute} Abonne-toi à Prisme Foot !"
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
await communicate.save(audio_file)
|
| 60 |
-
except Exception as e:
|
| 61 |
-
st.error(f"Erreur Audio : {e}")
|
| 62 |
-
return None
|
| 63 |
|
| 64 |
image_path = obtenir_image_fond(joueur)
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
# Montage FFmpeg (Ta version initiale)
|
| 68 |
cmd = (
|
| 69 |
-
f'ffmpeg
|
| 70 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
|
|
|
| 71 |
f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150:box=1:boxcolor=black@0.6,'
|
| 72 |
f'drawtext=text=\'{joueur.upper()}\':fontcolor=white:fontsize=60:x=(w-text_w)/2:y=(h-text_h)/2:box=1:boxcolor=red@0.8" '
|
| 73 |
f'-c:v libx264 -preset superfast -tune stillimage -c:a aac -shortest {video_final} -y'
|
| 74 |
)
|
| 75 |
|
| 76 |
subprocess.run(cmd, shell=True)
|
| 77 |
-
|
| 78 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 79 |
-
return video_final
|
| 80 |
|
| 81 |
-
# --- INTERFACE
|
| 82 |
st.set_page_config(page_title="Prisme Foot AI", page_icon="⚽")
|
| 83 |
st.title("⚽ Prisme Foot : Studio IA")
|
| 84 |
|
| 85 |
-
if 'actu' not in st.session_state:
|
| 86 |
-
st.session_state['actu'] = ""
|
| 87 |
-
|
| 88 |
joueur = st.text_input("👤 Nom du Joueur")
|
| 89 |
col1, col2 = st.columns(2)
|
| 90 |
|
| 91 |
with col1:
|
| 92 |
-
if st.button("🤖
|
| 93 |
if joueur:
|
| 94 |
with st.spinner("L'IA réfléchit..."):
|
| 95 |
st.session_state['actu'] = generer_actu_ia(joueur)
|
|
@@ -101,6 +97,7 @@ 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 |
-
if fichier:
|
| 105 |
st.video(fichier)
|
| 106 |
-
with open(fichier, "rb") as f:
|
|
|
|
|
|
| 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",
|
|
|
|
| 27 |
|
| 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:
|
| 40 |
r = requests.get(url, headers=headers, timeout=10)
|
| 41 |
if r.status_code == 200 and r.json().get('photos'):
|
|
|
|
| 42 |
img_url = r.json()['photos'][0]['src']['large2x']
|
| 43 |
nom_image = f"images_actu/fond_{uuid.uuid4().hex[:4]}.jpg"
|
| 44 |
with open(nom_image, 'wb') as f: f.write(requests.get(img_url).content)
|
|
|
|
| 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,'
|
| 72 |
f'drawtext=text=\'{joueur.upper()}\':fontcolor=white:fontsize=60:x=(w-text_w)/2:y=(h-text_h)/2:box=1:boxcolor=red@0.8" '
|
| 73 |
f'-c:v libx264 -preset superfast -tune stillimage -c:a aac -shortest {video_final} -y'
|
| 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 |
|
|
|
|
|
|
|
|
|
|
| 84 |
joueur = st.text_input("👤 Nom du Joueur")
|
| 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)
|
|
|
|
| 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)
|