PrismeFoot commited on
Commit
b65c1be
·
verified ·
1 Parent(s): 09bec4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -61
app.py CHANGED
@@ -1,116 +1,92 @@
1
- import os, random, uuid, json, subprocess, asyncio, requests
2
- import edge_tts
3
  import streamlit as st
4
  from gtts import gTTS
5
 
6
  # --- CONFIGURATION ---
7
- PEXELS_API_KEY = os.getenv("F0OHUeXv0LuK88CZYcc7v4mMqTupignugy2CkSHh4nW7r21pMWAo49ny")
8
- GROQ_API_KEY = os.getenv("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")
9
- VOIX_FR = "fr-FR-RemyNeural"
10
-
11
- # Initialisation du session_state pour éviter les erreurs au premier chargement
12
- if 'actu' not in st.session_state:
13
- st.session_state['actu'] = ""
14
 
15
  # --- FONCTIONS ---
16
  def generer_actu_ia(joueur):
17
- url = "https://groq.com"
18
- URL = "https://groq.com"
19
  headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
20
  prompt = f"Écris une news de foot ultra-virale sur {joueur}. Maximum 2 phrases."
21
-
22
  data = {
23
  "model": "mixtral-8x7b-32768",
24
  "messages": [{"role": "user", "content": prompt}],
25
  "temperature": 0.7
26
  }
27
-
28
  try:
29
  r = requests.post(url, headers=headers, json=data, timeout=10)
30
  return r.json()['choices'][0]['message']['content']
31
  except:
32
- return f"{joueur} vient de réaliser une performance incroyable ! ⚽"
33
 
34
  def obtenir_image_fond(sujet):
35
- if not os.path.exists("images_actu"): os.makedirs("images_actu")
36
- # Correction de l'URL Pexels
37
- url = f"https://pexels.com{sujet}+football&per_page=1"
38
  headers = {"Authorization": PEXELS_API_KEY}
39
  try:
40
  r = requests.get(url, headers=headers, timeout=10)
41
  if r.status_code == 200 and r.json().get('photos'):
42
  img_url = r.json()['photos'][0]['src']['large2x']
43
- nom_image = f"images_actu/fond_{uuid.uuid4().hex[:4]}.jpg"
44
- with open(nom_image, 'wb') as f: f.write(requests.get(img_url).content)
45
- return nom_image
46
  except: pass
47
  return "color=black@0.5:s=720x1280"
48
 
49
- # --- MOTEUR DE PRODUCTION ---
50
- def creer_video_expert(joueur, news_brute):
51
  uid = uuid.uuid4().hex[:6]
52
- audio_file = f"audio_{uid}.mp3"
53
  video_final = f"PRISME_{uid}.mp4"
 
54
 
55
- # 1. Préparation du texte
56
- script = f"Alerte Info ! {news_brute} Abonne-toi à Prisme Foot !"
57
-
58
- # 2. Génération Audio (Simple et robuste)
59
  try:
60
- from gtts import gTTS
61
  tts = gTTS(text=script, lang='fr')
62
  tts.save(audio_file)
63
- except Exception as e:
64
- st.error(f"Erreur Audio : {e}")
65
- return None
66
 
67
- # 3. Image de fond
68
- image_path = obtenir_image_fond(joueur)
69
- input_source = f'-i "{image_path}"' if not image_path.startswith("color") else f'-f lavfi -i "{image_path}"'
70
 
71
- # 4. LA VOICI : La commande FFmpeg (cmd)
72
  cmd = (
73
  f'ffmpeg -loop 1 {input_source} -i {audio_file} '
74
  f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
75
- f'zoompan=z=\'min(zoom+0.0015,1.5)\':d=125:s=720x1280,'
76
- f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150:box=1:boxcolor=black@0.6,'
77
- 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" '
78
  f'-c:v libx264 -preset superfast -tune stillimage -c:a aac -shortest {video_final} -y'
79
  )
80
 
81
- # Exécution de la commande
82
  subprocess.run(cmd, shell=True)
83
-
84
- # 5. Nettoyage
85
  if os.path.exists(audio_file): os.remove(audio_file)
86
  return video_final if os.path.exists(video_final) else None
87
 
88
  # --- INTERFACE ---
89
- st.set_page_config(page_title="Prisme Foot AI", page_icon="⚽")
90
- st.title("⚽ Prisme Foot : Studio IA")
 
 
91
 
92
  joueur = st.text_input("👤 Nom du Joueur")
93
- col1, col2 = st.columns(2)
94
 
95
- with col1:
96
- if st.button("🤖 IA : ÉCRIRE"):
97
- if joueur:
98
- with st.spinner("L'IA réfléchit..."):
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'])
103
 
104
- if st.button("🎬 GÉNÉRER LA VIDÉO"):
105
  if joueur and actu_finale:
