Spaces:
Running
Running
Update conver.py
Browse files
conver.py
CHANGED
@@ -11,6 +11,7 @@ import base64
|
|
11 |
from pathlib import Path
|
12 |
import time
|
13 |
from threading import Thread
|
|
|
14 |
|
15 |
@dataclass
|
16 |
class ConversationConfig:
|
@@ -25,6 +26,11 @@ class URLToAudioConverter:
|
|
25 |
self.llm_client = OpenAI(api_key=llm_api_key, base_url="https://api.together.xyz/v1")
|
26 |
self.llm_out = None
|
27 |
self._start_cleaner()
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def _start_cleaner(self, max_age_hours: int = 24):
|
30 |
def cleaner():
|
@@ -71,14 +77,17 @@ class URLToAudioConverter:
|
|
71 |
)
|
72 |
response_content = response.choices[0].message.content
|
73 |
print(f"Respuesta cruda del modelo: {response_content[:500]}...")
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
if
|
78 |
-
json_str =
|
|
|
|
|
|
|
79 |
dialogue = json.loads(json_str)
|
80 |
if not dialogue.get("conversation"):
|
81 |
-
print("Error: No
|
82 |
return dialogue
|
83 |
except Exception as e:
|
84 |
raise RuntimeError(f"Failed to parse dialogue: {str(e)}")
|
@@ -135,14 +144,26 @@ class URLToAudioConverter:
|
|
135 |
tags_paths: List[str],
|
136 |
custom_music_path: str = None
|
137 |
) -> AudioSegment:
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
music = AudioSegment.from_file(music_file).fade_out(2000) - 25
|
140 |
if len(music) < len(speech_audio):
|
141 |
music = music * ((len(speech_audio) // len(music)) + 1)
|
142 |
music = music[:len(speech_audio)]
|
143 |
mixed = speech_audio.overlay(music)
|
144 |
-
tag_outro = AudioSegment.from_file(
|
145 |
-
tag_trans = AudioSegment.from_file(
|
146 |
final_audio = mixed + tag_outro
|
147 |
silent_ranges = []
|
148 |
for i in range(0, len(speech_audio) - 500, 100):
|
@@ -180,8 +201,8 @@ class URLToAudioConverter:
|
|
180 |
combined = self.combine_audio_files(audio_files)
|
181 |
final_audio = self.add_background_music_and_tags(
|
182 |
combined,
|
183 |
-
|
184 |
-
[
|
185 |
custom_music_path
|
186 |
)
|
187 |
output_path = os.path.join(folder_name, "podcast_final.mp3")
|
@@ -192,5 +213,4 @@ class URLToAudioConverter:
|
|
192 |
f"{turn['speaker']}: {turn['text']}"
|
193 |
for turn in conversation["conversation"]
|
194 |
)
|
195 |
-
return output_path, text_output
|
196 |
-
|
|
|
11 |
from pathlib import Path
|
12 |
import time
|
13 |
from threading import Thread
|
14 |
+
import re
|
15 |
|
16 |
@dataclass
|
17 |
class ConversationConfig:
|
|
|
26 |
self.llm_client = OpenAI(api_key=llm_api_key, base_url="https://api.together.xyz/v1")
|
27 |
self.llm_out = None
|
28 |
self._start_cleaner()
|
29 |
+
# Define audio file paths relative to the script directory
|
30 |
+
self.ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
31 |
+
self.MUSICA_FONDO = os.path.join(self.ROOT_DIR, "musica.mp3")
|
32 |
+
self.TAG1 = os.path.join(self.ROOT_DIR, "tag.mp3")
|
33 |
+
self.TAG2 = os.path.join(self.ROOT_DIR, "tag2.mp3")
|
34 |
|
35 |
def _start_cleaner(self, max_age_hours: int = 24):
|
36 |
def cleaner():
|
|
|
77 |
)
|
78 |
response_content = response.choices[0].message.content
|
79 |
print(f"Respuesta cruda del modelo: {response_content[:500]}...")
|
80 |
+
|
81 |
+
# Extract valid JSON using regex
|
82 |
+
json_match = re.search(r'\{.*\}', response_content, re.DOTALL)
|
83 |
+
if json_match:
|
84 |
+
json_str = json_match.group(0)
|
85 |
+
else:
|
86 |
+
raise ValueError("No valid JSON object found in response")
|
87 |
+
|
88 |
dialogue = json.loads(json_str)
|
89 |
if not dialogue.get("conversation"):
|
90 |
+
print("Error: No valid conversation generated.")
|
91 |
return dialogue
|
92 |
except Exception as e:
|
93 |
raise RuntimeError(f"Failed to parse dialogue: {str(e)}")
|
|
|
144 |
tags_paths: List[str],
|
145 |
custom_music_path: str = None
|
146 |
) -> AudioSegment:
|
147 |
+
# Use predefined paths if no custom path is provided
|
148 |
+
music_file = custom_music_path if custom_music_path and os.path.exists(custom_music_path) else self.MUSICA_FONDO
|
149 |
+
tag_outro_file = self.TAG1
|
150 |
+
tag_trans_file = self.TAG2
|
151 |
+
|
152 |
+
# Check if files exist
|
153 |
+
if not os.path.exists(music_file):
|
154 |
+
raise FileNotFoundError(f"Music file not found: {music_file}")
|
155 |
+
if not os.path.exists(tag_outro_file):
|
156 |
+
raise FileNotFoundError(f"Tag file not found: {tag_outro_file}")
|
157 |
+
if not os.path.exists(tag_trans_file):
|
158 |
+
raise FileNotFoundError(f"Tag file not found: {tag_trans_file}")
|
159 |
+
|
160 |
music = AudioSegment.from_file(music_file).fade_out(2000) - 25
|
161 |
if len(music) < len(speech_audio):
|
162 |
music = music * ((len(speech_audio) // len(music)) + 1)
|
163 |
music = music[:len(speech_audio)]
|
164 |
mixed = speech_audio.overlay(music)
|
165 |
+
tag_outro = AudioSegment.from_file(tag_outro_file) - 10
|
166 |
+
tag_trans = AudioSegment.from_file(tag_trans_file) - 10
|
167 |
final_audio = mixed + tag_outro
|
168 |
silent_ranges = []
|
169 |
for i in range(0, len(speech_audio) - 500, 100):
|
|
|
201 |
combined = self.combine_audio_files(audio_files)
|
202 |
final_audio = self.add_background_music_and_tags(
|
203 |
combined,
|
204 |
+
self.MUSICA_FONDO, # Use instance variable
|
205 |
+
[self.TAG1, self.TAG2], # Use instance variables
|
206 |
custom_music_path
|
207 |
)
|
208 |
output_path = os.path.join(folder_name, "podcast_final.mp3")
|
|
|
213 |
f"{turn['speaker']}: {turn['text']}"
|
214 |
for turn in conversation["conversation"]
|
215 |
)
|
216 |
+
return output_path, text_output
|
|