File size: 4,625 Bytes
37ff7dd
 
 
4518be2
37ff7dd
 
 
4518be2
 
37ff7dd
6f33c06
4518be2
6f33c06
 
 
 
4518be2
37ff7dd
4518be2
37ff7dd
4518be2
 
 
 
 
 
 
6f33c06
4518be2
 
 
 
 
6f33c06
4518be2
 
 
37ff7dd
4518be2
 
 
 
 
 
 
 
 
 
 
6f33c06
37ff7dd
4518be2
37ff7dd
4518be2
 
 
 
 
 
 
 
 
 
8119b03
 
37ff7dd
4518be2
37ff7dd
8119b03
4518be2
 
 
 
 
 
8119b03
4518be2
 
8119b03
4518be2
 
 
 
 
 
 
 
 
6f33c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4518be2
 
8119b03
4518be2
 
 
8119b03
37ff7dd
 
 
 
8119b03
37ff7dd
 
4518be2
 
 
 
 
 
 
 
 
8119b03
4518be2
 
8119b03
4518be2
 
 
 
 
 
37ff7dd
8119b03
4518be2
6f33c06
 
 
 
4518be2
 
 
 
37ff7dd
 
 
4518be2
8119b03
 
4518be2
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# app.py
import gradio as gr
from smart_web_analyzer import WebAnalyzer
from typing import Dict, List, Any

analyzer = WebAnalyzer()

def format_results(results: Dict[str, Any]) -> Dict[str, str]:
    """Format analysis results for Gradio components"""
    if 'error' in results:
        error_msg = f"❌ Error: {results['error']}"
        return {
            "clean_text": error_msg,
            "summary": error_msg,
            "sentiment": error_msg,
            "topics": error_msg
        }
    
    formatted = {}
    
    # Format clean text
    text = results.get('clean_text', 'No text extracted')
    formatted["clean_text"] = text[:2000] + "..." if len(text) > 2000 else text
    
    # Format summary
    formatted["summary"] = (
        f"**AI Summary:**\n{results['summary']}"
        if 'summary' in results else "No summary requested"
    )
    
    # Format sentiment
    formatted["sentiment"] = (
        f"**Sentiment Analysis:**\n{results['sentiment']}"
        if 'sentiment' in results else "No sentiment analysis requested"
    )
    
    # Format topics
    if 'topics' in results:
        topics_list = sorted(
            results['topics'].items(),
            key=lambda x: x[1],
            reverse=True
        )
        topics_text = "\n".join(
            f"- **{topic}**: {score:.1%}" 
            for topic, score in topics_list
        )
        formatted["topics"] = f"**Detected Topics:**\n{topics_text}"
    else:
        formatted["topics"] = "No topic analysis requested"
    
    return formatted

def validate_url(url: str) -> bool:
    """Basic URL validation"""
    return bool(url and url.strip().startswith(('http://', 'https://')))

def update_button_state(url: str) -> Dict:
    """Update button state based on URL validity"""
    return gr.update(interactive=validate_url(url))

with gr.Blocks(title="Smart Web Analyzer Plus", theme=gr.themes.Soft()) as demo:
    # Header
    gr.Markdown("# 🌐 Smart Web Analyzer Plus")
    gr.Markdown("Analyze web content using AI to extract summaries, determine sentiment, and identify topics.")
    
    # Input Section
    with gr.Row():
        with gr.Column(scale=3):
            url_input = gr.Textbox(
                label="Enter URL",
                placeholder="https://example.com",
                show_label=True
            )
        with gr.Column(scale=2):
            analysis_types = gr.CheckboxGroup(
                choices=["summarize", "sentiment", "topics"],
                label="Analysis Types",
                value=["summarize"],
                show_label=True
            )
        with gr.Column(scale=1):
            analyze_btn = gr.Button(
                "Analyze",
                variant="primary",
                interactive=False
            )
    
    # Content display
    clean_text_out = gr.Markdown(visible=True, label="Clean Text")
    summary_out = gr.Markdown(visible=True, label="Summary")
    sentiment_out = gr.Markdown(visible=True, label="Sentiment")
    topics_out = gr.Markdown(visible=True, label="Topics")
    
    with gr.Tabs() as tabs:
        with gr.Tab("πŸ“„ Clean Text"):
            clean_text_out
        with gr.Tab("πŸ“ Summary"):
            summary_out
        with gr.Tab("🎭 Sentiment"):
            sentiment_out
        with gr.Tab("πŸ“Š Topics"):
            topics_out
    
    # Loading indicator
    status = gr.Markdown(visible=False)
    
    # Example Section
    gr.Examples(
        label="Try these examples",
        examples=[
            ["https://www.bbc.com/news/technology-67881954", ["summarize", "sentiment"]],
            ["https://arxiv.org/html/2312.17296v1", ["topics", "summarize"]]
        ],
        inputs=[url_input, analysis_types]
    )
    
    # Event Handlers
    url_input.change(
        fn=update_button_state,
        inputs=[url_input],
        outputs=[analyze_btn],
        queue=False
    )
    
    def on_analyze_start():
        return gr.update(value="⏳ Analysis in progress...", visible=True)
    
    def on_analyze_end():
        return gr.update(value="", visible=False)
    
    analyze_btn.click(
        fn=on_analyze_start,
        outputs=[status],
        queue=False
    ).then(
        fn=lambda url, m: format_results(analyzer.analyze(url, m)),
        inputs=[url_input, analysis_types],
        outputs=[
            clean_text_out,
            summary_out,
            sentiment_out,
            topics_out
        ]
    ).then(
        fn=on_analyze_end,
        outputs=[status]
    )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860
    )