Spaces:
Sleeping
Sleeping
# 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 | |
) | |