Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,97 +9,100 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
| 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":
|
| 15 |
-
"temperature": 0.
|
| 16 |
}
|
| 17 |
try:
|
| 18 |
-
r = requests.post(url, headers=headers, json=data, timeout=
|
| 19 |
-
# Correction de l'indexation JSON ici
|
| 20 |
return r.json()['choices'][0]['message']['content']
|
| 21 |
except:
|
| 22 |
-
return f"{joueur}
|
| 23 |
|
| 24 |
def obtenir_image_fond(sujet):
|
| 25 |
-
url = f"https://pexels.com{sujet}+
|
| 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:
|
| 33 |
-
|
|
|
|
|
|
|
| 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 |
-
#
|
| 43 |
news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
|
| 44 |
joueur_clean = joueur.replace("'", " ").replace('"', " ")
|
| 45 |
|
| 46 |
-
# 1.
|
| 47 |
try:
|
| 48 |
-
tts = gTTS(text=
|
| 49 |
tts.save(audio_file)
|
| 50 |
except: return None
|
| 51 |
|
| 52 |
-
# 2.
|
| 53 |
img = obtenir_image_fond(joueur)
|
| 54 |
if img and os.path.exists(img):
|
| 55 |
-
|
| 56 |
else:
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
# 3.
|
| 60 |
cmd = (
|
| 61 |
-
f'ffmpeg {
|
| 62 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
| 63 |
-
f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=
|
| 64 |
-
f'drawtext=text=\'{joueur_clean.upper()}\':fontcolor=white:fontsize=
|
| 65 |
-
f'-c:v libx264 -preset superfast -c:a aac -shortest {video_final}
|
| 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
|
| 77 |
-
st.set_page_config(page_title="Prisme Foot Studio",
|
| 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
|
| 84 |
|
| 85 |
-
if st.button("🤖 GÉNÉRER
|
| 86 |
if joueur:
|
| 87 |
-
with st.spinner("L'IA
|
| 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("📝
|
| 93 |
|
| 94 |
if st.button("🎬 CRÉER LA VIDÉO"):
|
| 95 |
if joueur and actu_finale:
|
| 96 |
-
with st.spinner("Montage
|
| 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
|
| 102 |
else:
|
| 103 |
-
st.error("Erreur
|
| 104 |
-
else:
|
| 105 |
-
st.error("Remplis tous les champs !")
|
|
|
|
| 9 |
def generer_actu_ia(joueur):
|
| 10 |
url = "https://groq.com"
|
| 11 |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 12 |
+
|
| 13 |
+
# Prompt plus long pour un texte riche
|
| 14 |
+
prompt = f"""Rédige un script de 45 secondes pour une vidéo TikTok sur {joueur}.
|
| 15 |
+
Parle de ses performances récentes, d'une rumeur de transfert ou d'un record.
|
| 16 |
+
Utilise un ton sensationnel et captivant. Fais environ 5 phrases longues."""
|
| 17 |
+
|
| 18 |
data = {
|
| 19 |
"model": "mixtral-8x7b-32768",
|
| 20 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 21 |
+
"temperature": 0.8 # Augmenté pour plus de variété
|
| 22 |
}
|
| 23 |
try:
|
| 24 |
+
r = requests.post(url, headers=headers, json=data, timeout=15)
|
|
|
|
| 25 |
return r.json()['choices'][0]['message']['content']
|
| 26 |
except:
|
| 27 |
+
return f"Incroyable mais vrai ! {joueur} est au centre de toutes les discussions aujourd'hui après une performance qui a laissé les observateurs sans voix. Le monde du football n'a jamais rien vu de tel !"
|
| 28 |
|
| 29 |
def obtenir_image_fond(sujet):
|
| 30 |
+
url = f"https://pexels.com{sujet}+soccer&per_page=1"
|
| 31 |
+
headers = {"Authorization": PEXELS_API_KEY, "User-Agent": "Mozilla/5.0"}
|
| 32 |
try:
|
| 33 |
r = requests.get(url, headers=headers, timeout=10)
|
| 34 |
if r.status_code == 200 and r.json().get('photos'):
|
| 35 |
img_url = r.json()['photos'][0]['src']['large2x']
|
| 36 |
+
path = os.path.abspath(f"fond_{uuid.uuid4().hex[:4]}.jpg")
|
| 37 |
+
with open(path, 'wb') as f:
|
| 38 |
+
f.write(requests.get(img_url).content)
|
| 39 |
+
if os.path.getsize(path) > 1000: # Vérifie que l'image n'est pas vide
|
| 40 |
+
return path
|
| 41 |
except: pass
|
| 42 |
return None
|
| 43 |
|
| 44 |
def creer_video_expert(joueur, news):
|
| 45 |
uid = uuid.uuid4().hex[:6]
|
| 46 |
+
audio_file = os.path.abspath(f"audio_{uid}.mp3")
|
| 47 |
+
video_final = os.path.abspath(f"PRISME_{uid}.mp4")
|
| 48 |
|
| 49 |
+
# Nettoyage pour FFmpeg
|
| 50 |
news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
|
| 51 |
joueur_clean = joueur.replace("'", " ").replace('"', " ")
|
| 52 |
|
| 53 |
+
# 1. Audio
|
| 54 |
try:
|
| 55 |
+
tts = gTTS(text=news_clean, lang='fr')
|
| 56 |
tts.save(audio_file)
|
| 57 |
except: return None
|
| 58 |
|
| 59 |
+
# 2. Image (Pexels ou secours)
|
| 60 |
img = obtenir_image_fond(joueur)
|
| 61 |
if img and os.path.exists(img):
|
| 62 |
+
input_src = f'-loop 1 -i "{img}"'
|
| 63 |
else:
|
| 64 |
+
# Image de secours football si Pexels échoue
|
| 65 |
+
input_src = '-f lavfi -i color=c=0x1a1a1a:s=720x1280'
|
| 66 |
|
| 67 |
+
# 3. Montage FFmpeg (Format TikTok + Textes)
|
| 68 |
cmd = (
|
| 69 |
+
f'ffmpeg -y {input_src} -i "{audio_file}" '
|
| 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_clean.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 -pix_fmt yuv420p -c:a aac -shortest "{video_final}"'
|
| 74 |
)
|
| 75 |
|
| 76 |
subprocess.run(cmd, shell=True)
|
| 77 |
|
|
|
|
| 78 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 79 |
if img and os.path.exists(img): os.remove(img)
|
| 80 |
|
| 81 |
return video_final if os.path.exists(video_final) else None
|
| 82 |
|
| 83 |
+
# --- INTERFACE ---
|
| 84 |
+
st.set_page_config(page_title="Prisme Foot Studio", layout="centered")
|
| 85 |
+
st.title("⚽ Prisme Foot Studio Pro")
|
| 86 |
|
| 87 |
+
if 'actu' not in st.session_state: st.session_state['actu'] = ""
|
|
|
|
| 88 |
|
| 89 |
+
joueur = st.text_input("👤 Nom du joueur (ex: Kylian Mbappé)")
|
| 90 |
|
| 91 |
+
if st.button("🤖 GÉNÉRER UNE NEWS LONGUE"):
|
| 92 |
if joueur:
|
| 93 |
+
with st.spinner("L'IA analyse les données..."):
|
| 94 |
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 95 |
+
else: st.error("Mets un nom !")
|
|
|
|
| 96 |
|
| 97 |
+
actu_finale = st.text_area("📝 Script de la vidéo", value=st.session_state['actu'], height=150)
|
| 98 |
|
| 99 |
if st.button("🎬 CRÉER LA VIDÉO"):
|
| 100 |
if joueur and actu_finale:
|
| 101 |
+
with st.spinner("Montage de la vidéo..."):
|
| 102 |
video = creer_video_expert(joueur, actu_finale)
|
| 103 |
if video and os.path.exists(video):
|
| 104 |
st.video(video)
|
| 105 |
with open(video, "rb") as f:
|
| 106 |
+
st.download_button("📥 Télécharger en HD", f, file_name=f"{joueur}_actu.mp4")
|
| 107 |
else:
|
| 108 |
+
st.error("Erreur FFmpeg. Vérifiez vos clés API.")
|
|
|
|
|
|