RohitCSharp commited on
Commit
83f07d9
·
verified ·
1 Parent(s): 96f0863

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -27
app.py CHANGED
@@ -1,23 +1,20 @@
1
  import gradio as gr
2
  from langchain.chains import LLMChain
3
  from langchain.prompts import PromptTemplate
4
- from langchain.document_loaders import WebBaseLoader
5
- from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain.llms import HuggingFacePipeline
7
  from transformers import pipeline
8
- import tempfile
9
- import os
10
  from bs4 import BeautifulSoup
11
  import requests
12
- import pyttsx3
 
13
 
14
- # CPU-friendly summarization LLM
15
  summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
16
  llm = HuggingFacePipeline(pipeline=summary_pipe)
17
 
18
- # Summarization prompt
19
  summary_prompt = PromptTemplate.from_template("""
20
- Summarize the following article content in a clear, concise, and emotionally engaging manner as if you're speaking to a curious listener:
21
 
22
  {text}
23
 
@@ -26,6 +23,9 @@ Summary:
26
 
27
  summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
28
 
 
 
 
29
  def extract_main_content(url):
30
  try:
31
  response = requests.get(url, timeout=10)
@@ -42,21 +42,8 @@ def extract_main_content(url):
42
 
43
  def generate_human_like_audio(text):
44
  try:
45
- engine = pyttsx3.init()
46
- engine.setProperty('rate', 150) # slower pace
47
- engine.setProperty('volume', 1.0)
48
- voices = engine.getProperty('voices')
49
-
50
- # Choose a more natural voice if available (optional: pick female)
51
- for voice in voices:
52
- if 'female' in voice.name.lower():
53
- engine.setProperty('voice', voice.id)
54
- break
55
-
56
- temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
57
- engine.save_to_file(text, temp_path.name)
58
- engine.runAndWait()
59
-
60
  return temp_path.name
61
  except Exception as e:
62
  return None
@@ -74,7 +61,6 @@ def url_to_audio_summary(url):
74
  return summary, None
75
 
76
  return summary, audio_path
77
-
78
  except Exception as e:
79
  return f"Error: {str(e)}", None
80
 
@@ -83,10 +69,10 @@ iface = gr.Interface(
83
  inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
84
  outputs=[
85
  gr.Textbox(label="Summary"),
86
- gr.Audio(label="Audio Summary")
87
  ],
88
- title="URL to Audio Summary Agent",
89
- description="Summarizes only the article content from a URL and gives a more human-like audio summary using pyttsx3. CPU-only."
90
  )
91
 
92
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from langchain.chains import LLMChain
3
  from langchain.prompts import PromptTemplate
 
 
4
  from langchain.llms import HuggingFacePipeline
5
  from transformers import pipeline
 
 
6
  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)
13
  llm = HuggingFacePipeline(pipeline=summary_pipe)
14
 
15
+ # Prompt for more engaging summary
16
  summary_prompt = PromptTemplate.from_template("""
17
+ Summarize the following article content in a clear, warm, and motivational tone like a preacher speaking to an audience:
18
 
19
  {text}
20
 
 
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)
 
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
 
61
  return summary, None
62
 
63
  return summary, audio_path
 
64
  except Exception as e:
65
  return f"Error: {str(e)}", None
66
 
 
69
  inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
70
  outputs=[
71
  gr.Textbox(label="Summary"),
72
+ gr.Audio(label="Preacher-style Audio Summary")
73
  ],
74
+ title="Preaching-Style URL to Audio Agent",
75
+ description="Summarizes article content and reads it aloud in a warm, preacher-style voice using YourTTS. CPU-only."
76
  )
77
 
78
  if __name__ == "__main__":