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