PrismeFoot commited on
Commit
5c1017d
·
verified ·
1 Parent(s): 4d61e93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -53
app.py CHANGED
@@ -1,71 +1,105 @@
1
- import os, uuid, subprocess, feedparser, urllib.parse
2
  import streamlit as st
3
  from gtts import gTTS
4
 
5
- # --- FONCTION DE RECHERCHE ---
6
- def recuperer_news_reelle(joueur):
 
 
 
 
 
 
 
 
 
 
7
  try:
8
- query = urllib.parse.quote(f"{joueur} football")
9
- url = f"https://google.com{query}&hl=fr&gl=FR&ceid=FR:fr"
10
- flux = feedparser.parse(url)
11
- if flux.entries:
12
- return flux.entries[0].title
13
- return f"INFO : {joueur} continue de briller sur les terrains cette semaine."
14
  except:
15
- return f"ACTUALITÉ : {joueur} est au centre des discussions sportives."
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # --- MOTEUR VIDÉO (CORRIGÉ SANS SYNTAX ERROR) ---
18
- def fabriquer_video(joueur, texte):
19
  uid = uuid.uuid4().hex[:6]
20
- audio, video = f"a_{uid}.mp3", f"v_{uid}.mp4"
21
-
22
- texte_clean = texte.replace("'", " ").replace('"', " ")
23
- joueur_up = joueur.upper()
24
 
 
 
 
 
 
25
  try:
26
- # 1. Audio
27
- gTTS(text=texte_clean, lang='fr').save(audio)
 
28
 
29
- # 2. Préparation du filtre texte (en une seule ligne propre)
30
- filtre_vf = (
31
- f"drawbox=y=ih/2-100:color=black@0.6:width=iw:height=300:t=fill,"
32
- f"drawtext=text='PRISME FOOT':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150,"
33
- f"drawtext=text='{joueur_up}':fontcolor=white:fontsize=60:x=(w-text_w)/2:y=300:box=1:boxcolor=red@0.8,"
34
- f"drawtext=text='{texte_clean[:50]}...':fontcolor=white:fontsize=32:x=(w-text_w)/2:y=(h-text_h)/2+50"
35
- )
36
 
37
- # 3. Commande FFmpeg en liste (SÉCURISÉ)
38
- cmd = [
39
- "ffmpeg", "-y",
40
- "-f", "lavfi", "-i", "color=c=0x001a33:s=720x1280:d=5",
41
- "-i", audio,
42
- "-vf", filtre_vf,
43
- "-c:v", "libx264", "-c:a", "aac", "-shortest", video
44
- ]
 
 
 
 
 
 
45
 
46
- subprocess.run(cmd, check=True)
47
- if os.path.exists(audio): os.remove(audio)
48
- return video
49
- except Exception as e:
50
- st.error(f"Erreur rendu : {e}")
51
- return None
52
 
53
- # --- INTERFACE ---
54
- st.title("⚽ Prisme Foot Studio - Fix")
 
55
 
56
- if 'script' not in st.session_state:
57
- st.session_state['script'] = ""
58
 
59
- nom = st.text_input("👤 Nom du joueur", "Vinicius Junior")
60
 
61
- if st.button("🔍 1. CHERCHER L'ACTU"):
62
- st.session_state['script'] = recuperer_news_reelle(nom)
 
 
 
 
63
 
64
- script_final = st.text_area("📝 Script", value=st.session_state['script'], height=100)
65
 
66
- if st.button("🎬 2. CRÉER LA VIDÉO"):
67
- if nom and script_final:
68
- with st.spinner("Montage..."):
69
- v = fabriquer_video(nom, script_final)
70
- if v:
71
- st.video(v)
 
 
 
 
 
 
 
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
+ def generer_actu_ia(joueur):
10
+ url = "https://groq.com"
11
+ headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
12
+ data = {
13
+ "model": "mixtral-8x7b-32768",
14
+ "messages": [{"role": "user", "content": f"Écris une news foot virale et courte sur {joueur}."}],
15
+ "temperature": 0.7
16
+ }
17
  try:
18
+ r = requests.post(url, headers=headers, json=data, timeout=10)
19
+ # Correction de l'indexation JSON ici
20
+ return r.json()['choices'][0]['message']['content']
 
 
 
21
  except:
