File size: 2,974 Bytes
d9efe10
 
 
 
 
 
 
 
 
e9d5607
 
4714e38
d9efe10
f4064e9
d9efe10
 
 
f4064e9
d9efe10
4714e38
d9efe10
 
 
 
 
 
 
 
e9d5607
 
 
 
 
 
 
 
 
 
 
 
 
 
4714e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9efe10
 
e9d5607
 
 
f4064e9
e9d5607
d9efe10
4714e38
 
 
d9efe10
4714e38
d9efe10
 
 
 
 
 
 
 
 
 
 
f4064e9
4714e38
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
from bs4 import BeautifulSoup
import requests
import pyttsx3

# CPU-friendly summarization LLM
summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
llm = HuggingFacePipeline(pipeline=summary_pipe)

# Summarization prompt
summary_prompt = PromptTemplate.from_template("""
Summarize the following article content in a clear, concise, and emotionally engaging manner as if you're speaking to a curious listener:

{text}

Summary:
""")

summary_chain = LLMChain(llm=llm, prompt=summary_prompt)

def extract_main_content(url):
    try:
        response = requests.get(url, timeout=10)
        soup = BeautifulSoup(response.content, "html.parser")

        for tag in soup(["nav", "header", "footer", "aside", "script", "style", "noscript"]):
            tag.decompose()

        paragraphs = soup.find_all("p")
        content = "\n".join([p.get_text() for p in paragraphs if len(p.get_text()) > 60])
        return content.strip()
    except Exception as e:
        return f"Error extracting article content: {str(e)}"

def generate_human_like_audio(text):
    try:
        engine = pyttsx3.init()
        engine.setProperty('rate', 150)  # slower pace
        engine.setProperty('volume', 1.0)
        voices = engine.getProperty('voices')

        # Choose a more natural voice if available (optional: pick female)
        for voice in voices:
            if 'female' in voice.name.lower():
                engine.setProperty('voice', voice.id)
                break

        temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
        engine.save_to_file(text, temp_path.name)
        engine.runAndWait()

        return temp_path.name
    except Exception as e:
        return None

def url_to_audio_summary(url):
    try:
        article_text = extract_main_content(url)
        if article_text.startswith("Error"):
            return article_text, None

        summary = summary_chain.run(text=article_text)

        audio_path = generate_human_like_audio(summary)
        if not audio_path:
            return summary, None

        return summary, audio_path

    except Exception as e:
        return f"Error: {str(e)}", None

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="Summarizes only the article content from a URL and gives a more human-like audio summary using pyttsx3. CPU-only."
)

if __name__ == "__main__":
    iface.launch()