106
- with st.spinner("🛠️ Montage en cours..."):
107
- # Pas de asyncio.run ici, appel direct
108
- fichier = creer_video_expert(joueur, actu_finale)
109
-
110
- # Vérification de l'existence du fichier (Ligne 108)
111
- if fichier and os.path.exists(fichier):
112
- st.video(fichier)
113
- with open(fichier, "rb") as f:
114
- st.download_button("📥 Télécharger", f, file_name=fichier)
115
  else:
116
- st.error("Échec de la génération de la vidéo. Vérifie tes clés API.")
 
1
+ import os, uuid, subprocess, requests
 
2
  import streamlit as st
3
  from gtts import gTTS
4
 
5
  # --- CONFIGURATION ---
6
+ PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
7
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
 
 
 
 
 
8
 
9
  # --- FONCTIONS ---
10
  def generer_actu_ia(joueur):
11
+ url = "https://groq.com"
 
12
  headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
13
  prompt = f"Écris une news de foot ultra-virale sur {joueur}. Maximum 2 phrases."
 
14
  data = {
15
  "model": "mixtral-8x7b-32768",
16
  "messages": [{"role": "user", "content": prompt}],
17
  "temperature": 0.7
18
  }
 
19
  try:
20
  r = requests.post(url, headers=headers, json=data, timeout=10)
21
  return r.json()['choices'][0]['message']['content']
22
  except:
23
+ return f"{joueur} impressionne tout le monde avec une performance incroyable ! ⚽"
24
 
25
  def obtenir_image_fond(sujet):
26
+ if not os.path.exists("temp"): os.makedirs("temp")
27
+ url = f"https://pexels.com{sujet}+football&per_page=1"
 
28
  headers = {"Authorization": PEXELS_API_KEY}
29
  try:
30
  r = requests.get(url, headers=headers, timeout=10)
31
  if r.status_code == 200 and r.json().get('photos'):
32
  img_url = r.json()['photos'][0]['src']['large2x']
33
+ path = f"temp/fond_{uuid.uuid4().hex[:4]}.jpg"
34
+ with open(path, 'wb') as f: f.write(requests.get(img_url).content)
35
+ return path
36
  except: pass
37
  return "color=black@0.5:s=720x1280"
38
 
39
+ def creer_video_expert(joueur, news):
 
40
  uid = uuid.uuid4().hex[:6]
41
+ audio_file = f"temp/audio_{uid}.mp3"
42
  video_final = f"PRISME_{uid}.mp4"
43
+ if not os.path.exists("temp"): os.makedirs("temp")
44
 
45
+ # 1. Audio
46
+ script = f"Alerte Info ! {news} Abonne-toi à Prisme Foot !"
 
 
47
  try:
 
48
  tts = gTTS(text=script, lang='fr')
49
  tts.save(audio_file)
50
+ except: return None
 
 
51
 
52
+ # 2. Image
53
+ img = obtenir_image_fond(joueur)
54
+ input_source = f'-i {img}' if not img.startswith("color") else f'-f lavfi -i "{img}"'
55
 
56
+ # 3. FFmpeg
57
  cmd = (
58
  f'ffmpeg -loop 1 {input_source} -i {audio_file} '
59
  f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
60
+ f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=40:x=(w-text_w)/2:y=100:box=1:boxcolor=black@0.5,'
61
+ f'drawtext=text=\'{joueur.upper()}\':fontcolor=white:fontsize=50:x=(w-text_w)/2:y=(h-text_h)/2:box=1:boxcolor=red@0.7" '
 
62
  f'-c:v libx264 -preset superfast -tune stillimage -c:a aac -shortest {video_final} -y'
63
  )
64
 
 
65
  subprocess.run(cmd, shell=True)
 
 
66
  if os.path.exists(audio_file): os.remove(audio_file)
67
  return video_final if os.path.exists(video_final) else None
68
 
69
  # --- INTERFACE ---
70
+ st.set_page_config(page_title="Prisme Foot AI")
71
+ st.title("⚽ Prisme Foot Studio")
72
+
73
+ if 'actu' not in st.session_state: st.session_state['actu'] = ""
74
 
75
  joueur = st.text_input("👤 Nom du Joueur")
 
76
 
77
+ if st.button("🤖 GÉNÉRER TEXTE"):
78
+ if joueur:
79
+ st.session_state['actu'] = generer_actu_ia(joueur)
80
+ else: st.error("Entrez un nom !")
 
 
81
 
82
  actu_finale = st.text_area("📝 News", value=st.session_state['actu'])
83
 
84
+ if st.button("🎬 CRÉER VIDÉO"):
85
  if joueur and actu_finale:
86
+ with st.spinner("Montage en cours..."):
87
+ video = creer_video_expert(joueur, actu_finale)
88
+ if video:
89
+ st.video(video)
90
+ st.download_button("📥 Télécharger", open(video, "rb"), file_name=video)
 
 
 
 
91
  else:
92
+ st.error("Erreur technique. Vérifiez vos clés API dans les Settings.")