dibend commited on
Commit
b69ff0d
·
verified ·
1 Parent(s): 47d20f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -2,10 +2,10 @@ from transformers import pipeline
2
  import gradio as gr
3
  import requests
4
 
5
- # Initialize the summarization pipeline (global initialization for efficiency)
6
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
7
 
8
- # Function to fetch content from Wikipedia with a limit on length
9
  def fetch_wikipedia_content(search_term, max_chars=10000):
10
  url = "https://en.wikipedia.org/w/api.php"
11
  params = {
@@ -17,7 +17,7 @@ def fetch_wikipedia_content(search_term, max_chars=10000):
17
  "redirects": 1 # Follow redirects
18
  }
19
  try:
20
- response = requests.get(url, headers=headers, params=params)
21
  response.raise_for_status()
22
  data = response.json()
23
  page = next(iter(data["query"]["pages"].values()))
@@ -38,7 +38,7 @@ def summarize_text(text):
38
  for i, chunk in enumerate(chunks):
39
  try:
40
  chunk_word_count = len(chunk.split())
41
- max_summary_length = min(120, max(40, chunk_word_count // 2)) # Optimized for smaller summaries
42
  summary = summarizer(chunk, max_length=max_summary_length, min_length=30, do_sample=False)[0]['summary_text']
43
  summaries.append(summary)
44
  except Exception as e:
@@ -57,11 +57,10 @@ iface = gr.Interface(
57
  fn=fetch_and_summarize,
58
  inputs=gr.Textbox(lines=1, placeholder="Enter a Wikipedia topic", label="Wikipedia Topic"),
59
  outputs=gr.Textbox(label="Summarized Content"),
60
- title="Optimized Wikipedia Article Summarizer",
61
- description="Fetch and summarize Wikipedia articles efficiently. Optimized for lightweight summarization.",
62
- allow_flagging="never" # Disable flagging for a lightweight deployment
63
  )
64
 
65
- # Launch Gradio with queuing for handling concurrent requests
66
  if __name__ == "__main__":
67
- iface.queue(concurrency_count=4).launch(debug=False)
 
2
  import gradio as gr
3
  import requests
4
 
5
+ # Initialize the summarization pipeline
6
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=-1)
7
 
8
+ # Function to fetch content from Wikipedia with a length cap
9
  def fetch_wikipedia_content(search_term, max_chars=10000):
10
  url = "https://en.wikipedia.org/w/api.php"
11
  params = {
 
17
  "redirects": 1 # Follow redirects
18
  }
19
  try:
20
+ response = requests.get(url, params=params)
21
  response.raise_for_status()
22
  data = response.json()
23
  page = next(iter(data["query"]["pages"].values()))
 
38
  for i, chunk in enumerate(chunks):
39
  try:
40
  chunk_word_count = len(chunk.split())
41
+ max_summary_length = min(120, max(50, chunk_word_count // 2)) # Adjust summary length
42
  summary = summarizer(chunk, max_length=max_summary_length, min_length=30, do_sample=False)[0]['summary_text']
43
  summaries.append(summary)
44
  except Exception as e:
 
57
  fn=fetch_and_summarize,
58
  inputs=gr.Textbox(lines=1, placeholder="Enter a Wikipedia topic", label="Wikipedia Topic"),
59
  outputs=gr.Textbox(label="Summarized Content"),
60
+ title="Wikipedia Article Summarizer",
61
+ description="Fetch and summarize Wikipedia articles efficiently. Just enter a topic to get started!",
 
62
  )
63
 
64
+ # Launch Gradio
65
  if __name__ == "__main__":
66
+ iface.launch(debug=True)