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