Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,94 +7,78 @@ 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
|
| 11 |
url = "https://groq.com"
|
| 12 |
headers = {
|
| 13 |
-
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 14 |
"Content-Type": "application/json"
|
| 15 |
}
|
| 16 |
-
|
| 17 |
data = {
|
| 18 |
"model": "mixtral-8x7b-32768",
|
| 19 |
-
"messages": [
|
| 20 |
-
{"role": "system", "content": "Tu es un journaliste sportif expert en football."},
|
| 21 |
-
{"role": "user", "content": f"Écris un paragraphe de 5 phrases captivantes sur {joueur}."}
|
| 22 |
-
],
|
| 23 |
"temperature": 0.7
|
| 24 |
}
|
| 25 |
-
|
| 26 |
try:
|
| 27 |
r = requests.post(url, headers=headers, json=data, timeout=15)
|
| 28 |
if r.status_code == 200:
|
| 29 |
return r.json()['choices'][0]['message']['content']
|
| 30 |
-
return f"Erreur
|
| 31 |
except Exception as e:
|
| 32 |
-
return f"Erreur
|
| 33 |
|
| 34 |
def obtenir_image_fond(sujet):
|
| 35 |
url = f"https://pexels.com{sujet}+soccer&per_page=1"
|
| 36 |
-
headers = {"Authorization": PEXELS_API_KEY
|
| 37 |
try:
|
| 38 |
r = requests.get(url, headers=headers, timeout=10)
|
| 39 |
data = r.json()
|
| 40 |
if r.status_code == 200 and data.get('photos'):
|
| 41 |
-
|
| 42 |
-
img_url = data['photos'][0]['src']['large2x']
|
| 43 |
path = os.path.abspath(f"fond_{uuid.uuid4().hex[:4]}.jpg")
|
| 44 |
with open(path, 'wb') as f:
|
| 45 |
f.write(requests.get(img_url).content)
|
| 46 |
return path
|
| 47 |
-
except:
|
| 48 |
return None
|
| 49 |
|
| 50 |
def creer_video_expert(joueur, news):
|
| 51 |
uid = uuid.uuid4().hex[:6]
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Nettoyage
|
| 56 |
news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# 1. Audio
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
tts.save(audio_file)
|
| 63 |
except: return None
|
| 64 |
|
| 65 |
-
# 2. Image
|
| 66 |
img = obtenir_image_fond(joueur)
|
| 67 |
input_src = f'-loop 1 -i "{img}"' if img else '-f lavfi -i color=c=0x1a1a1a:s=720x1280'
|
| 68 |
|
| 69 |
-
# 3. FFmpeg
|
| 70 |
cmd = (
|
| 71 |
-
f'ffmpeg -y {input_src} -i "{
|
| 72 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
| 73 |
-
f'drawtext=text=\'{
|
| 74 |
-
f'-c:v libx264 -preset superfast -pix_fmt yuv420p -c:a aac -shortest "{
|
| 75 |
)
|
| 76 |
-
|
| 77 |
subprocess.run(cmd, shell=True)
|
| 78 |
-
if os.path.exists(
|
| 79 |
-
if img and os.path.exists(img): os.remove(img)
|
| 80 |
-
return video_final if os.path.exists(video_final) else None
|
| 81 |
|
| 82 |
-
# ---
|
| 83 |
-
st.title("⚽ Prisme Foot Studio
|
| 84 |
if 'actu' not in st.session_state: st.session_state['actu'] = ""
|
| 85 |
-
joueur = st.text_input("👤 Nom du Joueur")
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
st.session_state['actu'] = generer_actu_ia(joueur)
|
| 91 |
-
else: st.warning("Mets un nom !")
|
| 92 |
|
| 93 |
-
actu_finale = st.text_area("
|
| 94 |
|
| 95 |
-
if st.button("🎬
|
| 96 |
with st.spinner("Montage..."):
|
| 97 |
-
|
| 98 |
-
if
|
| 99 |
-
st.video(
|
| 100 |
-
st.download_button("📥 Télécharger", open(
|
|
|
|
|
|
| 7 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 8 |
|
| 9 |
def generer_actu_ia(joueur):
|
| 10 |
+
# L'URL doit être EXACTEMENT celle-ci
|
| 11 |
url = "https://groq.com"
|
| 12 |
headers = {
|
| 13 |
+
"Authorization": f"Bearer {GROQ_API_KEY.strip()}",
|
| 14 |
"Content-Type": "application/json"
|
| 15 |
}
|
|
|
|
| 16 |
data = {
|
| 17 |
"model": "mixtral-8x7b-32768",
|
| 18 |
+
"messages": [{"role": "user", "content": f"Rédige 5 phrases de news foot sur {joueur}."}],
|
|
|
|
|
|
|
|
|
|
| 19 |
"temperature": 0.7
|
| 20 |
}
|
|
|
|
| 21 |
try:
|
| 22 |
r = requests.post(url, headers=headers, json=data, timeout=15)
|
| 23 |
if r.status_code == 200:
|
| 24 |
return r.json()['choices'][0]['message']['content']
|
| 25 |
+
return f"Erreur {r.status_code}: {r.text}"
|
| 26 |
except Exception as e:
|
| 27 |
+
return f"Erreur connexion: {e}"
|
| 28 |
|
| 29 |
def obtenir_image_fond(sujet):
|
| 30 |
url = f"https://pexels.com{sujet}+soccer&per_page=1"
|
| 31 |
+
headers = {"Authorization": PEXELS_API_KEY}
|
| 32 |
try:
|
| 33 |
r = requests.get(url, headers=headers, timeout=10)
|
| 34 |
data = r.json()
|
| 35 |
if r.status_code == 200 and data.get('photos'):
|
| 36 |
+
img_url = data['photos'][0]['src']['large2x'] # Index [0] ajouté ici
|
|
|
|
| 37 |
path = os.path.abspath(f"fond_{uuid.uuid4().hex[:4]}.jpg")
|
| 38 |
with open(path, 'wb') as f:
|
| 39 |
f.write(requests.get(img_url).content)
|
| 40 |
return path
|
| 41 |
+
except: return None
|
| 42 |
return None
|
| 43 |
|
| 44 |
def creer_video_expert(joueur, news):
|
| 45 |
uid = uuid.uuid4().hex[:6]
|
| 46 |
+
audio = os.path.abspath(f"a_{uid}.mp3")
|
| 47 |
+
video = os.path.abspath(f"PRISME_{uid}.mp4")
|
| 48 |
+
|
| 49 |
+
# Nettoyage
|
| 50 |
news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
|
| 51 |
+
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
+
gTTS(text=news_clean, lang='fr').save(audio)
|
|
|
|
| 54 |
except: return None
|
| 55 |
|
|
|
|
| 56 |
img = obtenir_image_fond(joueur)
|
| 57 |
input_src = f'-loop 1 -i "{img}"' if img else '-f lavfi -i color=c=0x1a1a1a:s=720x1280'
|
| 58 |
|
|
|
|
| 59 |
cmd = (
|
| 60 |
+
f'ffmpeg -y {input_src} -i "{audio}" '
|
| 61 |
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
| 62 |
+
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" '
|
| 63 |
+
f'-c:v libx264 -preset superfast -pix_fmt yuv420p -c:a aac -shortest "{video}"'
|
| 64 |
)
|
|
|
|
| 65 |
subprocess.run(cmd, shell=True)
|
| 66 |
+
return video if os.path.exists(video) else None
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# --- UI ---
|
| 69 |
+
st.title("⚽ Prisme Foot Studio")
|
| 70 |
if 'actu' not in st.session_state: st.session_state['actu'] = ""
|
|
|
|
| 71 |
|
| 72 |
+
joueur = st.text_input("Joueur")
|
| 73 |
+
if st.button("🤖 TEXTE IA"):
|
| 74 |
+
st.session_state['actu'] = generer_actu_ia(joueur)
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
actu_finale = st.text_area("News", value=st.session_state['actu'], height=150)
|
| 77 |
|
| 78 |
+
if st.button("🎬 VIDÉO"):
|
| 79 |
with st.spinner("Montage..."):
|
| 80 |
+
res = creer_video_expert(joueur, actu_finale)
|
| 81 |
+
if res:
|
| 82 |
+
st.video(res)
|
| 83 |
+
st.download_button("📥 Télécharger", open(res, "rb"), file_name=f"{joueur}.mp4")
|
| 84 |
+
Uti
|