Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,152 +1,42 @@
|
|
| 1 |
-
from flask import Flask, request, send_file
|
| 2 |
from pydub import AudioSegment
|
| 3 |
from io import BytesIO
|
| 4 |
import requests
|
| 5 |
import zipfile
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
# 1. L'INTERFACE GRAPHIQUE (HTML/CSS/JS)
|
| 12 |
-
# ==========================================
|
| 13 |
-
HTML_TEMPLATE = """
|
| 14 |
-
<!DOCTYPE html>
|
| 15 |
-
<html lang="fr">
|
| 16 |
-
<head>
|
| 17 |
-
<meta charset="UTF-8">
|
| 18 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 19 |
-
<title>PodGen AI Studio</title>
|
| 20 |
-
<script src="https://cdn.tailwindcss.com"></script>
|
| 21 |
-
<style>
|
| 22 |
-
body { background-color: #0f172a; color: #e2e8f0; font-family: sans-serif; }
|
| 23 |
-
.glass { background: rgba(30, 41, 59, 0.7); backdrop-filter: blur(10px); border: 1px solid rgba(255, 255, 255, 0.1); }
|
| 24 |
-
</style>
|
| 25 |
-
</head>
|
| 26 |
-
<body class="min-h-screen flex flex-col items-center justify-center p-4">
|
| 27 |
-
<div class="glass p-8 rounded-2xl shadow-2xl w-full max-w-2xl">
|
| 28 |
-
<div class="flex items-center gap-3 mb-6">
|
| 29 |
-
<div class="h-8 w-8 bg-blue-500 rounded-full flex items-center justify-center text-white font-bold">P</div>
|
| 30 |
-
<h1 class="text-2xl font-bold bg-clip-text text-transparent bg-gradient-to-r from-blue-400 to-purple-500">PodGen AI Studio</h1>
|
| 31 |
-
</div>
|
| 32 |
-
|
| 33 |
-
<p class="text-slate-400 mb-6 text-sm">Upload tes segments audio, assigne les speakers, et récupère le mix synchronisé prêt pour l'animation.</p>
|
| 34 |
-
|
| 35 |
-
<form id="uploadForm" class="space-y-4">
|
| 36 |
-
<div id="fileList" class="space-y-2">
|
| 37 |
-
</div>
|
| 38 |
-
|
| 39 |
-
<button type="button" onclick="addFileRow()" class="w-full py-3 border-2 border-dashed border-slate-600 rounded-lg text-slate-400 hover:border-blue-500 hover:text-blue-400 transition flex items-center justify-center gap-2">
|
| 40 |
-
<span>+ Ajouter un segment audio</span>
|
| 41 |
-
</button>
|
| 42 |
-
|
| 43 |
-
<button type="submit" id="submitBtn" class="w-full bg-blue-600 hover:bg-blue-500 text-white font-bold py-3 rounded-lg shadow-lg shadow-blue-500/30 transition mt-6">
|
| 44 |
-
Générer & Télécharger le ZIP
|
| 45 |
-
</button>
|
| 46 |
-
</form>
|
| 47 |
-
</div>
|
| 48 |
-
|
| 49 |
-
<script>
|
| 50 |
-
function addFileRow() {
|
| 51 |
-
const div = document.createElement('div');
|
| 52 |
-
div.className = 'flex gap-3 items-center glass p-3 rounded-lg animate-fade-in';
|
| 53 |
-
div.innerHTML = `
|
| 54 |
-
<select name="speakers[]" class="bg-slate-800 border border-slate-600 rounded px-2 py-2 text-sm focus:outline-none focus:border-blue-500">
|
| 55 |
-
<option value="1">Speaker 1</option>
|
| 56 |
-
<option value="2">Speaker 2</option>
|
| 57 |
-
</select>
|
| 58 |
-
<input type="file" name="files[]" accept="audio/*" class="block w-full text-sm text-slate-400 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-semibold file:bg-slate-700 file:text-blue-400 hover:file:bg-slate-600" required>
|
| 59 |
-
<button type="button" onclick="this.parentElement.remove()" class="text-red-400 hover:text-red-300 px-2">✕</button>
|
| 60 |
-
`;
|
| 61 |
-
document.getElementById('fileList').appendChild(div);
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
// Ajouter une première ligne au démarrage
|
| 65 |
-
addFileRow();
|
| 66 |
-
|
| 67 |
-
document.getElementById('uploadForm').onsubmit = async (e) => {
|
| 68 |
-
e.preventDefault();
|
| 69 |
-
const btn = document.getElementById('submitBtn');
|
| 70 |
-
btn.innerHTML = 'Traitement en cours... (Ne fermez pas)';
|
| 71 |
-
btn.disabled = true;
|
| 72 |
-
btn.classList.add('opacity-50', 'cursor-not-allowed');
|
| 73 |
-
|
| 74 |
-
const formData = new FormData(e.target);
|
| 75 |
-
|
| 76 |
-
try {
|
| 77 |
-
const response = await fetch('/web-generate', {
|
| 78 |
-
method: 'POST',
|
| 79 |
-
body: formData
|
| 80 |
-
});
|
| 81 |
-
|
| 82 |
-
if (response.ok) {
|
| 83 |
-
const blob = await response.blob();
|
| 84 |
-
const url = window.URL.createObjectURL(blob);
|
| 85 |
-
const a = document.createElement('a');
|
| 86 |
-
a.href = url;
|
| 87 |
-
a.download = "podcast_pack.zip";
|
| 88 |
-
document.body.appendChild(a);
|
| 89 |
-
a.click();
|
| 90 |
-
window.URL.revokeObjectURL(url);
|
| 91 |
-
btn.innerHTML = 'Succès ! Téléchargement lancé';
|
| 92 |
-
} else {
|
| 93 |
-
alert("Erreur lors de la génération.");
|
| 94 |
-
btn.innerHTML = 'Réessayer';
|
| 95 |
-
}
|
| 96 |
-
} catch (err) {
|
| 97 |
-
console.error(err);
|
| 98 |
-
alert("Erreur réseau");
|
| 99 |
-
btn.innerHTML = 'Réessayer';
|
| 100 |
-
}
|
| 101 |
-
btn.disabled = false;
|
| 102 |
-
btn.classList.remove('opacity-50', 'cursor-not-allowed');
|
| 103 |
-
};
|
| 104 |
-
</script>
|
| 105 |
-
</body>
|
| 106 |
-
</html>
|
| 107 |
-
"""
|
| 108 |
-
|
| 109 |
-
# ==========================================
|
| 110 |
-
# 2. MOTEUR DE TRAITEMENT (Universel)
|
| 111 |
-
# ==========================================
|
| 112 |
-
def process_audio_logic(inputs):
|
| 113 |
-
"""
|
| 114 |
-
inputs est une liste de tuples : (type, content, person_id)
|
| 115 |
-
type: 'url' ou 'file'
|
| 116 |
-
"""
|
| 117 |
track_1 = AudioSegment.empty()
|
| 118 |
track_2 = AudioSegment.empty()
|
| 119 |
full_mix = AudioSegment.empty()
|
| 120 |
crossfade = 100
|
| 121 |
cursor = 0
|
| 122 |
|
| 123 |
-
print(f"
|
| 124 |
|
| 125 |
-
for i,
|
| 126 |
try:
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
audio_data = BytesIO(r.content)
|
| 130 |
-
else: # src_type == 'file'
|
| 131 |
-
audio_data = content # C'est déjà un file object
|
| 132 |
-
|
| 133 |
chunk = AudioSegment.from_file(audio_data, format="mp3")
|
| 134 |
except Exception as e:
|
| 135 |
-
print(f"
|
| 136 |
continue
|
| 137 |
|
| 138 |
duration = len(chunk)
|
|
|
|
| 139 |
if i > 0: cursor -= crossfade
|
| 140 |
|
| 141 |
# Track 1
|
| 142 |
gap_1 = cursor - len(track_1)
|
| 143 |
if gap_1 > 0: track_1 += AudioSegment.silent(duration=gap_1)
|
| 144 |
-
if str(
|
| 145 |
|
| 146 |
# Track 2
|
| 147 |
gap_2 = cursor - len(track_2)
|
| 148 |
if gap_2 > 0: track_2 += AudioSegment.silent(duration=gap_2)
|
| 149 |
-
if str(
|
| 150 |
|
| 151 |
# Mix
|
| 152 |
if i == 0: full_mix += chunk
|
|
@@ -154,12 +44,10 @@ def process_audio_logic(inputs):
|
|
| 154 |
|
| 155 |
cursor += duration
|
| 156 |
|
| 157 |
-
# Equalize lengths
|
| 158 |
final_len = len(full_mix)
|
| 159 |
if len(track_1) < final_len: track_1 += AudioSegment.silent(duration=final_len - len(track_1))
|
| 160 |
if len(track_2) < final_len: track_2 += AudioSegment.silent(duration=final_len - len(track_2))
|
| 161 |
|
| 162 |
-
# ZIP Export
|
| 163 |
memory_file = BytesIO()
|
| 164 |
with zipfile.ZipFile(memory_file, 'w') as zf:
|
| 165 |
for name, audio in [("speaker_1.mp3", track_1), ("speaker_2.mp3", track_2), ("mix.mp3", full_mix)]:
|
|
@@ -170,43 +58,17 @@ def process_audio_logic(inputs):
|
|
| 170 |
memory_file.seek(0)
|
| 171 |
return memory_file
|
| 172 |
|
| 173 |
-
# ==========================================
|
| 174 |
-
# 3. ROUTES DU SERVEUR
|
| 175 |
-
# ==========================================
|
| 176 |
-
|
| 177 |
-
# Route A : L'interface Web (AI Studio)
|
| 178 |
@app.route('/')
|
| 179 |
def home():
|
| 180 |
-
return
|
| 181 |
|
| 182 |
-
# Route B : Traitement du Formulaire Web
|
| 183 |
-
@app.route('/web-generate', methods=['POST'])
|
| 184 |
-
def web_generate():
|
| 185 |
-
files = request.files.getlist('files[]')
|
| 186 |
-
speakers = request.form.getlist('speakers[]')
|
| 187 |
-
|
| 188 |
-
# On prépare les données pour le moteur
|
| 189 |
-
# inputs format: list of ('file', file_object, person_id)
|
| 190 |
-
inputs = []
|
| 191 |
-
for f, s in zip(files, speakers):
|
| 192 |
-
inputs.append(('file', f, s))
|
| 193 |
-
|
| 194 |
-
zip_buffer = process_audio_logic(inputs)
|
| 195 |
-
return send_file(zip_buffer, mimetype='application/zip', as_attachment=True, download_name='podcast_pack.zip')
|
| 196 |
-
|
| 197 |
-
# Route C : API pour n8n (Reste inchangée pour tes workflows)
|
| 198 |
@app.route('/generate', methods=['POST'])
|
| 199 |
-
def
|
| 200 |
data = request.json
|
| 201 |
segments = data.get('segments', [])
|
| 202 |
-
|
| 203 |
-
# inputs format: list of ('url', url_string, person_id)
|
| 204 |
-
inputs = []
|
| 205 |
-
for seg in segments:
|
| 206 |
-
inputs.append(('url', seg['url'], seg['person']))
|
| 207 |
-
|
| 208 |
try:
|
| 209 |
-
zip_buffer =
|
| 210 |
return send_file(zip_buffer, mimetype='application/zip', as_attachment=True, download_name='podcast_pack.zip')
|
| 211 |
except Exception as e:
|
| 212 |
return {"error": str(e)}, 500
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file
|
| 2 |
from pydub import AudioSegment
|
| 3 |
from io import BytesIO
|
| 4 |
import requests
|
| 5 |
import zipfile
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
def process_audio(segments):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
track_1 = AudioSegment.empty()
|
| 11 |
track_2 = AudioSegment.empty()
|
| 12 |
full_mix = AudioSegment.empty()
|
| 13 |
crossfade = 100
|
| 14 |
cursor = 0
|
| 15 |
|
| 16 |
+
print(f"Traitement de {len(segments)} segments...")
|
| 17 |
|
| 18 |
+
for i, seg in enumerate(segments):
|
| 19 |
try:
|
| 20 |
+
r = requests.get(seg['url'])
|
| 21 |
+
audio_data = BytesIO(r.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
chunk = AudioSegment.from_file(audio_data, format="mp3")
|
| 23 |
except Exception as e:
|
| 24 |
+
print(f"Erreur téléchargement segment {i}: {e}")
|
| 25 |
continue
|
| 26 |
|
| 27 |
duration = len(chunk)
|
| 28 |
+
|
| 29 |
if i > 0: cursor -= crossfade
|
| 30 |
|
| 31 |
# Track 1
|
| 32 |
gap_1 = cursor - len(track_1)
|
| 33 |
if gap_1 > 0: track_1 += AudioSegment.silent(duration=gap_1)
|
| 34 |
+
if str(seg['person']) == "1": track_1 += chunk
|
| 35 |
|
| 36 |
# Track 2
|
| 37 |
gap_2 = cursor - len(track_2)
|
| 38 |
if gap_2 > 0: track_2 += AudioSegment.silent(duration=gap_2)
|
| 39 |
+
if str(seg['person']) == "2": track_2 += chunk
|
| 40 |
|
| 41 |
# Mix
|
| 42 |
if i == 0: full_mix += chunk
|
|
|
|
| 44 |
|
| 45 |
cursor += duration
|
| 46 |
|
|
|
|
| 47 |
final_len = len(full_mix)
|
| 48 |
if len(track_1) < final_len: track_1 += AudioSegment.silent(duration=final_len - len(track_1))
|
| 49 |
if len(track_2) < final_len: track_2 += AudioSegment.silent(duration=final_len - len(track_2))
|
| 50 |
|
|
|
|
| 51 |
memory_file = BytesIO()
|
| 52 |
with zipfile.ZipFile(memory_file, 'w') as zf:
|
| 53 |
for name, audio in [("speaker_1.mp3", track_1), ("speaker_2.mp3", track_2), ("mix.mp3", full_mix)]:
|
|
|
|
| 58 |
memory_file.seek(0)
|
| 59 |
return memory_file
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@app.route('/')
|
| 62 |
def home():
|
| 63 |
+
return "PodGen API is running! Send POST requests to /generate"
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
@app.route('/generate', methods=['POST'])
|
| 66 |
+
def generate():
|
| 67 |
data = request.json
|
| 68 |
segments = data.get('segments', [])
|
| 69 |
+
if not segments: return {"error": "No segments provided"}, 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
try:
|
| 71 |
+
zip_buffer = process_audio(segments)
|
| 72 |
return send_file(zip_buffer, mimetype='application/zip', as_attachment=True, download_name='podcast_pack.zip')
|
| 73 |
except Exception as e:
|
| 74 |
return {"error": str(e)}, 500
|