adithya747 commited on
Commit
bf2abeb
Β·
verified Β·
1 Parent(s): 6078dfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -38
app.py CHANGED
@@ -16,40 +16,38 @@ def scrape_website(url):
16
  soup = BeautifulSoup(response.text, "html.parser")
17
  text_elements = soup.find_all(['p', 'article', 'main', 'section'])
18
  text = " ".join([e.get_text(strip=True, separator=' ') for e in text_elements])
19
-
20
  return text.strip() if text.strip() else "No content found"
21
-
22
  except Exception as e:
23
  return f"Scraping Error: {str(e)}"
24
 
25
  def summarize_website(url):
26
- """Handles website summarization with proper truncation"""
27
  try:
28
- extracted_text = scrape_website(url)
29
-
30
- if "Error" in extracted_text:
31
- return f"❌ {extracted_text}"
32
-
33
- if len(extracted_text.split()) < 50:
34
- return "⚠️ Error: Insufficient content for summarization (minimum 50 words required)"
35
-
36
- # Ensure input is within token limit
37
- max_input_tokens = 1024 # Model limit
38
- truncated_text = " ".join(extracted_text.split()[:max_input_tokens])
39
-
40
- # Summarization
41
- summary = summarizer(
42
- truncated_text,
43
- max_length=250, # Extended summary
44
- min_length=80,
45
- do_sample=False
46
- )
47
-
48
- if not summary or not isinstance(summary, list) or "summary_text" not in summary[0]:
49
- return "⚠️ Error: Summarization failed. Try a different website."
50
-
51
- return f"## πŸ“ Summary\n\n{summary[0]['summary_text']}"
52
-
53
  except Exception as e:
54
  return f"β›” Summarization Error: {str(e)}"
55
 
@@ -69,7 +67,7 @@ css = """
69
  with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Website Summarizer") as app:
70
  gr.Markdown("# 🌐 AI Website Summarizer")
71
  gr.Markdown("Paste any website URL below to get an instant AI-powered summary!")
72
-
73
  with gr.Row():
74
  url_input = gr.Textbox(
75
  label="Website URL",
@@ -78,13 +76,13 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Website Summarizer") as a
78
  max_lines=1,
79
  elem_id="input-box"
80
  )
81
-
82
  with gr.Row():
83
  submit_btn = gr.Button("Generate Summary πŸš€", variant="primary")
84
  clear_btn = gr.Button("Clear πŸ”„")
85
-
86
  output = gr.Markdown()
87
-
88
  # Example section
89
  gr.Examples(
90
  examples=[
@@ -95,10 +93,10 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Website Summarizer") as a
95
  label="Try these examples:",
96
  examples_per_page=2
97
  )
98
-
99
  # Progress indicator
100
  progress = gr.Textbox(visible=False)
101
-
102
  # Event handlers
103
  submit_btn.click(
104
  fn=summarize_website,
@@ -106,7 +104,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Website Summarizer") as a
106
  outputs=output,
107
  api_name="summarize"
108
  )
109
-
110
  clear_btn.click(
111
  fn=lambda: ("", ""),
112
  inputs=None,
@@ -114,8 +112,9 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Website Summarizer") as a
114
  queue=False
115
  )
116
 
117
- # Launch the app without broken favicon
118
  app.launch(
119
  server_name="0.0.0.0",
120
- server_port=7860
121
- )
 
 
16
  soup = BeautifulSoup(response.text, "html.parser")
17
  text_elements = soup.find_all(['p', 'article', 'main', 'section'])
18
  text = " ".join([e.get_text(strip=True, separator=' ') for e in text_elements])
 
19
  return text.strip() if text.strip() else "No content found"
20
+
21
  except Exception as e:
22
  return f"Scraping Error: {str(e)}"
23
 
24
  def summarize_website(url):
25
+ """Handles the full summarization pipeline"""
26
  try:
27
+ with gr.Column(variant="panel"):
28
+ gr.Markdown("## ⚑ Processing...")
29
+
30
+ extracted_text = scrape_website(url)
31
+
32
+ if "Error" in extracted_text:
33
+ return f"❌ {extracted_text}"
34
+
35
+ if len(extracted_text.split()) < 50:
36
+ return "⚠️ Error: Insufficient content for summarization (minimum 50 words required)"
37
+
38
+ max_input_length = 1000
39
+ truncated_text = extracted_text[:max_input_length]
40
+
41
+ summary = summarizer(
42
+ truncated_text,
43
+ max_length=200,
44
+ min_length=50,
45
+ do_sample=False,
46
+ truncation=True
47
+ )
48
+
49
+ return f"## πŸ“ Summary\n\n{summary[0]['summary_text']}"
50
+
 
51
  except Exception as e:
52
  return f"β›” Summarization Error: {str(e)}"
53
 
 
67
  with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Website Summarizer") as app:
68
  gr.Markdown("# 🌐 AI Website Summarizer")
69
  gr.Markdown("Paste any website URL below to get an instant AI-powered summary!")
70
+
71
  with gr.Row():
72
  url_input = gr.Textbox(
73
  label="Website URL",
 
76
  max_lines=1,
77
  elem_id="input-box"
78
  )
79
+
80
  with gr.Row():
81
  submit_btn = gr.Button("Generate Summary πŸš€", variant="primary")
82
  clear_btn = gr.Button("Clear πŸ”„")
83
+
84
  output = gr.Markdown()
85
+
86
  # Example section
87
  gr.Examples(
88
  examples=[
 
93
  label="Try these examples:",
94
  examples_per_page=2
95
  )
96
+
97
  # Progress indicator
98
  progress = gr.Textbox(visible=False)
99
+
100
  # Event handlers
101
  submit_btn.click(
102
  fn=summarize_website,
 
104
  outputs=output,
105
  api_name="summarize"
106
  )
107
+
108
  clear_btn.click(
109
  fn=lambda: ("", ""),
110
  inputs=None,
 
112
  queue=False
113
  )
114
 
115
+ # Mobile-friendly configuration
116
  app.launch(
117
  server_name="0.0.0.0",
118
+ server_port=7860,
119
+ favicon_path="https://www.svgrepo.com/show/355037/huggingface.svg"
120
+ )