Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,35 +7,35 @@ 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 |
-
# L'URL
|
| 11 |
-
url = "https://groq.com"
|
| 12 |
-
headers = {
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
}
|
| 16 |
-
prompt = f"Rédige un script TikTok de 5 phrases percutantes sur l'actualité de {joueur}. Sois sensationnel !"
|
| 17 |
|
| 18 |
data = {
|
| 19 |
"model": "mixtral-8x7b-32768",
|
| 20 |
"messages": [{"role": "user", "content": prompt}],
|
| 21 |
-
"temperature": 0.
|
| 22 |
}
|
| 23 |
try:
|
| 24 |
r = requests.post(url, headers=headers, json=data, timeout=15)
|
| 25 |
-
if r.status_code
|
| 26 |
-
return
|
| 27 |
-
return r.
|
| 28 |
except Exception as e:
|
| 29 |
-
return f"
|
| 30 |
|
| 31 |
def obtenir_image_fond(sujet):
|
|
|
|
| 32 |
url = f"https://pexels.com{sujet}+soccer&per_page=1"
|
| 33 |
headers = {"Authorization": PEXELS_API_KEY, "User-Agent": "Mozilla/5.0"}
|
| 34 |
try:
|
| 35 |
r = requests.get(url, headers=headers, timeout=10)
|
| 36 |
data = r.json()
|
| 37 |
if r.status_code == 200 and data.get('photos'):
|
| 38 |
-
#
|
| 39 |
img_url = data['photos'][0]['src']['large2x']
|
| 40 |
path = os.path.abspath(f"fond_{uuid.uuid4().hex[:4]}.jpg")
|
| 41 |
with open(path, 'wb') as f:
|
|
@@ -49,7 +49,7 @@ def creer_video_expert(joueur, news):
|
|
| 49 |
audio_file = os.path.abspath(f"audio_{uid}.mp3")
|
| 50 |
video_final = os.path.abspath(f"PRISME_{uid}.mp4")
|
| 51 |
|
| 52 |
-
# Nettoyage pour FFmpeg (
|
| 53 |
news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
|
| 54 |
joueur_clean = joueur.replace("'", " ").replace('"', " ")
|
| 55 |
|
|
@@ -59,7 +59,7 @@ def creer_video_expert(joueur, news):
|
|
| 59 |
tts.save(audio_file)
|
| 60 |
except: return None
|
| 61 |
|
| 62 |
-
# 2. Image
|
| 63 |
img = obtenir_image_fond(joueur)
|
| 64 |
if img and os.path.exists(img):
|
| 65 |
input_src = f'-loop 1 -i "{img}"'
|
|
@@ -67,7 +67,7 @@ def creer_video_expert(joueur, news):
|
|
| 67 |
# Fond vert de secours si Pexels échoue
|
| 68 |
input_src = '-f lavfi -i color=c=0x228B22:s=720x1280'
|
| 69 |
|
| 70 |
-
# 3.
|
| 71 |
cmd = (
|
| 72 |
f'ffmpeg -y {input_src} -i "{audio_file}" '
|
| 73 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
|
@@ -78,31 +78,34 @@ def creer_video_expert(joueur, news):
|
|
| 78 |
|
| 79 |
subprocess.run(cmd, shell=True)
|
| 80 |
|
|
|
|
| 81 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 82 |
if img and os.path.exists(img): os.remove(img)
|
| 83 |
|
| 84 |
return video_final if os.path.exists(video_final) else None
|
| 85 |
|
| 86 |
-
# --- INTERFACE
|
| 87 |
-
st.set_page_config(page_title="Prisme Foot Studio
|
| 88 |
st.title("⚽ Prisme Foot Studio Pro")
|
| 89 |
|
| 90 |
if 'actu' not in st.session_state: st.session_state['actu'] = ""
|
| 91 |
|
| 92 |
-
joueur = st.text_input("👤 Nom du
|
| 93 |
|
| 94 |
-
if st.button("🤖 GÉNÉRER
|
| 95 |
if joueur:
|
| 96 |
-
with st.spinner("L'IA
|
| 97 |
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 98 |
-
else: st.
|
| 99 |
|
| 100 |
-
actu_finale = st.text_area("📝
|
| 101 |
|
| 102 |
if st.button("🎬 CRÉER LA VIDÉO"):
|
| 103 |
if joueur and actu_finale:
|
| 104 |
-
with st.spinner("Montage
|
| 105 |
video = creer_video_expert(joueur, actu_finale)
|
| 106 |
-
if video:
|
| 107 |
st.video(video)
|
| 108 |
-
st.download_button("📥 Télécharger", open(video, "rb"), file_name=f"{joueur}.mp4")
|
|
|
|
|
|
|
|
|
| 7 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 8 |
|
| 9 |
def generer_actu_ia(joueur):
|
| 10 |
+
# L'URL doit être exactement celle-ci pour éviter l'erreur 405
|
| 11 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 12 |
+
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 13 |
+
|
| 14 |
+
# Prompt optimisé pour la longueur et la qualité
|
| 15 |
+
prompt = f"Écris un script passionnant de 5 phrases pour une vidéo sur {joueur}. Parle de ses exploits récents et termine par une question pour l'audience."
|
|
|
|
| 16 |
|
| 17 |
data = {
|
| 18 |
"model": "mixtral-8x7b-32768",
|
| 19 |
"messages": [{"role": "user", "content": prompt}],
|
| 20 |
+
"temperature": 0.8
|
| 21 |
}
|
| 22 |
try:
|
| 23 |
r = requests.post(url, headers=headers, json=data, timeout=15)
|
| 24 |
+
if r.status_code == 200:
|
| 25 |
+
return r.json()['choices'][0]['message']['content']
|
| 26 |
+
return f"Erreur API Groq (Code {r.status_code}). Vérifie ta clé dans les Secrets."
|
| 27 |
except Exception as e:
|
| 28 |
+
return f"Connexion impossible à l'IA : {e}"
|
| 29 |
|
| 30 |
def obtenir_image_fond(sujet):
|
| 31 |
+
# On cherche 'soccer' pour garantir des images de foot
|
| 32 |
url = f"https://pexels.com{sujet}+soccer&per_page=1"
|
| 33 |
headers = {"Authorization": PEXELS_API_KEY, "User-Agent": "Mozilla/5.0"}
|
| 34 |
try:
|
| 35 |
r = requests.get(url, headers=headers, timeout=10)
|
| 36 |
data = r.json()
|
| 37 |
if r.status_code == 200 and data.get('photos'):
|
| 38 |
+
# CORRECTION : On accède bien au premier élément de la liste
|
| 39 |
img_url = data['photos'][0]['src']['large2x']
|
| 40 |
path = os.path.abspath(f"fond_{uuid.uuid4().hex[:4]}.jpg")
|
| 41 |
with open(path, 'wb') as f:
|
|
|
|
| 49 |
audio_file = os.path.abspath(f"audio_{uid}.mp3")
|
| 50 |
video_final = os.path.abspath(f"PRISME_{uid}.mp4")
|
| 51 |
|
| 52 |
+
# Nettoyage pour FFmpeg (crucial pour éviter les erreurs de texte)
|
| 53 |
news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
|
| 54 |
joueur_clean = joueur.replace("'", " ").replace('"', " ")
|
| 55 |
|
|
|
|
| 59 |
tts.save(audio_file)
|
| 60 |
except: return None
|
| 61 |
|
| 62 |
+
# 2. Image (Pexels ou secours)
|
| 63 |
img = obtenir_image_fond(joueur)
|
| 64 |
if img and os.path.exists(img):
|
| 65 |
input_src = f'-loop 1 -i "{img}"'
|
|
|
|
| 67 |
# Fond vert de secours si Pexels échoue
|
| 68 |
input_src = '-f lavfi -i color=c=0x228B22:s=720x1280'
|
| 69 |
|
| 70 |
+
# 3. FFmpeg - Format vertical TikTok/Shorts
|
| 71 |
cmd = (
|
| 72 |
f'ffmpeg -y {input_src} -i "{audio_file}" '
|
| 73 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
|
|
|
| 78 |
|
| 79 |
subprocess.run(cmd, shell=True)
|
| 80 |
|
| 81 |
+
# Nettoyage
|
| 82 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 83 |
if img and os.path.exists(img): os.remove(img)
|
| 84 |
|
| 85 |
return video_final if os.path.exists(video_final) else None
|
| 86 |
|
| 87 |
+
# --- INTERFACE ---
|
| 88 |
+
st.set_page_config(page_title="Prisme Foot Studio", page_icon="⚽")
|
| 89 |
st.title("⚽ Prisme Foot Studio Pro")
|
| 90 |
|
| 91 |
if 'actu' not in st.session_state: st.session_state['actu'] = ""
|
| 92 |
|
| 93 |
+
joueur = st.text_input("👤 Nom du Joueur", placeholder="Ex: Vinicius Junior")
|
| 94 |
|
| 95 |
+
if st.button("🤖 GÉNÉRER TEXTE IA"):
|
| 96 |
if joueur:
|
| 97 |
+
with st.spinner("L'IA rédige ton script..."):
|
| 98 |
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 99 |
+
else: st.warning("Mets un nom !")
|
| 100 |
|
| 101 |
+
actu_finale = st.text_area("📝 News", value=st.session_state['actu'], height=150)
|
| 102 |
|
| 103 |
if st.button("🎬 CRÉER LA VIDÉO"):
|
| 104 |
if joueur and actu_finale:
|
| 105 |
+
with st.spinner("Montage vidéo..."):
|
| 106 |
video = creer_video_expert(joueur, actu_finale)
|
| 107 |
+
if video and os.path.exists(video):
|
| 108 |
st.video(video)
|
| 109 |
+
st.download_button("📥 Télécharger", open(video, "rb"), file_name=f"{joueur}.mp4")
|
| 110 |
+
else:
|
| 111 |
+
st.error("Erreur technique. Vérifie tes clés API.")
|