22
+ return f"{joueur} impressionne tout le monde avec une performance incroyable ! ⚽"
23
+
24
+ def obtenir_image_fond(sujet):
25
+ url = f"https://pexels.com{sujet}+football&per_page=1"
26
+ headers = {"Authorization": PEXELS_API_KEY}
27
+ try:
28
+ r = requests.get(url, headers=headers, timeout=10)
29
+ if r.status_code == 200 and r.json().get('photos'):
30
+ img_url = r.json()['photos'][0]['src']['large2x']
31
+ path = f"fond_{uuid.uuid4().hex[:4]}.jpg"
32
+ with open(path, 'wb') as f: f.write(requests.get(img_url).content)
33
+ return path
34
+ except: pass
35
+ return None
36
 
37
+ def creer_video_expert(joueur, news):
 
38
  uid = uuid.uuid4().hex[:6]
39
+ audio_file = f"audio_{uid}.mp3"
40
+ video_final = f"PRISME_{uid}.mp4"
 
 
41
 
42
+ # NETTOYAGE CRUCIAL DES TEXTES POUR FFMPEG
43
+ news_clean = news.replace("'", " ").replace('"', " ").replace("\n", " ")
44
+ joueur_clean = joueur.replace("'", " ").replace('"', " ")
45
+
46
+ # 1. Génération Audio
47
  try:
48
+ tts = gTTS(text=f"Alerte Info ! {news_clean}", lang='fr')
49
+ tts.save(audio_file)
50
+ except: return None
51
 
52
+ # 2. Source Image
53
+ img = obtenir_image_fond(joueur)
54
+ if img and os.path.exists(img):
55
+ input_source = f'-loop 1 -i {img}'
56
+ else:
57
+ input_source = '-f lavfi -i color=c=black:s=720x1280'
 
58
 
59
+ # 3. Commande FFmpeg Blindée
60
+ cmd = (
61
+ f'ffmpeg {input_source} -i {audio_file} '
62
+ f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
63
+ f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=40:x=(w-text_w)/2:y=120:box=1:boxcolor=black@0.5,'
64
+ f'drawtext=text=\'{joueur_clean.upper()}\':fontcolor=white:fontsize=55:x=(w-text_w)/2:y=(h-text_h)/2:box=1:boxcolor=red@0.7" '
65
+ f'-c:v libx264 -preset superfast -c:a aac -shortest {video_final} -y'
66
+ )
67
+
68
+ subprocess.run(cmd, shell=True)
69
+
70
+ # Nettoyage des fichiers temporaires
71
+ if os.path.exists(audio_file): os.remove(audio_file)
72
+ if img and os.path.exists(img): os.remove(img)
73
 
74
+ return video_final if os.path.exists(video_final) else None
 
 
 
 
 
75
 
76
+ # --- INTERFACE STREAMLIT ---
77
+ st.set_page_config(page_title="Prisme Foot Studio", page_icon="⚽")
78
+ st.title("⚽ Prisme Foot Studio")
79
 
80
+ if 'actu' not in st.session_state:
81
+ st.session_state['actu'] = ""
82
 
83
+ joueur = st.text_input("👤 Nom du Joueur", placeholder="Ex: Vinicius Junior")
84
 
85
+ if st.button("🤖 GÉNÉRER TEXTE IA"):
86
+ if joueur:
87
+ with st.spinner("L'IA rédige..."):
88
+ st.session_state['actu'] = generer_actu_ia(joueur)
89
+ else:
90
+ st.warning("Veuillez entrer un nom.")
91
 
92
+ actu_finale = st.text_area("📝 News (modifiables)", value=st.session_state['actu'], height=100)
93
 
94
+ if st.button("🎬 CRÉER LA VIDÉO"):
95
+ if joueur and actu_finale:
96
+ with st.spinner("Montage vidéo en cours..."):
97
+ video = creer_video_expert(joueur, actu_finale)
98
+ if video and os.path.exists(video):
99
+ st.video(video)
100
+ with open(video, "rb") as f:
101
+ st.download_button("📥 Télécharger la vidéo", f, file_name=video)
102
+ else:
103
+ st.error("Erreur technique. Vérifie tes clés API Pexels et Groq dans les Settings.")
104
+ else:
105
+ st.error("Remplis tous les champs !")