MHamdan's picture
Update files
8cc50db verified
raw
history blame
3.57 kB
# app.py
"""
Gradio App for Smart Web Analyzer Plus
Key Features:
- Accepts a URL
- Lets users select analysis modes (Clean Text, Summarization, Sentiment, Topic)
- Fetches and processes content
- Displays JSON output with results
- 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:
dict: a dictionary of results or an error message
"""
results = {}
# Attempt to fetch the web content
try:
html_content = fetch_web_content(url)
except Exception as e:
return {"error": str(e)} # show error in JSON output
# Clean the content
cleaned = clean_text(html_content)
# Perform selected analyses
if "Clean Text Preview" in modes:
results["Clean Text Preview"] = preview_clean_text(cleaned, max_chars=500)
if "Summarization" in modes:
results["Summarization"] = summarize_text(cleaned)
if "Sentiment Analysis" in modes:
results["Sentiment Analysis"] = analyze_sentiment(cleaned)
if "Topic Detection" in modes:
topics = detect_topic(cleaned)
if isinstance(topics, dict) and "error" in topics:
results["Topic Detection"] = topics["error"]
else:
# Format detected topics into a readable string
# for the output
topics_formatted = "\n".join([f"{t}: {s:.2f}" for t, s in topics.items()])
results["Topic Detection"] = topics_formatted
return results
# Build Gradio Interface
def build_app():
with gr.Blocks(title="Smart Web Analyzer Plus") as demo:
gr.Markdown("# Smart Web Analyzer Plus")
gr.Markdown(
"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"]
)
output_box = gr.JSON(label="Analysis Results")
# Button to run analysis
analyze_button = gr.Button("Analyze")
# On click, run the analysis function
analyze_button.click(
fn=analyze_url,
inputs=[url_input, mode_selector],
outputs=output_box
)
# 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_app = build_app()
demo_app.launch()