File size: 3,339 Bytes
66a4d52
 
 
6078dfb
66a4d52
90a9f3d
501ba1b
1936100
 
6078dfb
66a4d52
90a9f3d
501ba1b
90a9f3d
501ba1b
66a4d52
501ba1b
 
6078dfb
bf2abeb
66a4d52
501ba1b
1936100
 
bf2abeb
6c15bdd
 
 
 
 
bf2abeb
6c15bdd
 
bf2abeb
6c15bdd
 
 
 
 
 
 
 
 
 
 
 
 
501ba1b
90a9f3d
 
6078dfb
90a9f3d
 
6078dfb
 
 
 
 
 
90a9f3d
 
1936100
6078dfb
90a9f3d
 
 
bf2abeb
90a9f3d
 
 
 
 
 
 
 
bf2abeb
90a9f3d
 
 
bf2abeb
90a9f3d
bf2abeb
6078dfb
90a9f3d
 
 
 
 
 
 
 
 
bf2abeb
6078dfb
90a9f3d
 
 
6078dfb
90a9f3d
 
bf2abeb
90a9f3d
6078dfb
90a9f3d
6078dfb
90a9f3d
 
66a4d52
bf2abeb
90a9f3d
6078dfb
bf2abeb
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import gradio as gr
import requests
from bs4 import BeautifulSoup
from transformers import pipeline

# Load summarization pipeline
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")

def scrape_website(url):
    """Extracts text from a website with error handling"""
    try:
        headers = {'User-Agent': 'Mozilla/5.0'}
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()
        
        soup = BeautifulSoup(response.text, "html.parser")
        text_elements = soup.find_all(['p', 'article', 'main', 'section'])
        text = " ".join([e.get_text(strip=True, separator=' ') for e in text_elements])
        return text.strip() if text.strip() else "No content found"
        
    except Exception as e:
        return f"Scraping Error: {str(e)}"

def summarize_website(url):
    """Handles the full summarization pipeline"""
    try:            
        extracted_text = scrape_website(url)
        
        if "Error" in extracted_text:
            return f"❌ {extracted_text}"
            
        if len(extracted_text.split()) < 50:
            return "⚠️ Error: Insufficient content for summarization (minimum 50 words required)"
            
        max_input_length = 1000
        truncated_text = extracted_text[:max_input_length]
        
        summary = summarizer(
            truncated_text,
            max_length=200,
            min_length=50,
            do_sample=False,
            truncation=True
        )
        
        return f"## πŸ“ Summary\n\n{summary[0]['summary_text']}"
        
    except Exception as e:
        return f"β›” Summarization Error: {str(e)}"

# Custom CSS for mobile optimization
css = """
@media screen and (max-width: 600px) {
    .container {
        padding: 10px !important;
    }
    .input-box textarea {
        font-size: 16px !important;
    }
}
"""

# Mobile-optimized interface with Blocks API
with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Website Summarizer") as app:
    gr.Markdown("# 🌐 AI Website Summarizer")
    gr.Markdown("Paste any website URL below to get an instant AI-powered summary!")
    
    with gr.Row():
        url_input = gr.Textbox(
            label="Website URL",
            placeholder="Enter full URL (https://...)",
            lines=1,
            max_lines=1,
            elem_id="input-box"
        )
    
    with gr.Row():
        submit_btn = gr.Button("Generate Summary πŸš€", variant="primary")
        clear_btn = gr.Button("Clear πŸ”„")
    
    output = gr.Markdown()
    
    # Example section
    gr.Examples(
        examples=[
            ["https://en.wikipedia.org/wiki/Large_language_model"],
            ["https://www.bbc.com/news/technology-66510295"]
        ],
        inputs=url_input,
        label="Try these examples:",
        examples_per_page=2
    )
    
    # Event handlers
    submit_btn.click(
        fn=summarize_website,
        inputs=url_input,
        outputs=output,
        api_name="summarize"
    )
    
    clear_btn.click(
        fn=lambda: ("", ""),
        inputs=None,
        outputs=[url_input, output],
        queue=False
    )

# Mobile-friendly configuration
app.launch(
    server_name="0.0.0.0",
    server_port=7860,
    favicon_path="https://www.svgrepo.com/show/355037/huggingface.svg"
)