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