Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,37 +7,41 @@ 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 = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
data = {
|
| 13 |
"model": "mixtral-8x7b-32768",
|
| 14 |
-
"messages": [{"role": "user", "content":
|
| 15 |
"temperature": 0.7
|
| 16 |
}
|
| 17 |
try:
|
| 18 |
r = requests.post(url, headers=headers, json=data, timeout=15)
|
| 19 |
-
# On vérifie si la requête a réussi avant de lire le JSON
|
| 20 |
if r.status_code != 200:
|
| 21 |
-
return f"Erreur API Groq (Code {r.status_code}). Vérifie ta clé
|
| 22 |
return r.json()['choices'][0]['message']['content']
|
| 23 |
except Exception as e:
|
| 24 |
return f"Erreur de connexion : {e}"
|
| 25 |
|
| 26 |
def obtenir_image_fond(sujet):
|
| 27 |
url = f"https://pexels.com{sujet}+soccer&per_page=1"
|
| 28 |
-
headers = {"Authorization": PEXELS_API_KEY}
|
| 29 |
try:
|
| 30 |
r = requests.get(url, headers=headers, timeout=10)
|
| 31 |
data = r.json()
|
| 32 |
if r.status_code == 200 and data.get('photos'):
|
| 33 |
-
#
|
| 34 |
-
img_url = data['photos'][0]['src']['large2x']
|
| 35 |
path = os.path.abspath(f"fond_{uuid.uuid4().hex[:4]}.jpg")
|
| 36 |
with open(path, 'wb') as f:
|
| 37 |
f.write(requests.get(img_url).content)
|
| 38 |
return path
|
| 39 |
-
except:
|
| 40 |
-
pass
|
| 41 |
return None
|
| 42 |
|
| 43 |
def creer_video_expert(joueur, news):
|
|
@@ -45,28 +49,25 @@ def creer_video_expert(joueur, news):
|
|
| 45 |
audio_file = os.path.abspath(f"audio_{uid}.mp3")
|
| 46 |
video_final = os.path.abspath(f"PRISME_{uid}.mp4")
|
| 47 |
|
| 48 |
-
# Nettoyage
|
| 49 |
news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
|
| 50 |
joueur_clean = joueur.replace("'", " ").replace('"', " ")
|
| 51 |
|
| 52 |
-
# 1. Audio
|
| 53 |
try:
|
| 54 |
tts = gTTS(text=news_clean, lang='fr')
|
| 55 |
tts.save(audio_file)
|
| 56 |
-
except
|
| 57 |
-
st.error(f"Erreur Audio : {e}")
|
| 58 |
-
return None
|
| 59 |
|
| 60 |
# 2. Image de fond
|
| 61 |
img = obtenir_image_fond(joueur)
|
| 62 |
if img and os.path.exists(img):
|
| 63 |
-
|
| 64 |
-
input_src = f'-loop 1 -t 15 -i "{img}"'
|
| 65 |
else:
|
| 66 |
-
#
|
| 67 |
-
input_src = '-f lavfi -i color=c=0x228B22:s=720x1280
|
| 68 |
|
| 69 |
-
# 3. Commande FFmpeg
|
| 70 |
cmd = (
|
| 71 |
f'ffmpeg -y {input_src} -i "{audio_file}" '
|
| 72 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
|
@@ -75,30 +76,28 @@ def creer_video_expert(joueur, news):
|
|
| 75 |
f'-c:v libx264 -preset superfast -pix_fmt yuv420p -c:a aac -shortest "{video_final}"'
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
st.error(f"Erreur FFmpeg : {result.stderr}")
|
| 81 |
-
|
| 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 Pro")
|
| 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"
|
| 94 |
|
| 95 |
-
if st.button("🤖 GÉNÉRER UNE NEWS
|
| 96 |
if joueur:
|
| 97 |
-
with st.spinner("L'IA
|
| 98 |
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 99 |
else: st.error("Mets un nom !")
|
| 100 |
|
| 101 |
-
actu_finale = st.text_area("📝 Script
|
| 102 |
|
| 103 |
if st.button("🎬 CRÉER LA VIDÉO"):
|
| 104 |
if joueur and actu_finale:
|
|
|
|
| 7 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 8 |
|
| 9 |
def generer_actu_ia(joueur):
|
| 10 |
+
# L'URL EXACTE de l'API Groq (ne pas changer)
|
| 11 |
url = "https://groq.com"
|
| 12 |
+
headers = {
|
| 13 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 14 |
+
"Content-Type": "application/json"
|
| 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.7
|
| 22 |
}
|
| 23 |
try:
|
| 24 |
r = requests.post(url, headers=headers, json=data, timeout=15)
|
|
|
|
| 25 |
if r.status_code != 200:
|
| 26 |
+
return f"Erreur API Groq (Code {r.status_code}). Vérifie ta clé et l'URL."
|
| 27 |
return r.json()['choices'][0]['message']['content']
|
| 28 |
except Exception as e:
|
| 29 |
return f"Erreur de connexion : {e}"
|
| 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 |
+
# ACCÈS AU PREMIER ÉLÉMENT [0] (Très important)
|
| 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:
|
| 42 |
f.write(requests.get(img_url).content)
|
| 43 |
return path
|
| 44 |
+
except: pass
|
|
|
|
| 45 |
return None
|
| 46 |
|
| 47 |
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 (évite les crashs sur les apostrophes)
|
| 53 |
news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
|
| 54 |
joueur_clean = joueur.replace("'", " ").replace('"', " ")
|
| 55 |
|
| 56 |
+
# 1. Génération Audio
|
| 57 |
try:
|
| 58 |
tts = gTTS(text=news_clean, lang='fr')
|
| 59 |
tts.save(audio_file)
|
| 60 |
+
except: return None
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# 2. Image de fond
|
| 63 |
img = obtenir_image_fond(joueur)
|
| 64 |
if img and os.path.exists(img):
|
| 65 |
+
input_src = f'-loop 1 -i "{img}"'
|
|
|
|
| 66 |
else:
|
| 67 |
+
# Fond vert de secours si Pexels échoue
|
| 68 |
+
input_src = '-f lavfi -i color=c=0x228B22:s=720x1280'
|
| 69 |
|
| 70 |
+
# 3. Commande FFmpeg
|
| 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,'
|
|
|
|
| 76 |
f'-c:v libx264 -preset superfast -pix_fmt yuv420p -c:a aac -shortest "{video_final}"'
|
| 77 |
)
|
| 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 STREAMLIT ---
|
| 87 |
st.set_page_config(page_title="Prisme Foot Studio Pro")
|
| 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 joueur")
|
| 93 |
|
| 94 |
+
if st.button("🤖 GÉNÉRER UNE NEWS"):
|
| 95 |
if joueur:
|
| 96 |
+
with st.spinner("L'IA travaille..."):
|
| 97 |
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 98 |
else: st.error("Mets un nom !")
|
| 99 |
|
| 100 |
+
actu_finale = st.text_area("📝 Script", value=st.session_state['actu'], height=150)
|
| 101 |
|
| 102 |
if st.button("🎬 CRÉER LA VIDÉO"):
|
| 103 |
if joueur and actu_finale:
|