RohitCSharp commited on
Commit
f4064e9
·
verified ·
1 Parent(s): 44577f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -20
app.py CHANGED
@@ -5,14 +5,15 @@ 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
 
11
- # Step 1: CPU-friendly summarization LLM (Flan-T5 Small)
12
  summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
13
  llm = HuggingFacePipeline(pipeline=summary_pipe)
14
 
15
- # Step 2: Summarization Prompt
16
  summary_prompt = PromptTemplate.from_template("""
17
  Summarize the following webpage content in a clear, concise way:
18
 
@@ -23,36 +24,26 @@ Summary:
23
 
24
  summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
25
 
26
- # Step 3: URL to Text -> Summarize -> Text to Speech
27
-
28
  def url_to_audio_summary(url):
29
  try:
30
- # Load and split text
31
  loader = WebBaseLoader(url)
32
  docs = loader.load()
33
  splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=100)
34
  splits = splitter.split_documents(docs)
35
-
36
  full_text = "\n".join([s.page_content for s in splits])
37
-
38
- # Summarize
39
  summary = summary_chain.run(text=full_text)
40
 
41
- # Text to Speech
42
- tts_pipe = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits", device=-1)
43
- audio = tts_pipe(summary)["audio"]
44
-
45
- # Save audio to temp WAV
46
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
47
- f.write(audio)
48
- audio_path = f.name
49
 
50
- return summary, audio_path
51
 
52
  except Exception as e:
53
  return f"Error: {str(e)}", None
54
 
55
- # Step 4: Gradio Interface
56
  iface = gr.Interface(
57
  fn=url_to_audio_summary,
58
  inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
@@ -60,8 +51,8 @@ iface = gr.Interface(
60
  gr.Textbox(label="Summary"),
61
  gr.Audio(label="Audio Summary")
62
  ],
63
- title="🗣️ URL to Audio Summary Agent",
64
- description="An agent that reads web articles and gives you an audio summary. CPU-only. Built with LangChain + Hugging Face."
65
  )
66
 
67
  if __name__ == "__main__":
 
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain.llms import HuggingFacePipeline
7
  from transformers import pipeline
8
+ from gtts import gTTS
9
  import tempfile
10
  import os
11
 
12
+ # CPU-friendly summarization LLM
13
  summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
14
  llm = HuggingFacePipeline(pipeline=summary_pipe)
15
 
16
+ # Summarization prompt
17
  summary_prompt = PromptTemplate.from_template("""
18
  Summarize the following webpage content in a clear, concise way:
19
 
 
24
 
25
  summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
26
 
 
 
27
  def url_to_audio_summary(url):
28
  try:
 
29
  loader = WebBaseLoader(url)
30
  docs = loader.load()
31
  splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=100)
32
  splits = splitter.split_documents(docs)
33
+
34
  full_text = "\n".join([s.page_content for s in splits])
 
 
35
  summary = summary_chain.run(text=full_text)
36
 
37
+ # Use gTTS for TTS since Hugging Face TTS model failed
38
+ tts = gTTS(text=summary)
39
+ temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
40
+ tts.save(temp_path.name)
 
 
 
 
41
 
42
+ return summary, temp_path.name
43
 
44
  except Exception as e:
45
  return f"Error: {str(e)}", None
46
 
 
47
  iface = gr.Interface(
48
  fn=url_to_audio_summary,
49
  inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
 
51
  gr.Textbox(label="Summary"),
52
  gr.Audio(label="Audio Summary")
53
  ],
54
+ title="URL to Audio Summary Agent",
55
+ description="Summarizes article from a URL and gives an audio summary. CPU-only using gTTS."
56
  )
57
 
58
  if __name__ == "__main__":