PrismeFoot commited on
Commit
eb3e15c
·
verified ·
1 Parent(s): 2f83fce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -32
app.py CHANGED
@@ -7,14 +7,17 @@ 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
 
13
- # Structure exacte demandée par Groq
14
  data = {
15
  "model": "mixtral-8x7b-32768",
16
  "messages": [
17
- {"role": "system", "content": "Tu es un journaliste sportif sensationnel."},
18
  {"role": "user", "content": f"Écris un paragraphe de 5 phrases captivantes sur {joueur}."}
19
  ],
20
  "temperature": 0.7
@@ -26,17 +29,16 @@ def generer_actu_ia(joueur):
26
  return r.json()['choices'][0]['message']['content']
27
  return f"Erreur API Groq (Code {r.status_code}). Vérifie ta clé."
28
  except Exception as e:
29
- return f"Erreur : {e}"
30
 
31
  def obtenir_image_fond(sujet):
32
- # On cherche 'soccer' pour garantir des images de foot
33
  url = f"https://pexels.com{sujet}+soccer&per_page=1"
34
  headers = {"Authorization": PEXELS_API_KEY, "User-Agent": "Mozilla/5.0"}
35
  try:
36
  r = requests.get(url, headers=headers, timeout=10)
37
  data = r.json()
38
  if r.status_code == 200 and data.get('photos'):
39
- # CORRECTION : On accède bien au premier élément de la liste
40
  img_url = data['photos'][0]['src']['large2x']
41
  path = os.path.abspath(f"fond_{uuid.uuid4().hex[:4]}.jpg")
42
  with open(path, 'wb') as f:
@@ -50,63 +52,49 @@ def creer_video_expert(joueur, news):
50
  audio_file = os.path.abspath(f"audio_{uid}.mp3")
51
  video_final = os.path.abspath(f"PRISME_{uid}.mp4")
52
 
53
- # Nettoyage pour FFmpeg (crucial pour éviter les erreurs de texte)
54
  news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
55
  joueur_clean = joueur.replace("'", " ").replace('"', " ")
56
 
57
- # 1. Génération Audio
58
  try:
59
  tts = gTTS(text=news_clean, lang='fr')
60
  tts.save(audio_file)
61
  except: return None
62
 
63
- # 2. Image (Pexels ou secours)
64
  img = obtenir_image_fond(joueur)
65
- if img and os.path.exists(img):
66
- input_src = f'-loop 1 -i "{img}"'
67
- else:
68
- # Fond vert de secours si Pexels échoue
69
- input_src = '-f lavfi -i color=c=0x228B22:s=720x1280'
70
 
71
- # 3. FFmpeg - Format vertical TikTok/Shorts
72
  cmd = (
73
  f'ffmpeg -y {input_src} -i "{audio_file}" '
74
  f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
75
- f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150:box=1:boxcolor=black@0.6,'
76
  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" '
77
  f'-c:v libx264 -preset superfast -pix_fmt yuv420p -c:a aac -shortest "{video_final}"'
78
  )
79
 
80
  subprocess.run(cmd, shell=True)
81
-
82
- # Nettoyage
83
  if os.path.exists(audio_file): os.remove(audio_file)
84
  if img and os.path.exists(img): os.remove(img)
85
-
86
  return video_final if os.path.exists(video_final) else None
87
 
88
  # --- INTERFACE ---
89
- st.set_page_config(page_title="Prisme Foot Studio", page_icon="⚽")
90
  st.title("⚽ Prisme Foot Studio Pro")
91
-
92
  if 'actu' not in st.session_state: st.session_state['actu'] = ""
93
-
94
- joueur = st.text_input("👤 Nom du Joueur", placeholder="Ex: Vinicius Junior")
95
 
96
  if st.button("🤖 GÉNÉRER TEXTE IA"):
97
  if joueur:
98
- with st.spinner("L'IA rédige ton script..."):
99
  st.session_state['actu'] = generer_actu_ia(joueur)
100
  else: st.warning("Mets un nom !")
101
 
102
  actu_finale = st.text_area("📝 News", value=st.session_state['actu'], height=150)
103
 
104
  if st.button("🎬 CRÉER LA VIDÉO"):
105
- if joueur and actu_finale:
106
- with st.spinner("Montage vidéo..."):
107
- video = creer_video_expert(joueur, actu_finale)
108
- if video and os.path.exists(video):
109
- st.video(video)
110
- st.download_button("📥 Télécharger", open(video, "rb"), file_name=f"{joueur}.mp4")
111
- else:
112
- st.error("Erreur technique. Vérifie tes clés API.")
 
7
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
8
 
9
  def generer_actu_ia(joueur):
10
+ # URL OFFICIELLE GROQ
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
 
29
  return r.json()['choices'][0]['message']['content']
30
  return f"Erreur API Groq (Code {r.status_code}). Vérifie ta clé."
31
  except Exception as e:
32
+ return f"Erreur de connexion : {e}"
33
 
34
  def obtenir_image_fond(sujet):
 
35
  url = f"https://pexels.com{sujet}+soccer&per_page=1"
36
  headers = {"Authorization": PEXELS_API_KEY, "User-Agent": "Mozilla/5.0"}
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
+ # CORRECTION : On accède au premier élément de la liste 'photos'
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:
 
52
  audio_file = os.path.abspath(f"audio_{uid}.mp3")
53
  video_final = os.path.abspath(f"PRISME_{uid}.mp4")
54
 
55
+ # Nettoyage pour FFmpeg
56
  news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
57
  joueur_clean = joueur.replace("'", " ").replace('"', " ")
58
 
59
+ # 1. Audio
60
  try:
61
  tts = gTTS(text=news_clean, lang='fr')
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 "{audio_file}" '
72
  f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
 
73
  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" '
74
  f'-c:v libx264 -preset superfast -pix_fmt yuv420p -c:a aac -shortest "{video_final}"'
75
  )
76
 
77
  subprocess.run(cmd, shell=True)
 
 
78
  if os.path.exists(audio_file): os.remove(audio_file)
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
  # --- INTERFACE ---
 
83
  st.title("⚽ Prisme Foot Studio Pro")
 
84
  if 'actu' not in st.session_state: st.session_state['actu'] = ""
85
+ joueur = st.text_input("👤 Nom du Joueur")
 
86
 
87
  if st.button("🤖 GÉNÉRER TEXTE IA"):
88
  if joueur:
89
+ with st.spinner("Appel à Groq..."):
90
  st.session_state['actu'] = generer_actu_ia(joueur)
91
  else: st.warning("Mets un nom !")
92
 
93
  actu_finale = st.text_area("📝 News", value=st.session_state['actu'], height=150)
94
 
95
  if st.button("🎬 CRÉER LA VIDÉO"):
96
+ with st.spinner("Montage..."):
97
+ video = creer_video_expert(joueur, actu_finale)
98
+ if video:
99
+ st.video(video)
100
+ st.download_button("📥 Télécharger", open(video, "rb"), file_name=f"{joueur}.mp4")