Spaces:
Runtime error
Runtime error
File size: 5,078 Bytes
8cc50db eadc0a8 b35bc08 8cc50db eadc0a8 8cc50db b35bc08 8cc50db 7df49c3 8cc50db 7df49c3 8cc50db 7df49c3 8cc50db eadc0a8 8cc50db eadc0a8 7df49c3 eadc0a8 8cc50db eadc0a8 7df49c3 eadc0a8 8cc50db 7df49c3 eadc0a8 8cc50db eadc0a8 7df49c3 eadc0a8 8cc50db eadc0a8 7df49c3 eadc0a8 8cc50db eadc0a8 7df49c3 eadc0a8 8cc50db eadc0a8 8cc50db eadc0a8 8cc50db eadc0a8 7df49c3 eadc0a8 7df49c3 8cc50db eadc0a8 7df49c3 8cc50db 7df49c3 eadc0a8 7df49c3 8cc50db 7df49c3 eadc0a8 8cc50db eadc0a8 7df49c3 8cc50db 7df49c3 eadc0a8 8cc50db 7df49c3 8cc50db eadc0a8 |
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 |
# app.py
"""
Gradio App for Smart Web Analyzer Plus - Human-Readable Outputs
Key Features:
- Accepts a URL
- Lets users select analysis modes (Clean Text, Summarization, Sentiment, Topic)
- Fetches and processes content (smart_web_analyzer.py)
- Displays each result in its own tab for readability
- Includes example URLs
"""
import gradio as gr
from smart_web_analyzer import (
fetch_web_content,
clean_text,
summarize_text,
analyze_sentiment,
detect_topic,
preview_clean_text,
)
def analyze_url(url, modes):
"""
Fetches web content and performs selected analyses (modes).
Parameters:
url (str): URL to analyze
modes (list): list of selected modes
Returns:
tuple of str: (clean_text_result, summarization_result, sentiment_result, topics_result)
"""
# Default messages if a mode is not selected
clean_text_result = "Mode not selected."
summarization_result = "Mode not selected."
sentiment_result = "Mode not selected."
topics_result = "Mode not selected."
# 1) Fetch/clean the web content
try:
html_content = fetch_web_content(url)
except Exception as e:
# Return the error in each field for clarity
error_msg = f"**Error fetching URL**: {e}"
return (error_msg, error_msg, error_msg, error_msg)
# Clean the text (keeping <script> and <style>)
cleaned = clean_text(html_content)
# 2) If the user requested a text preview
if "Clean Text Preview" in modes:
clean_text_result = preview_clean_text(cleaned, max_chars=500)
# 3) Summarization
if "Summarization" in modes:
result = summarize_text(cleaned)
# If the result starts with "Error", we can highlight it
if isinstance(result, str) and "Error" in result:
summarization_result = f"**Error during summarization**: {result}"
else:
summarization_result = result
# 4) Sentiment Analysis
if "Sentiment Analysis" in modes:
result = analyze_sentiment(cleaned)
if isinstance(result, str) and "Error" in result:
sentiment_result = f"**Error during sentiment analysis**: {result}"
else:
sentiment_result = f"**Predicted Sentiment**: {result}"
# 5) Topic Detection
if "Topic Detection" in modes:
topics = detect_topic(cleaned)
# Check if there's an error
if isinstance(topics, dict) and "error" in topics:
topics_result = f"**Error during topic detection**: {topics['error']}"
else:
# Format the topics into a readable string
formatted = ""
for t, score in topics.items():
formatted += f"- **{t}**: {score:.2f}\n"
topics_result = formatted if formatted else "No topics detected."
return (clean_text_result, summarization_result, sentiment_result, topics_result)
def build_app():
with gr.Blocks(title="Smart Web Analyzer Plus") as demo:
gr.Markdown("## Smart Web Analyzer Plus\n"
"Analyze web content for **summarization**, **sentiment**, and **topics**. "
"Choose your analysis modes and enter a URL below.")
with gr.Row():
url_input = gr.Textbox(
label="Enter URL",
placeholder="https://example.com",
lines=1
)
mode_selector = gr.CheckboxGroup(
label="Select Analysis Modes",
choices=["Clean Text Preview", "Summarization", "Sentiment Analysis", "Topic Detection"],
value=["Clean Text Preview", "Summarization", "Sentiment Analysis", "Topic Detection"]
)
# We'll display results in separate tabs for clarity
with gr.Tabs():
with gr.Tab("Clean Text Preview"):
preview_output = gr.Markdown()
with gr.Tab("Summarization"):
summary_output = gr.Markdown()
with gr.Tab("Sentiment Analysis"):
sentiment_output = gr.Markdown()
with gr.Tab("Topic Detection"):
topic_output = gr.Markdown()
analyze_button = gr.Button("Analyze")
# The "analyze_url" function returns a tuple of four strings
analyze_button.click(
fn=analyze_url,
inputs=[url_input, mode_selector],
outputs=[preview_output, summary_output, sentiment_output, topic_output]
)
# Example URLs
gr.Markdown("### Example URLs")
gr.Examples(
examples=[
["https://www.artificialintelligence-news.com/2024/02/14/openai-anthropic-google-white-house-red-teaming/"],
["https://www.artificialintelligence-news.com/2024/02/13/ai-21-labs-wordtune-chatgpt-plugin/"]
],
inputs=url_input,
label="Click an example to analyze"
)
return demo
if __name__ == "__main__":
demo = build_app()
demo.launch()
|