input length check added
Browse files
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 |
-
|
|
|
28 |
result = await response.json()
|
29 |
return result[0]["summary_text"]
|
30 |
except aiohttp.ClientResponseError as e:
|
31 |
-
#
|
32 |
if e.status == 503 and attempt < retries - 1:
|
33 |
print(f"Retry attempt {attempt+1} for 503 error.")
|
34 |
-
|
|
|
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,
|