Spaces:
Running
Running
Update conver.py
Browse files
conver.py
CHANGED
@@ -65,7 +65,7 @@ class URLToAudioConverter:
|
|
65 |
prompt = self.config.custom_prompt_template.format(text=text) if self.config.custom_prompt_template else (
|
66 |
f"{text}\nConvierte el texto en un diálogo de podcast en español entre Anfitrión1 y Anfitrión2. "
|
67 |
f"Genera una conversación extensa y natural con al menos 5 intercambios por hablante. "
|
68 |
-
f"Devuelve SOLO un objeto JSON: "
|
69 |
f'{{"conversation": [{{"speaker": "Anfitrión1", "text": "..."}}, {{"speaker": "Anfitrión2", "text": "..."}}]}}'
|
70 |
)
|
71 |
response = self.llm_client.chat.completions.create(
|
@@ -74,27 +74,33 @@ class URLToAudioConverter:
|
|
74 |
response_format={"type": "json_object"}
|
75 |
)
|
76 |
response_content = response.choices[0].message.content
|
77 |
-
# Clean response to
|
78 |
response_content = response_content.strip()
|
79 |
-
#
|
80 |
-
|
81 |
-
|
|
|
82 |
raise ValueError("No valid JSON object found in response")
|
83 |
-
json_str =
|
84 |
-
# Clean problematic characters and fix
|
85 |
json_str = re.sub(r',\s*([\]}])', r'\1', json_str) # Remove trailing commas
|
86 |
json_str = re.sub(r'\s+', ' ', json_str) # Replace multiple spaces
|
87 |
json_str = json_str.replace('\\"', '"').replace('"{', '{').replace('}"', '}')
|
|
|
88 |
try:
|
89 |
dialogue = json.loads(json_str)
|
90 |
except json.JSONDecodeError as e:
|
91 |
-
# Attempt to fix
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
98 |
raise ValueError("No valid conversation generated")
|
99 |
return dialogue
|
100 |
except Exception as e:
|
@@ -202,7 +208,7 @@ class URLToAudioConverter:
|
|
202 |
voice_2: str,
|
203 |
custom_music_path: str = None
|
204 |
) -> Tuple[str, str]:
|
205 |
-
audio_files, folder_name = await self.text_to_speech(
|
206 |
combined = self.combine_audio_files(audio_files)
|
207 |
final_audio = self.add_background_music_and_tags(
|
208 |
combined,
|
|
|
65 |
prompt = self.config.custom_prompt_template.format(text=text) if self.config.custom_prompt_template else (
|
66 |
f"{text}\nConvierte el texto en un diálogo de podcast en español entre Anfitrión1 y Anfitrión2. "
|
67 |
f"Genera una conversación extensa y natural con al menos 5 intercambios por hablante. "
|
68 |
+
f"Devuelve SOLO un objeto JSON con la estructura: "
|
69 |
f'{{"conversation": [{{"speaker": "Anfitrión1", "text": "..."}}, {{"speaker": "Anfitrión2", "text": "..."}}]}}'
|
70 |
)
|
71 |
response = self.llm_client.chat.completions.create(
|
|
|
74 |
response_format={"type": "json_object"}
|
75 |
)
|
76 |
response_content = response.choices[0].message.content
|
77 |
+
# Clean response to extract valid JSON
|
78 |
response_content = response_content.strip()
|
79 |
+
# Find the first valid JSON object
|
80 |
+
start_idx = response_content.find('{')
|
81 |
+
end_idx = response_content.rfind('}') + 1
|
82 |
+
if start_idx == -1 or end_idx == 0:
|
83 |
raise ValueError("No valid JSON object found in response")
|
84 |
+
json_str = response_content[start_idx:end_idx]
|
85 |
+
# Clean problematic characters and fix JSON issues
|
86 |
json_str = re.sub(r',\s*([\]}])', r'\1', json_str) # Remove trailing commas
|
87 |
json_str = re.sub(r'\s+', ' ', json_str) # Replace multiple spaces
|
88 |
json_str = json_str.replace('\\"', '"').replace('"{', '{').replace('}"', '}')
|
89 |
+
json_str = re.sub(r'(\w+):', r'"\1":', json_str) # Ensure keys are quoted
|
90 |
try:
|
91 |
dialogue = json.loads(json_str)
|
92 |
except json.JSONDecodeError as e:
|
93 |
+
# Attempt to fix by truncating to last valid array element
|
94 |
+
last_comma = json_str.rfind(',', 0, json_str.rfind(']'))
|
95 |
+
if last_comma != -1:
|
96 |
+
json_str = json_str[:last_comma] + json_str[json_str.rfind(']'):]
|
97 |
+
try:
|
98 |
+
dialogue = json.loads(json_str)
|
99 |
+
except json.JSONDecodeError as e2:
|
100 |
+
raise ValueError(f"JSON parsing failed: {str(e2)}")
|
101 |
+
else:
|
102 |
+
raise ValueError(f"JSON parsing failed: {str(e)}")
|
103 |
+
if not dialogue.get("conversation") or not isinstance(dialogue["conversation"], list):
|
104 |
raise ValueError("No valid conversation generated")
|
105 |
return dialogue
|
106 |
except Exception as e:
|
|
|
208 |
voice_2: str,
|
209 |
custom_music_path: str = None
|
210 |
) -> Tuple[str, str]:
|
211 |
+
audio_files, folder_name = await self.text_to_speech(con Scan to continue...versation, voice_1, voice_2)
|
212 |
combined = self.combine_audio_files(audio_files)
|
213 |
final_audio = self.add_background_music_and_tags(
|
214 |
combined,
|