RohitCSharp commited on
Commit
556278e
·
verified ·
1 Parent(s): fc21b43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -7,6 +7,8 @@ from bs4 import BeautifulSoup
7
  import requests
8
  from TTS.api import TTS
9
  import tempfile
 
 
10
 
11
  # Setup summarization LLM
12
  summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
@@ -23,17 +25,15 @@ Summary:
23
 
24
  summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
25
 
26
- # TTS model setup (multi-lingual, expressive)
27
  tts_model = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
28
 
29
  def extract_main_content(url):
30
  try:
31
  response = requests.get(url, timeout=10)
32
  soup = BeautifulSoup(response.content, "html.parser")
33
-
34
  for tag in soup(["nav", "header", "footer", "aside", "script", "style", "noscript"]):
35
  tag.decompose()
36
-
37
  paragraphs = soup.find_all("p")
38
  content = "\n".join([p.get_text() for p in paragraphs if len(p.get_text()) > 60])
39
  return content.strip()
@@ -42,39 +42,54 @@ def extract_main_content(url):
42
 
43
  def generate_human_like_audio(text):
44
  try:
45
- temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
46
- tts_model.tts_to_file(text=text, file_path=temp_path.name)
47
- return temp_path.name
 
 
 
 
 
 
 
 
 
 
48
  except Exception as e:
49
- return None
 
50
 
51
  def url_to_audio_summary(url):
52
  try:
53
  article_text = extract_main_content(url)
54
  if article_text.startswith("Error"):
55
- return article_text, None
56
 
57
- # Truncate for model's 512-token limit
58
  if len(article_text) > 1500:
59
  article_text = article_text[:1500] + "..."
60
-
61
  summary = summary_chain.invoke({"text": article_text})
62
  summary = summary["text"] if isinstance(summary, dict) and "text" in summary else summary
63
 
64
- audio_path = generate_human_like_audio(summary)
65
- if not audio_path:
66
- return summary, None
67
-
68
- return summary, audio_path
69
  except Exception as e:
70
- return f"Error: {str(e)}", None
 
 
 
 
 
 
 
71
 
72
  iface = gr.Interface(
73
- fn=url_to_audio_summary,
74
  inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
75
  outputs=[
76
  gr.Textbox(label="Summary"),
77
- gr.Audio(label="Preacher-style Audio Summary")
 
78
  ],
79
  title="Preaching-Style URL to Audio Agent",
80
  description="Summarizes article content and reads it aloud in a warm, preacher-style voice using YourTTS. CPU-only."
 
7
  import requests
8
  from TTS.api import TTS
9
  import tempfile
10
+ import os
11
+ import shutil
12
 
13
  # Setup summarization LLM
14
  summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
 
25
 
26
  summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
27
 
28
+ # TTS model setup
29
  tts_model = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
30
 
31
  def extract_main_content(url):
32
  try:
33
  response = requests.get(url, timeout=10)
34
  soup = BeautifulSoup(response.content, "html.parser")
 
35
  for tag in soup(["nav", "header", "footer", "aside", "script", "style", "noscript"]):
36
  tag.decompose()
 
37
  paragraphs = soup.find_all("p")
38
  content = "\n".join([p.get_text() for p in paragraphs if len(p.get_text()) > 60])
39
  return content.strip()
 
42
 
43
  def generate_human_like_audio(text):
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, file_path=wav_path)
50
+
51
+ # Convert to mp3 for download link (requires ffmpeg in Hugging Face Spaces)
52
+ os.system(f"ffmpeg -y -i {wav_path} -codec:a libmp3lame -qscale:a 4 {mp3_path}")
53
+
54
+ if os.path.exists(mp3_path):
55
+ return wav_path, mp3_path
56
+ else:
57
+ return wav_path, None
58
  except Exception as e:
59
+ print(f"TTS ERROR: {e}")
60
+ return None, None
61
 
62
  def url_to_audio_summary(url):
63
  try:
64
  article_text = extract_main_content(url)
65
  if article_text.startswith("Error"):
66
+ return article_text, None, None
67
 
 
68
  if len(article_text) > 1500:
69
  article_text = article_text[:1500] + "..."
70
+
71
  summary = summary_chain.invoke({"text": article_text})
72
  summary = summary["text"] if isinstance(summary, dict) and "text" in summary else summary
73
 
74
+ wav_path, mp3_path = generate_human_like_audio(summary)
75
+ return summary, wav_path, mp3_path
 
 
 
76
  except Exception as e:
77
+ return f"Error: {str(e)}", None, None
78
+
79
+ def interface_wrapper(url):
80
+ summary, wav_path, mp3_path = url_to_audio_summary(url)
81
+ download_html = ""
82
+ if mp3_path and os.path.exists(mp3_path):
83
+ download_html = f'<a href="file/{os.path.basename(mp3_path)}" download target="_blank">Click to download MP3</a>'
84
+ return summary, wav_path, download_html
85
 
86
  iface = gr.Interface(
87
+ fn=interface_wrapper,
88
  inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
89
  outputs=[
90
  gr.Textbox(label="Summary"),
91
+ gr.Audio(label="Preacher-style Audio Summary", type="filepath"),
92
+ gr.HTML(label="Download MP3")
93
  ],
94
  title="Preaching-Style URL to Audio Agent",
95
  description="Summarizes article content and reads it aloud in a warm, preacher-style voice using YourTTS. CPU-only."