# 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: return { "clean_text": f"❌ Error: {results['error']}", "summary": "", "sentiment": "", "topics": "" } 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 "" ) # Format sentiment formatted["sentiment"] = ( f"**Sentiment Analysis:**\n{results['sentiment']}" if 'sentiment' in results else "" ) # 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"] = "" 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 with single row of tabs output_tabs = gr.Tabs([ gr.Tab("📄 Clean Text", gr.Markdown(label="Clean Text")), gr.Tab("📝 Summary", gr.Markdown(label="Summary")), gr.Tab("🎭 Sentiment", gr.Markdown(label="Sentiment")), gr.Tab("📊 Topics", gr.Markdown(label="Topics")) ]) # 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=[ output_tabs.select(0), # Clean Text output_tabs.select(1), # Summary output_tabs.select(2), # Sentiment output_tabs.select(3) # Topics ] ).then( fn=on_analyze_end, outputs=[status] ) if __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=7860 )