File size: 3,565 Bytes
8cc50db
 
 
b35bc08
8cc50db
 
 
 
 
 
 
b35bc08
8cc50db
 
 
 
 
 
 
 
 
7df49c3
8cc50db
 
 
7df49c3
8cc50db
 
 
7df49c3
8cc50db
 
 
 
7df49c3
8cc50db
 
 
 
 
7df49c3
8cc50db
 
7df49c3
8cc50db
 
 
7df49c3
8cc50db
 
7df49c3
8cc50db
 
7df49c3
8cc50db
 
 
 
 
 
 
 
 
7df49c3
8cc50db
7df49c3
8cc50db
 
 
 
 
 
 
7df49c3
 
 
8cc50db
 
 
 
 
 
 
 
 
7df49c3
 
8cc50db
7df49c3
8cc50db
 
7df49c3
8cc50db
 
 
 
 
7df49c3
 
8cc50db
 
 
 
 
 
 
 
 
7df49c3
8cc50db
7df49c3
8cc50db
 
 
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
115
# app.py
"""
Gradio App for Smart Web Analyzer Plus

Key Features:
- Accepts a URL
- Lets users select analysis modes (Clean Text, Summarization, Sentiment, Topic)
- Fetches and processes content
- Displays JSON output with results
- Includes example URLs
"""

import gradio as gr
from smart_web_analyzer import (
    fetch_web_content,
    clean_text,
    summarize_text,
    analyze_sentiment,
    detect_topic,
    preview_clean_text,
)

def analyze_url(url, modes):
    """
    Fetches web content and performs selected analyses (modes).
    
    Parameters:
        url (str): URL to analyze
        modes (list): list of selected modes
    
    Returns:
        dict: a dictionary of results or an error message
    """
    results = {}
    
    # Attempt to fetch the web content
    try:
        html_content = fetch_web_content(url)
    except Exception as e:
        return {"error": str(e)}  # show error in JSON output
    
    # Clean the content
    cleaned = clean_text(html_content)
    
    # Perform selected analyses
    if "Clean Text Preview" in modes:
        results["Clean Text Preview"] = preview_clean_text(cleaned, max_chars=500)
    
    if "Summarization" in modes:
        results["Summarization"] = summarize_text(cleaned)
    
    if "Sentiment Analysis" in modes:
        results["Sentiment Analysis"] = analyze_sentiment(cleaned)
    
    if "Topic Detection" in modes:
        topics = detect_topic(cleaned)
        if isinstance(topics, dict) and "error" in topics:
            results["Topic Detection"] = topics["error"]
        else:
            # Format detected topics into a readable string
            # for the output
            topics_formatted = "\n".join([f"{t}: {s:.2f}" for t, s in topics.items()])
            results["Topic Detection"] = topics_formatted
    
    return results

# Build Gradio Interface
def build_app():
    with gr.Blocks(title="Smart Web Analyzer Plus") as demo:
        gr.Markdown("# Smart Web Analyzer Plus")
        gr.Markdown(
            "Analyze web content for summarization, sentiment, and topics. "
            "Choose your analysis modes and enter a URL below."
        )
        
        with gr.Row():
            url_input = gr.Textbox(
                label="Enter URL",
                placeholder="https://example.com",
                lines=1
            )
            mode_selector = gr.CheckboxGroup(
                label="Select Analysis Modes",
                choices=["Clean Text Preview", "Summarization", "Sentiment Analysis", "Topic Detection"],
                value=["Clean Text Preview", "Summarization", "Sentiment Analysis", "Topic Detection"]
            )
        
        output_box = gr.JSON(label="Analysis Results")
        
        # Button to run analysis
        analyze_button = gr.Button("Analyze")
        
        # On click, run the analysis function
        analyze_button.click(
            fn=analyze_url,
            inputs=[url_input, mode_selector],
            outputs=output_box
        )
        
        # Example URLs
        gr.Markdown("### Example URLs")
        gr.Examples(
            examples=[
                ["https://www.artificialintelligence-news.com/2024/02/14/openai-anthropic-google-white-house-red-teaming/"],
                ["https://www.artificialintelligence-news.com/2024/02/13/ai-21-labs-wordtune-chatgpt-plugin/"]
            ],
            inputs=url_input,
            label="Click an example to analyze"
        )
    return demo

if __name__ == "__main__":
    demo_app = build_app()
    demo_app.launch()