Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,92 @@
|
|
| 1 |
-
import os,
|
| 2 |
-
import edge_tts
|
| 3 |
import streamlit as st
|
| 4 |
from gtts import gTTS
|
| 5 |
|
| 6 |
# --- CONFIGURATION ---
|
| 7 |
-
PEXELS_API_KEY = os.getenv("
|
| 8 |
-
GROQ_API_KEY = os.getenv("
|
| 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 |
-
|
| 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",
|
| 24 |
"messages": [{"role": "user", "content": prompt}],
|
| 25 |
"temperature": 0.7
|
| 26 |
}
|
| 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}
|
| 33 |
|
| 34 |
def obtenir_image_fond(sujet):
|
| 35 |
-
if not os.path.exists("
|
| 36 |
-
|
| 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 |
-
|
| 44 |
-
with open(
|
| 45 |
-
return
|
| 46 |
except: pass
|
| 47 |
return "color=black@0.5:s=720x1280"
|
| 48 |
|
| 49 |
-
|
| 50 |
-
def creer_video_expert(joueur, news_brute):
|
| 51 |
uid = uuid.uuid4().hex[:6]
|
| 52 |
-
audio_file = f"audio_{uid}.mp3"
|
| 53 |
video_final = f"PRISME_{uid}.mp4"
|
|
|
|
| 54 |
|
| 55 |
-
# 1.
|
| 56 |
-
script = f"Alerte Info ! {
|
| 57 |
-
|
| 58 |
-
# 2. Génération Audio (Simple et robuste)
|
| 59 |
try:
|
| 60 |
-
from gtts import gTTS
|
| 61 |
tts = gTTS(text=script, lang='fr')
|
| 62 |
tts.save(audio_file)
|
| 63 |
-
except
|
| 64 |
-
st.error(f"Erreur Audio : {e}")
|
| 65 |
-
return None
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
input_source = f'-i
|
| 70 |
|
| 71 |
-
#
|
| 72 |
cmd = (
|
| 73 |
f'ffmpeg -loop 1 {input_source} -i {audio_file} '
|
| 74 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
| 75 |
-
f'
|
| 76 |
-
f'drawtext=text=\'
|
| 77 |
-
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" '
|
| 78 |
f'-c:v libx264 -preset superfast -tune stillimage -c:a aac -shortest {video_final} -y'
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# Exécution de la commande
|
| 82 |
subprocess.run(cmd, shell=True)
|
| 83 |
-
|
| 84 |
-
# 5. Nettoyage
|
| 85 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 86 |
return video_final if os.path.exists(video_final) else None
|
| 87 |
|
| 88 |
# --- INTERFACE ---
|
| 89 |
-
st.set_page_config(page_title="Prisme Foot AI"
|
| 90 |
-
st.title("⚽ Prisme Foot
|
|
|
|
|
|
|
| 91 |
|
| 92 |
joueur = st.text_input("👤 Nom du Joueur")
|
| 93 |
-
col1, col2 = st.columns(2)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
if
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 100 |
-
else: st.warning("Mets un nom !")
|
| 101 |
|
| 102 |
actu_finale = st.text_area("📝 News", value=st.session_state['actu'])
|
| 103 |
|
| 104 |
-
if st.button("🎬
|
| 105 |
if joueur and actu_finale:
|
| 106 |
-
with st.spinner("
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
if fichier and os.path.exists(fichier):
|
| 112 |
-
st.video(fichier)
|
| 113 |
-
with open(fichier, "rb") as f:
|
| 114 |
-
st.download_button("📥 Télécharger", f, file_name=fichier)
|
| 115 |
else:
|
| 116 |
-
st.error("
|
|
|
|
| 1 |
+
import os, uuid, subprocess, requests
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from gtts import gTTS
|
| 4 |
|
| 5 |
# --- CONFIGURATION ---
|
| 6 |
+
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 7 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# --- FONCTIONS ---
|
| 10 |
def generer_actu_ia(joueur):
|
| 11 |
+
url = "https://groq.com"
|
|
|
|
| 12 |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 13 |
prompt = f"Écris une news de foot ultra-virale sur {joueur}. Maximum 2 phrases."
|
|
|
|
| 14 |
data = {
|
| 15 |
"model": "mixtral-8x7b-32768",
|
| 16 |
"messages": [{"role": "user", "content": prompt}],
|
| 17 |
"temperature": 0.7
|
| 18 |
}
|
|
|
|
| 19 |
try:
|
| 20 |
r = requests.post(url, headers=headers, json=data, timeout=10)
|
| 21 |
return r.json()['choices'][0]['message']['content']
|
| 22 |
except:
|
| 23 |
+
return f"{joueur} impressionne tout le monde avec une performance incroyable ! ⚽"
|
| 24 |
|
| 25 |
def obtenir_image_fond(sujet):
|
| 26 |
+
if not os.path.exists("temp"): os.makedirs("temp")
|
| 27 |
+
url = f"https://pexels.com{sujet}+football&per_page=1"
|
|
|
|
| 28 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 29 |
try:
|
| 30 |
r = requests.get(url, headers=headers, timeout=10)
|
| 31 |
if r.status_code == 200 and r.json().get('photos'):
|
| 32 |
img_url = r.json()['photos'][0]['src']['large2x']
|
| 33 |
+
path = f"temp/fond_{uuid.uuid4().hex[:4]}.jpg"
|
| 34 |
+
with open(path, 'wb') as f: f.write(requests.get(img_url).content)
|
| 35 |
+
return path
|
| 36 |
except: pass
|
| 37 |
return "color=black@0.5:s=720x1280"
|
| 38 |
|
| 39 |
+
def creer_video_expert(joueur, news):
|
|
|
|
| 40 |
uid = uuid.uuid4().hex[:6]
|
| 41 |
+
audio_file = f"temp/audio_{uid}.mp3"
|
| 42 |
video_final = f"PRISME_{uid}.mp4"
|
| 43 |
+
if not os.path.exists("temp"): os.makedirs("temp")
|
| 44 |
|
| 45 |
+
# 1. Audio
|
| 46 |
+
script = f"Alerte Info ! {news} Abonne-toi à Prisme Foot !"
|
|
|
|
|
|
|
| 47 |
try:
|
|
|
|
| 48 |
tts = gTTS(text=script, lang='fr')
|
| 49 |
tts.save(audio_file)
|
| 50 |
+
except: return None
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# 2. Image
|
| 53 |
+
img = obtenir_image_fond(joueur)
|
| 54 |
+
input_source = f'-i {img}' if not img.startswith("color") else f'-f lavfi -i "{img}"'
|
| 55 |
|
| 56 |
+
# 3. FFmpeg
|
| 57 |
cmd = (
|
| 58 |
f'ffmpeg -loop 1 {input_source} -i {audio_file} '
|
| 59 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
| 60 |
+
f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=40:x=(w-text_w)/2:y=100:box=1:boxcolor=black@0.5,'
|
| 61 |
+
f'drawtext=text=\'{joueur.upper()}\':fontcolor=white:fontsize=50:x=(w-text_w)/2:y=(h-text_h)/2:box=1:boxcolor=red@0.7" '
|
|
|
|
| 62 |
f'-c:v libx264 -preset superfast -tune stillimage -c:a aac -shortest {video_final} -y'
|
| 63 |
)
|
| 64 |
|
|
|
|
| 65 |
subprocess.run(cmd, shell=True)
|
|
|
|
|
|
|
| 66 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 67 |
return video_final if os.path.exists(video_final) else None
|
| 68 |
|
| 69 |
# --- INTERFACE ---
|
| 70 |
+
st.set_page_config(page_title="Prisme Foot AI")
|
| 71 |
+
st.title("⚽ Prisme Foot Studio")
|
| 72 |
+
|
| 73 |
+
if 'actu' not in st.session_state: st.session_state['actu'] = ""
|
| 74 |
|
| 75 |
joueur = st.text_input("👤 Nom du Joueur")
|
|
|
|
| 76 |
|
| 77 |
+
if st.button("🤖 GÉNÉRER TEXTE"):
|
| 78 |
+
if joueur:
|
| 79 |
+
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 80 |
+
else: st.error("Entrez un nom !")
|
|
|
|
|
|
|
| 81 |
|
| 82 |
actu_finale = st.text_area("📝 News", value=st.session_state['actu'])
|
| 83 |
|
| 84 |
+
if st.button("🎬 CRÉER VIDÉO"):
|
| 85 |
if joueur and actu_finale:
|
| 86 |
+
with st.spinner("Montage en cours..."):
|
| 87 |
+
video = creer_video_expert(joueur, actu_finale)
|
| 88 |
+
if video:
|
| 89 |
+
st.video(video)
|
| 90 |
+
st.download_button("📥 Télécharger", open(video, "rb"), file_name=video)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
else:
|
| 92 |
+
st.error("Erreur technique. Vérifiez vos clés API dans les Settings.")
|