Rahmat82 commited on
Commit
b577308
·
verified ·
1 Parent(s): 6e41e2e

input length check added

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -7,7 +7,10 @@ import os
7
  API_URL = "https://api-inference.huggingface.co/models/Rahmat82/t5-small-finetuned-summarization-xsum"
8
  SECRET_KEY = os.environ.get("summarizer")
9
 
 
10
  async def summarize(text, retries=3):
 
 
11
  headers = {"Authorization": f"Bearer {SECRET_KEY}"}
12
  data = {
13
  "inputs": text,
@@ -24,18 +27,21 @@ async def summarize(text, retries=3):
24
  for attempt in range(retries):
25
  try:
26
  async with session.post(API_URL, headers=headers, json=data) as response:
27
- response.raise_for_status() # Raise exception for non-200 status codes
 
28
  result = await response.json()
29
  return result[0]["summary_text"]
30
  except aiohttp.ClientResponseError as e:
31
- # Retry only if the error is 503 (Service Unavailable)
32
  if e.status == 503 and attempt < retries - 1:
33
  print(f"Retry attempt {attempt+1} for 503 error.")
34
- await asyncio.sleep(1) # Add a short delay before retrying
 
35
  continue
36
  else:
37
  return f"Error: {e.status}, message='{e.message}'"
38
 
 
39
  iface = gr.Interface(
40
  theme=gr.themes.Soft(),
41
  fn=summarize,
 
7
  API_URL = "https://api-inference.huggingface.co/models/Rahmat82/t5-small-finetuned-summarization-xsum"
8
  SECRET_KEY = os.environ.get("summarizer")
9
 
10
+ # asynchronous function
11
  async def summarize(text, retries=3):
12
+ if len(text)) < 20:
13
+ return "Error: Input should be at least few sentences long!"
14
  headers = {"Authorization": f"Bearer {SECRET_KEY}"}
15
  data = {
16
  "inputs": text,
 
27
  for attempt in range(retries):
28
  try:
29
  async with session.post(API_URL, headers=headers, json=data) as response:
30
+ # raise exception for non-200 status codes
31
+ response.raise_for_status()
32
  result = await response.json()
33
  return result[0]["summary_text"]
34
  except aiohttp.ClientResponseError as e:
35
+ # retry only if the error is 503 (Service Unavailable)
36
  if e.status == 503 and attempt < retries - 1:
37
  print(f"Retry attempt {attempt+1} for 503 error.")
38
+ # add a short delay before retrying
39
+ await asyncio.sleep(1)
40
  continue
41
  else:
42
  return f"Error: {e.status}, message='{e.message}'"
43
 
44
+ # define gradio interface
45
  iface = gr.Interface(
46
  theme=gr.themes.Soft(),
47
  fn=summarize,