Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain.prompts import PromptTemplate
|
| 3 |
-
from
|
| 4 |
from transformers import pipeline
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import requests
|
|
@@ -21,11 +21,12 @@ Summarize the following article content in a clear, warm, and motivational tone
|
|
| 21 |
Summary:
|
| 22 |
""")
|
| 23 |
|
| 24 |
-
# Updated chaining method
|
| 25 |
summary_chain = summary_prompt | llm
|
| 26 |
|
| 27 |
# TTS model setup
|
| 28 |
tts_model = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def extract_main_content(url):
|
| 31 |
try:
|
|
@@ -39,13 +40,13 @@ def extract_main_content(url):
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error extracting article content: {str(e)}"
|
| 41 |
|
| 42 |
-
def generate_human_like_audio(text):
|
| 43 |
try:
|
| 44 |
temp_dir = tempfile.mkdtemp()
|
| 45 |
wav_path = os.path.join(temp_dir, "summary.wav")
|
| 46 |
mp3_path = os.path.join(temp_dir, "summary.mp3")
|
| 47 |
|
| 48 |
-
tts_model.tts_to_file(text=text, file_path=wav_path)
|
| 49 |
|
| 50 |
os.system(f"ffmpeg -y -i {wav_path} -codec:a libmp3lame -qscale:a 4 {mp3_path}")
|
| 51 |
|
|
@@ -57,7 +58,7 @@ def generate_human_like_audio(text):
|
|
| 57 |
print(f"TTS ERROR: {e}")
|
| 58 |
return None, None
|
| 59 |
|
| 60 |
-
def url_to_audio_summary(url):
|
| 61 |
try:
|
| 62 |
article_text = extract_main_content(url)
|
| 63 |
if article_text.startswith("Error"):
|
|
@@ -69,13 +70,13 @@ def url_to_audio_summary(url):
|
|
| 69 |
summary = summary_chain.invoke({"text": article_text})
|
| 70 |
summary = summary["text"] if isinstance(summary, dict) and "text" in summary else summary
|
| 71 |
|
| 72 |
-
wav_path, mp3_path = generate_human_like_audio(summary)
|
| 73 |
return summary, wav_path, mp3_path
|
| 74 |
except Exception as e:
|
| 75 |
return f"Error: {str(e)}", None, None
|
| 76 |
|
| 77 |
-
def interface_wrapper(url):
|
| 78 |
-
summary, wav_path, mp3_path = url_to_audio_summary(url)
|
| 79 |
download_html = ""
|
| 80 |
if mp3_path and os.path.exists(mp3_path):
|
| 81 |
download_html = f'<a href="file/{os.path.basename(mp3_path)}" download target="_blank">Click to download MP3</a>'
|
|
@@ -83,7 +84,10 @@ def interface_wrapper(url):
|
|
| 83 |
|
| 84 |
iface = gr.Interface(
|
| 85 |
fn=interface_wrapper,
|
| 86 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 87 |
outputs=[
|
| 88 |
gr.Textbox(label="Summary"),
|
| 89 |
gr.Audio(label="Preacher-style Audio Summary", type="filepath"),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain.prompts import PromptTemplate
|
| 3 |
+
from langchain_huggingface import HuggingFacePipeline
|
| 4 |
from transformers import pipeline
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import requests
|
|
|
|
| 21 |
Summary:
|
| 22 |
""")
|
| 23 |
|
|
|
|
| 24 |
summary_chain = summary_prompt | llm
|
| 25 |
|
| 26 |
# TTS model setup
|
| 27 |
tts_model = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
|
| 28 |
+
SPEAKER_LIST = tts_model.speakers
|
| 29 |
+
DEFAULT_SPEAKER = SPEAKER_LIST[0] if SPEAKER_LIST else None
|
| 30 |
|
| 31 |
def extract_main_content(url):
|
| 32 |
try:
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error extracting article content: {str(e)}"
|
| 42 |
|
| 43 |
+
def generate_human_like_audio(text, speaker):
|
| 44 |
try:
|
| 45 |
temp_dir = tempfile.mkdtemp()
|
| 46 |
wav_path = os.path.join(temp_dir, "summary.wav")
|
| 47 |
mp3_path = os.path.join(temp_dir, "summary.mp3")
|
| 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 |
|
|
|
|
| 58 |
print(f"TTS ERROR: {e}")
|
| 59 |
return None, None
|
| 60 |
|
| 61 |
+
def url_to_audio_summary(url, speaker):
|
| 62 |
try:
|
| 63 |
article_text = extract_main_content(url)
|
| 64 |
if article_text.startswith("Error"):
|
|
|
|
| 70 |
summary = summary_chain.invoke({"text": article_text})
|
| 71 |
summary = summary["text"] if isinstance(summary, dict) and "text" in summary else summary
|
| 72 |
|
| 73 |
+
wav_path, mp3_path = generate_human_like_audio(summary, speaker)
|
| 74 |
return summary, wav_path, mp3_path
|
| 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 |
download_html = ""
|
| 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>'
|
|
|
|
| 84 |
|
| 85 |
iface = gr.Interface(
|
| 86 |
fn=interface_wrapper,
|
| 87 |
+
inputs=[
|
| 88 |
+
gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
|
| 89 |
+
gr.Dropdown(choices=SPEAKER_LIST, value=DEFAULT_SPEAKER, label="Select Speaker")
|
| 90 |
+
],
|
| 91 |
outputs=[
|
| 92 |
gr.Textbox(label="Summary"),
|
| 93 |
gr.Audio(label="Preacher-style Audio Summary", type="filepath"),
|