MHamdan commited on
Commit
eadc0a8
·
verified ·
1 Parent(s): 8cc50db

update app

Browse files
Files changed (1) hide show
  1. app.py +58 -32
app.py CHANGED
@@ -1,12 +1,12 @@
1
  # app.py
2
  """
3
- Gradio App for Smart Web Analyzer Plus
4
 
5
  Key Features:
6
  - Accepts a URL
7
  - Lets users select analysis modes (Clean Text, Summarization, Sentiment, Topic)
8
- - Fetches and processes content
9
- - Displays JSON output with results
10
  - Includes example URLs
11
  """
12
 
@@ -29,50 +29,67 @@ def analyze_url(url, modes):
29
  modes (list): list of selected modes
30
 
31
  Returns:
32
- dict: a dictionary of results or an error message
33
  """
34
- results = {}
 
 
 
 
35
 
36
- # Attempt to fetch the web content
37
  try:
38
  html_content = fetch_web_content(url)
39
  except Exception as e:
40
- return {"error": str(e)} # show error in JSON output
 
 
41
 
42
- # Clean the content
43
  cleaned = clean_text(html_content)
44
 
45
- # Perform selected analyses
46
  if "Clean Text Preview" in modes:
47
- results["Clean Text Preview"] = preview_clean_text(cleaned, max_chars=500)
48
 
 
49
  if "Summarization" in modes:
50
- results["Summarization"] = summarize_text(cleaned)
 
 
 
 
 
51
 
 
52
  if "Sentiment Analysis" in modes:
53
- results["Sentiment Analysis"] = analyze_sentiment(cleaned)
 
 
 
 
54
 
 
55
  if "Topic Detection" in modes:
56
  topics = detect_topic(cleaned)
 
57
  if isinstance(topics, dict) and "error" in topics:
58
- results["Topic Detection"] = topics["error"]
59
  else:
60
- # Format detected topics into a readable string
61
- # for the output
62
- topics_formatted = "\n".join([f"{t}: {s:.2f}" for t, s in topics.items()])
63
- results["Topic Detection"] = topics_formatted
 
64
 
65
- return results
66
 
67
- # Build Gradio Interface
68
  def build_app():
69
  with gr.Blocks(title="Smart Web Analyzer Plus") as demo:
70
- gr.Markdown("# Smart Web Analyzer Plus")
71
- gr.Markdown(
72
- "Analyze web content for summarization, sentiment, and topics. "
73
- "Choose your analysis modes and enter a URL below."
74
- )
75
-
76
  with gr.Row():
77
  url_input = gr.Textbox(
78
  label="Enter URL",
@@ -84,17 +101,25 @@ def build_app():
84
  choices=["Clean Text Preview", "Summarization", "Sentiment Analysis", "Topic Detection"],
85
  value=["Clean Text Preview", "Summarization", "Sentiment Analysis", "Topic Detection"]
86
  )
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- output_box = gr.JSON(label="Analysis Results")
89
-
90
- # Button to run analysis
91
  analyze_button = gr.Button("Analyze")
92
 
93
- # On click, run the analysis function
94
  analyze_button.click(
95
  fn=analyze_url,
96
  inputs=[url_input, mode_selector],
97
- outputs=output_box
98
  )
99
 
100
  # Example URLs
@@ -107,8 +132,9 @@ def build_app():
107
  inputs=url_input,
108
  label="Click an example to analyze"
109
  )
 
110
  return demo
111
 
112
  if __name__ == "__main__":
113
- demo_app = build_app()
114
- demo_app.launch()
 
1
  # app.py
2
  """
3
+ Gradio App for Smart Web Analyzer Plus - Human-Readable Outputs
4
 
5
  Key Features:
6
  - Accepts a URL
7
  - Lets users select analysis modes (Clean Text, Summarization, Sentiment, Topic)
8
+ - Fetches and processes content (smart_web_analyzer.py)
9
+ - Displays each result in its own tab for readability
10
  - Includes example URLs
11
  """
12
 
 
29
  modes (list): list of selected modes
30
 
31
  Returns:
32
+ tuple of str: (clean_text_result, summarization_result, sentiment_result, topics_result)
33
  """
