File size: 3,730 Bytes
265b6a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Gradio_UI.py
import gradio as gr
from smolagents import CodeAgent
from typing import Optional

class GradioUI:
    def __init__(self, agent: CodeAgent):
        self.agent = agent

    def process_query(self, query: str) -> str:
        try:
            response = self.agent.run(query)
            return response
        except Exception as e:
            return f"Error processing query: {str(e)}"

    def launch(self, 
               server_name: Optional[str] = None,
               server_port: Optional[int] = None,
               share: bool = False):
        
        # Create the interface
        with gr.Blocks(title="Smart Web Analyzer Plus") as demo:
            gr.Markdown("# 🌐 Smart Web Analyzer Plus")
            gr.Markdown("Analyze web content using AI to extract summaries, determine sentiment, and identify topics.")
            
            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"
                    )
            
            # Output display
            with gr.Tabs() as tabs:
                with gr.Tab("πŸ“„ Clean Text"):
                    clean_text_output = gr.Markdown()
                with gr.Tab("πŸ“ Summary"):
                    summary_output = gr.Markdown()
                with gr.Tab("🎭 Sentiment"):
                    sentiment_output = gr.Markdown()
                with gr.Tab("πŸ“Š Topics"):
                    topics_output = gr.Markdown()
            
            # Loading indicator
            status = gr.Markdown(visible=False)
            
            # Examples
            gr.Examples(
                examples=[
                    ["https://www.bbc.com/news/technology-67881954", ["summarize", "sentiment"]],
                    ["https://arxiv.org/html/2312.17296v1", ["topics", "summarize"]]
                ],
                inputs=[url_input, analysis_types],
                label="Try these examples"
            )
            
            def create_analysis_prompt(url: str, types: list) -> str:
                type_str = ", ".join(types)
                return f"Analyze the content at {url} and provide {type_str} analysis."
            
            def on_analyze_start():
                return gr.update(value="⏳ Analysis in progress...", visible=True)
            
            def on_analyze_end():
                return gr.update(value="", visible=False)
            
            # Event handlers
            analyze_btn.click(
                fn=on_analyze_start,
                outputs=[status]
            ).then(
                fn=lambda url, types: self.process_query(create_analysis_prompt(url, types)),
                inputs=[url_input, analysis_types],
                outputs=[clean_text_output]  # The agent will format the output appropriately
            ).then(
                fn=on_analyze_end,
                outputs=[status]
            )
        
        # Launch the interface
        demo.launch(
            server_name=server_name,
            server_port=server_port,
            share=share
        )