Spaces:
Sleeping
Sleeping
File size: 4,358 Bytes
37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 8119b03 37ff7dd 4518be2 37ff7dd 8119b03 4518be2 8119b03 4518be2 8119b03 4518be2 8119b03 4518be2 8119b03 4518be2 8119b03 37ff7dd 8119b03 37ff7dd 4518be2 8119b03 4518be2 8119b03 4518be2 37ff7dd 8119b03 4518be2 8119b03 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 |
# 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
) |