34
+ # Default messages if a mode is not selected
35
+ clean_text_result = "Mode not selected."
36
+ summarization_result = "Mode not selected."
37
+ sentiment_result = "Mode not selected."
38
+ topics_result = "Mode not selected."
39
 
40
+ # 1) Fetch/clean the web content
41
  try:
42
  html_content = fetch_web_content(url)
43
  except Exception as e:
44
+ # Return the error in each field for clarity
45
+ error_msg = f"**Error fetching URL**: {e}"
46
+ return (error_msg, error_msg, error_msg, error_msg)
47
 
48
+ # Clean the text (keeping <script> and <style>)
49
  cleaned = clean_text(html_content)
50
 
51
+ # 2) If the user requested a text preview
52
  if "Clean Text Preview" in modes:
53
+ clean_text_result = preview_clean_text(cleaned, max_chars=500)
54
 
55
+ # 3) Summarization
56
  if "Summarization" in modes:
57
+ result = summarize_text(cleaned)
58
+ # If the result starts with "Error", we can highlight it
59
+ if isinstance(result, str) and "Error" in result:
60
+ summarization_result = f"**Error during summarization**: {result}"
61
+ else:
62
+ summarization_result = result
63
 
64
+ # 4) Sentiment Analysis
65
  if "Sentiment Analysis" in modes:
66
+ result = analyze_sentiment(cleaned)
67
+ if isinstance(result, str) and "Error" in result:
68
+ sentiment_result = f"**Error during sentiment analysis**: {result}"
69
+ else:
70
+ sentiment_result = f"**Predicted Sentiment**: {result}"
71
 
72
+ # 5) Topic Detection
73
  if "Topic Detection" in modes:
74
  topics = detect_topic(cleaned)
75
+ # Check if there's an error
76
  if isinstance(topics, dict) and "error" in topics:
77
+ topics_result = f"**Error during topic detection**: {topics['error']}"
78
  else:
79
+ # Format the topics into a readable string
80
+ formatted = ""
81
+ for t, score in topics.items():
82
+ formatted += f"- **{t}**: {score:.2f}\n"
83
+ topics_result = formatted if formatted else "No topics detected."
84
 
85
+ return (clean_text_result, summarization_result, sentiment_result, topics_result)
86
 
 
87
  def build_app():
88
  with gr.Blocks(title="Smart Web Analyzer Plus") as demo:
89
+ gr.Markdown("## Smart Web Analyzer Plus\n"
90
+ "Analyze web content for **summarization**, **sentiment**, and **topics**. "
91
+ "Choose your analysis modes and enter a URL below.")
92
+
 
 
93
  with gr.Row():
94
  url_input = gr.Textbox(
95
  label="Enter URL",
 
101
  choices=["Clean Text Preview", "Summarization", "Sentiment Analysis", "Topic Detection"],
102
  value=["Clean Text Preview", "Summarization", "Sentiment Analysis", "Topic Detection"]
103
  )
104
+
105
+ # We'll display results in separate tabs for clarity
106
+ with gr.Tabs():
107
+ with gr.Tab("Clean Text Preview"):
108
+ preview_output = gr.Markdown()
109
+ with gr.Tab("Summarization"):
110
+ summary_output = gr.Markdown()
111
+ with gr.Tab("Sentiment Analysis"):
112
+ sentiment_output = gr.Markdown()
113
+ with gr.Tab("Topic Detection"):
114
+ topic_output = gr.Markdown()
115
 
 
 
 
116
  analyze_button = gr.Button("Analyze")
117
 
118
+ # The "analyze_url" function returns a tuple of four strings
119
  analyze_button.click(
120
  fn=analyze_url,
121
  inputs=[url_input, mode_selector],
122
+ outputs=[preview_output, summary_output, sentiment_output, topic_output]
123
  )
124
 
125
  # Example URLs
 
132
  inputs=url_input,
133
  label="Click an example to analyze"
134
  )
135
+
136
  return demo
137
 
138
  if __name__ == "__main__":
139
+ demo = build_app()
140
+ demo.launch()