Spaces:
Sleeping
Sleeping
File size: 4,458 Bytes
37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 4518be2 37ff7dd 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 146 147 148 149 150 151 152 153 154 |
# 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
Analyze web content using AI to extract summaries, determine sentiment, and identify topics.
"""
)
# Input Section
with gr.Row():
with gr.Column(scale=4):
url_input = gr.Textbox(
label="Enter URL",
placeholder="https://example.com",
show_label=True
)
with gr.Column(scale=2):
modes = gr.CheckboxGroup(
choices=["summarize", "sentiment", "topics"],
label="Analysis Types",
value=["summarize"], # Default selection
show_label=True
)
with gr.Column(scale=1):
analyze_btn = gr.Button(
"Analyze",
variant="primary",
interactive=False
)
# Results Section
with gr.Tabs() as tabs:
with gr.TabItem("π Clean Text"):
clean_text_output = gr.Markdown()
with gr.TabItem("π Summary"):
summary_output = gr.Markdown()
with gr.TabItem("π Sentiment"):
sentiment_output = gr.Markdown()
with gr.TabItem("π Topics"):
topics_output = gr.Markdown()
# Loading indicator
with gr.Row():
status = gr.Markdown("")
# Example Section
gr.Examples(
examples=[
["https://www.bbc.com/news/technology-67881954", ["summarize", "sentiment"]],
["https://arxiv.org/html/2312.17296v1", ["topics", "summarize"]]
],
inputs=[url_input, modes],
label="Try these examples"
)
# 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...")
def on_analyze_end():
return gr.update(value="")
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, modes],
outputs=[
clean_text_output,
summary_output,
sentiment_output,
topics_output
]
).then(
fn=on_analyze_end,
outputs=[status]
)
if __name__ == "__main__":
demo.launch(
share=False, # Set to True to create a public link
server_name="0.0.0.0", # Allow external connections
server_port=7860 # Default Gradio port
) |