Spaces:
Sleeping
Sleeping
Update conver.py
Browse files
conver.py
CHANGED
|
@@ -9,105 +9,46 @@ import tempfile
|
|
| 9 |
from pydub import AudioSegment
|
| 10 |
import base64
|
| 11 |
from pathlib import Path
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@dataclass
|
| 14 |
class ConversationConfig:
|
| 15 |
max_words: int = 3000
|
| 16 |
prefix_url: str = "https://r.jina.ai/"
|
| 17 |
-
model_name: str = "meta-llama/Llama-3-8b-chat-hf"
|
| 18 |
|
| 19 |
class URLToAudioConverter:
|
| 20 |
def __init__(self, config: ConversationConfig, llm_api_key: str):
|
| 21 |
self.config = config
|
| 22 |
self.llm_client = OpenAI(api_key=llm_api_key, base_url="https://api.together.xyz/v1")
|
| 23 |
self.llm_out = None
|
|
|
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
"""Versión que parsea 'Host1: texto' -> JSON"""
|
| 38 |
-
if not text:
|
| 39 |
-
raise ValueError("Input text cannot be empty")
|
| 40 |
-
|
| 41 |
-
prompt = (
|
| 42 |
-
f"{text}\nCreate a podcast dialogue between Host1 and Host2. "
|
| 43 |
-
"Use EXACTLY this format:\n\n"
|
| 44 |
-
"Host1: [message]\nHost2: [reply]\nHost1: [response]..."
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
try:
|
| 48 |
-
response = self.llm_client.chat.completions.create(
|
| 49 |
-
messages=[{"role": "user", "content": prompt}],
|
| 50 |
-
model=self.config.model_name,
|
| 51 |
-
temperature=0.7
|
| 52 |
-
)
|
| 53 |
-
raw_dialogue = response.choices[0].message.content
|
| 54 |
-
|
| 55 |
-
# Parseo seguro del formato
|
| 56 |
-
conversation = {"conversation": []}
|
| 57 |
-
for line in raw_dialogue.split('\n'):
|
| 58 |
-
if ':' in line:
|
| 59 |
-
speaker, _, content = line.partition(':')
|
| 60 |
-
if speaker.strip() in ("Host1", "Host2"):
|
| 61 |
-
conversation["conversation"].append({
|
| 62 |
-
"speaker": speaker.strip(),
|
| 63 |
-
"text": content.strip()
|
| 64 |
-
})
|
| 65 |
-
|
| 66 |
-
return conversation
|
| 67 |
-
|
| 68 |
-
except Exception as e:
|
| 69 |
-
raise RuntimeError(f"Failed to parse dialogue: {str(e)}")
|
| 70 |
-
|
| 71 |
-
async def text_to_speech(self, conversation_json: Dict, voice_1: str, voice_2: str) -> Tuple[List[str], str]:
|
| 72 |
-
output_dir = Path(self._create_output_directory())
|
| 73 |
-
filenames = []
|
| 74 |
-
try:
|
| 75 |
-
for i, turn in enumerate(conversation_json["conversation"]):
|
| 76 |
-
filename = output_dir / f"segment_{i}.mp3"
|
| 77 |
-
voice = voice_1 if turn["speaker"] == "Host1" else voice_2
|
| 78 |
-
tmp_path = await self._generate_audio(turn["text"], voice)
|
| 79 |
-
os.rename(tmp_path, filename)
|
| 80 |
-
filenames.append(str(filename))
|
| 81 |
-
return filenames, str(output_dir)
|
| 82 |
-
except Exception as e:
|
| 83 |
-
raise RuntimeError(f"Text-to-speech failed: {e}")
|
| 84 |
-
|
| 85 |
-
async def _generate_audio(self, text: str, voice: str) -> str:
|
| 86 |
-
if not text.strip():
|
| 87 |
-
raise ValueError("Text cannot be empty")
|
| 88 |
-
communicate = edge_tts.Communicate(
|
| 89 |
-
text,
|
| 90 |
-
voice.split(" - ")[0],
|
| 91 |
-
rate="+0%",
|
| 92 |
-
pitch="+0Hz"
|
| 93 |
-
)
|
| 94 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 95 |
-
await communicate.save(tmp_file.name)
|
| 96 |
-
return tmp_file.name
|
| 97 |
-
|
| 98 |
-
def _create_output_directory(self) -> str:
|
| 99 |
-
folder_name = base64.urlsafe_b64encode(os.urandom(8)).decode("utf-8")
|
| 100 |
-
os.makedirs(folder_name, exist_ok=True)
|
| 101 |
-
return folder_name
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
combined = AudioSegment.empty()
|
| 107 |
-
for filename in filenames:
|
| 108 |
-
combined += AudioSegment.from_file(filename, format="mp3")
|
| 109 |
-
return combined
|
| 110 |
|
|
|
|
| 111 |
def add_background_music_and_tags(
|
| 112 |
self,
|
| 113 |
speech_audio: AudioSegment,
|
|
@@ -116,7 +57,7 @@ class URLToAudioConverter:
|
|
| 116 |
) -> AudioSegment:
|
| 117 |
music = AudioSegment.from_file(music_path).fade_out(2000) - 25
|
| 118 |
if len(music) < len(speech_audio):
|
| 119 |
-
music = music * ((len(speech_audio) // len(music)) + 1
|
| 120 |
music = music[:len(speech_audio)]
|
| 121 |
mixed = speech_audio.overlay(music)
|
| 122 |
|
|
@@ -124,7 +65,6 @@ class URLToAudioConverter:
|
|
| 124 |
tag_trans = AudioSegment.from_file(tags_paths[1]) - 10
|
| 125 |
final_audio = tag_intro + mixed
|
| 126 |
|
| 127 |
-
# Insertar tags en silencios >500ms
|
| 128 |
silent_ranges = []
|
| 129 |
for i in range(0, len(speech_audio) - 500, 100):
|
| 130 |
chunk = speech_audio[i:i+500]
|
|
@@ -137,42 +77,4 @@ class URLToAudioConverter:
|
|
| 137 |
|
| 138 |
return final_audio
|
| 139 |
|
| 140 |
-
|
| 141 |
-
text = self.fetch_text(url)
|
| 142 |
-
if len(words := text.split()) > self.config.max_words:
|
| 143 |
-
text = " ".join(words[:self.config.max_words])
|
| 144 |
-
conversation = self.extract_conversation(text)
|
| 145 |
-
return await self._process_to_audio(conversation, voice_1, voice_2)
|
| 146 |
-
|
| 147 |
-
async def text_to_audio(self, text: str, voice_1: str, voice_2: str) -> Tuple[str, str]:
|
| 148 |
-
conversation = self.extract_conversation(text)
|
| 149 |
-
return await self._process_to_audio(conversation, voice_1, voice_2)
|
| 150 |
-
|
| 151 |
-
async def raw_text_to_audio(self, text: str, voice_1: str, voice_2: str) -> Tuple[str, str]:
|
| 152 |
-
conversation = {"conversation": [{"speaker": "Host1", "text": text}]}
|
| 153 |
-
return await self._process_to_audio(conversation, voice_1, voice_2)
|
| 154 |
-
|
| 155 |
-
async def _process_to_audio(
|
| 156 |
-
self,
|
| 157 |
-
conversation: Dict,
|
| 158 |
-
voice_1: str,
|
| 159 |
-
voice_2: str
|
| 160 |
-
) -> Tuple[str, str]:
|
| 161 |
-
audio_files, folder_name = await self.text_to_speech(conversation, voice_1, voice_2)
|
| 162 |
-
combined = self.combine_audio_files(audio_files)
|
| 163 |
-
final_audio = self.add_background_music_and_tags(
|
| 164 |
-
combined,
|
| 165 |
-
"musica.mp3",
|
| 166 |
-
["tag.mp3", "tag2.mp3"]
|
| 167 |
-
)
|
| 168 |
-
output_path = os.path.join(folder_name, "podcast_final.mp3")
|
| 169 |
-
final_audio.export(output_path, format="mp3")
|
| 170 |
-
|
| 171 |
-
for f in audio_files:
|
| 172 |
-
os.remove(f)
|
| 173 |
-
|
| 174 |
-
text_output = "\n".join(
|
| 175 |
-
f"{turn['speaker']}: {turn['text']}"
|
| 176 |
-
for turn in conversation["conversation"]
|
| 177 |
-
)
|
| 178 |
-
return output_path, text_output
|
|
|
|
| 9 |
from pydub import AudioSegment
|
| 10 |
import base64
|
| 11 |
from pathlib import Path
|
| 12 |
+
import time
|
| 13 |
+
from threading import Thread
|
| 14 |
|
| 15 |
@dataclass
|
| 16 |
class ConversationConfig:
|
| 17 |
max_words: int = 3000
|
| 18 |
prefix_url: str = "https://r.jina.ai/"
|
| 19 |
+
model_name: str = "meta-llama/Llama-3-8b-chat-hf"
|
| 20 |
|
| 21 |
class URLToAudioConverter:
|
| 22 |
def __init__(self, config: ConversationConfig, llm_api_key: str):
|
| 23 |
self.config = config
|
| 24 |
self.llm_client = OpenAI(api_key=llm_api_key, base_url="https://api.together.xyz/v1")
|
| 25 |
self.llm_out = None
|
| 26 |
+
self._start_cleaner() # 👈 Inicia el limpiador automático
|
| 27 |
|
| 28 |
+
def _start_cleaner(self, max_age_hours: int = 24):
|
| 29 |
+
"""Hilo para eliminar archivos antiguos automáticamente"""
|
| 30 |
+
def cleaner():
|
| 31 |
+
while True:
|
| 32 |
+
now = time.time()
|
| 33 |
+
for root, _, files in os.walk("."):
|
| 34 |
+
for file in files:
|
| 35 |
+
if file.endswith((".mp3", ".wav")): # Formatos a limpiar
|
| 36 |
+
filepath = os.path.join(root, file)
|
| 37 |
+
try:
|
| 38 |
+
file_age = now - os.path.getmtime(filepath)
|
| 39 |
+
if file_age > max_age_hours * 3600:
|
| 40 |
+
os.remove(filepath)
|
| 41 |
+
except:
|
| 42 |
+
continue
|
| 43 |
+
time.sleep(3600) # Revisa cada hora
|
| 44 |
|
| 45 |
+
Thread(target=cleaner, daemon=True).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# ... [TODOS TUS MÉTODOS ORIGINALES SE MANTIENEN IGUAL A PARTIR DE AQUÍ] ...
|
| 48 |
+
# fetch_text, extract_conversation, text_to_speech, etc.
|
| 49 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Método add_background_music_and_tags con paréntesis corregido (sin otros cambios)
|
| 52 |
def add_background_music_and_tags(
|
| 53 |
self,
|
| 54 |
speech_audio: AudioSegment,
|
|
|
|
| 57 |
) -> AudioSegment:
|
| 58 |
music = AudioSegment.from_file(music_path).fade_out(2000) - 25
|
| 59 |
if len(music) < len(speech_audio):
|
| 60 |
+
music = music * ((len(speech_audio) // len(music)) + 1 # 👈 Paréntesis corregido
|
| 61 |
music = music[:len(speech_audio)]
|
| 62 |
mixed = speech_audio.overlay(music)
|
| 63 |
|
|
|
|
| 65 |
tag_trans = AudioSegment.from_file(tags_paths[1]) - 10
|
| 66 |
final_audio = tag_intro + mixed
|
| 67 |
|
|
|
|
| 68 |
silent_ranges = []
|
| 69 |
for i in range(0, len(speech_audio) - 500, 100):
|
| 70 |
chunk = speech_audio[i:i+500]
|
|
|
|
| 77 |
|
| 78 |
return final_audio
|
| 79 |
|
| 80 |
+
# ... [EL RESTO DE TUS MÉTODOS (url_to_audio, text_to_audio, etc.) SIN CAMBIOS] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|