Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,45 +42,37 @@ def extract_main_content(url):
|
|
| 42 |
|
| 43 |
def generate_human_like_audio(text, speaker):
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
tts_model.tts_to_file(text=text, speaker=speaker, file_path=wav_path)
|
| 50 |
-
|
| 51 |
os.system(f"ffmpeg -y -i {wav_path} -codec:a libmp3lame -qscale:a 4 {mp3_path}")
|
| 52 |
|
| 53 |
-
if os.path.exists(mp3_path)
|
| 54 |
-
return wav_path, mp3_path
|
| 55 |
-
else:
|
| 56 |
-
return wav_path, None
|
| 57 |
except Exception as e:
|
| 58 |
print(f"TTS ERROR: {e}")
|
| 59 |
return None, None
|
| 60 |
|
| 61 |
def url_to_audio_summary(url, speaker):
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
return article_text, None, None
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
except Exception as e:
|
| 76 |
-
return f"Error: {str(e)}", None, None
|
| 77 |
|
| 78 |
def interface_wrapper(url, speaker):
|
| 79 |
summary, wav_path, mp3_path = url_to_audio_summary(url, speaker)
|
| 80 |
-
|
| 81 |
-
if mp3_path and os.path.exists(mp3_path):
|
| 82 |
-
download_html = f'<a href="file/{os.path.basename(mp3_path)}" download target="_blank">Click to download MP3</a>'
|
| 83 |
-
return summary, wav_path, download_html
|
| 84 |
|
| 85 |
iface = gr.Interface(
|
| 86 |
fn=interface_wrapper,
|
|
@@ -91,7 +83,7 @@ iface = gr.Interface(
|
|
| 91 |
outputs=[
|
| 92 |
gr.Textbox(label="Summary"),
|
| 93 |
gr.Audio(label="Preacher-style Audio Summary", type="filepath"),
|
| 94 |
-
gr.
|
| 95 |
],
|
| 96 |
title="Preaching-Style URL to Audio Agent",
|
| 97 |
description="Summarizes article content and reads it aloud in a warm, preacher-style voice using YourTTS. CPU-only."
|
|
|
|
| 42 |
|
| 43 |
def generate_human_like_audio(text, speaker):
|
| 44 |
try:
|
| 45 |
+
# Save in tmp directory accessible by Gradio
|
| 46 |
+
base = tempfile.gettempdir()
|
| 47 |
+
wav_path = os.path.join(base, "summary.wav")
|
| 48 |
+
mp3_path = os.path.join(base, "summary.mp3")
|
| 49 |
|
| 50 |
tts_model.tts_to_file(text=text, speaker=speaker, file_path=wav_path)
|
|
|
|
| 51 |
os.system(f"ffmpeg -y -i {wav_path} -codec:a libmp3lame -qscale:a 4 {mp3_path}")
|
| 52 |
|
| 53 |
+
return wav_path if os.path.exists(wav_path) else None, mp3_path if os.path.exists(mp3_path) else None
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
print(f"TTS ERROR: {e}")
|
| 56 |
return None, None
|
| 57 |
|
| 58 |
def url_to_audio_summary(url, speaker):
|
| 59 |
+
article_text = extract_main_content(url)
|
| 60 |
+
if article_text.startswith("Error"):
|
| 61 |
+
return article_text, None, None
|
|
|
|
| 62 |
|
| 63 |
+
# Truncate
|
| 64 |
+
if len(article_text) > 1500:
|
| 65 |
+
article_text = article_text[:1500] + "..."
|
| 66 |
|
| 67 |
+
summary = (summary_chain.invoke if hasattr(summary_chain, 'invoke') else summary_chain)({"text": article_text})
|
| 68 |
+
summary = summary.get("text") if isinstance(summary, dict) else summary
|
| 69 |
|
| 70 |
+
wav_path, mp3_path = generate_human_like_audio(summary, speaker)
|
| 71 |
+
return summary, wav_path, mp3_path
|
|
|
|
|
|
|
| 72 |
|
| 73 |
def interface_wrapper(url, speaker):
|
| 74 |
summary, wav_path, mp3_path = url_to_audio_summary(url, speaker)
|
| 75 |
+
return summary, wav_path, mp3_path
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
iface = gr.Interface(
|
| 78 |
fn=interface_wrapper,
|
|
|
|
| 83 |
outputs=[
|
| 84 |
gr.Textbox(label="Summary"),
|
| 85 |
gr.Audio(label="Preacher-style Audio Summary", type="filepath"),
|
| 86 |
+
gr.File(label="Download MP3")
|
| 87 |
],
|
| 88 |
title="Preaching-Style URL to Audio Agent",
|
| 89 |
description="Summarizes article content and reads it aloud in a warm, preacher-style voice using YourTTS. CPU-only."
|