Spaces:
Running
Running
Update app.py
Browse files
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 |
-
#
|
12 |
summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
13 |
llm = HuggingFacePipeline(pipeline=summary_pipe)
|
14 |
|
15 |
-
#
|
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 |
-
#
|
42 |
-
|
43 |
-
|
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,
|
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="
|
64 |
-
description="
|
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__":
|