connection attempt update
Browse files
app.py
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import aiohttp
|
| 3 |
import asyncio
|
|
@@ -9,7 +47,7 @@ SECRET_KEY = os.environ.get("summarizer")
|
|
| 9 |
|
| 10 |
# asynchronous function
|
| 11 |
async def summarize(text, retries=3):
|
| 12 |
-
if len(text)<250:
|
| 13 |
return "⛔ You input is too short! Please input a longer text for summarization."
|
| 14 |
headers = {"Authorization": f"Bearer {SECRET_KEY}"}
|
| 15 |
data = {
|
|
@@ -28,19 +66,19 @@ async def summarize(text, retries=3):
|
|
| 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 "Oops! 🙈 It looks like those mischievous monkeys🐒 might be swinging around the server, causing a bit of chaos with the cables. Please try it again!"
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
# define gradio interface
|
| 46 |
iface = gr.Interface(
|
|
|
|
| 1 |
+
|
| 2 |
+
# # asynchronous function
|
| 3 |
+
# async def summarize(text, retries=3):
|
| 4 |
+
# if len(text)<250:
|
| 5 |
+
# return "⛔ You input is too short! Please input a longer text for summarization."
|
| 6 |
+
# headers = {"Authorization": f"Bearer {SECRET_KEY}"}
|
| 7 |
+
# data = {
|
| 8 |
+
# "inputs": text,
|
| 9 |
+
# "options": {
|
| 10 |
+
# "max_new_tokens": 100,
|
| 11 |
+
# "min_length": 10,
|
| 12 |
+
# "max_length": 500,
|
| 13 |
+
# "length_penalty": 2.0,
|
| 14 |
+
# "num_beams": 3,
|
| 15 |
+
# "early_stopping": True
|
| 16 |
+
# }
|
| 17 |
+
# }
|
| 18 |
+
# async with aiohttp.ClientSession() as session:
|
| 19 |
+
# for attempt in range(retries):
|
| 20 |
+
# try:
|
| 21 |
+
# async with session.post(API_URL, headers=headers, json=data) as response:
|
| 22 |
+
# # raise exception for non-200 status codes
|
| 23 |
+
# response.raise_for_status()
|
| 24 |
+
# result = await response.json()
|
| 25 |
+
# return result[0]["summary_text"]
|
| 26 |
+
# except aiohttp.ClientResponseError as e:
|
| 27 |
+
# # retry only if the error is 503 (Service Unavailable)
|
| 28 |
+
# if e.status == 503 and attempt < retries - 1:
|
| 29 |
+
# # print(f"Retry attempt {attempt+1} for 503 error.")
|
| 30 |
+
# # add a short delay before retrying
|
| 31 |
+
# await asyncio.sleep(1)
|
| 32 |
+
# continue
|
| 33 |
+
# else:
|
| 34 |
+
# return "Oops! 🙈 It looks like those mischievous monkeys🐒 might be swinging around the server, causing a bit of chaos with the cables. Please try it again!"
|
| 35 |
+
# # return f"Error: {e.status}, message='{e.message}'. Please try again 🔁"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
import gradio as gr
|
| 40 |
import aiohttp
|
| 41 |
import asyncio
|
|
|
|
| 47 |
|
| 48 |
# asynchronous function
|
| 49 |
async def summarize(text, retries=3):
|
| 50 |
+
if len(text) < 250:
|
| 51 |
return "⛔ You input is too short! Please input a longer text for summarization."
|
| 52 |
headers = {"Authorization": f"Bearer {SECRET_KEY}"}
|
| 53 |
data = {
|
|
|
|
| 66 |
try:
|
| 67 |
async with session.post(API_URL, headers=headers, json=data) as response:
|
| 68 |
# raise exception for non-200 status codes
|
| 69 |
+
response.raise_for_status()
|
| 70 |
result = await response.json()
|
| 71 |
return result[0]["summary_text"]
|
| 72 |
+
except (aiohttp.ClientResponseError, aiohttp.ClientConnectionError) as e:
|
| 73 |
# retry only if the error is 503 (Service Unavailable)
|
| 74 |
+
if isinstance(e, aiohttp.ClientResponseError) and e.status == 503 and attempt < retries - 1:
|
|
|
|
| 75 |
# add a short delay before retrying
|
| 76 |
+
await asyncio.sleep(1)
|
| 77 |
continue
|
| 78 |
else:
|
| 79 |
return "Oops! 🙈 It looks like those mischievous monkeys🐒 might be swinging around the server, causing a bit of chaos with the cables. Please try it again!"
|
| 80 |
+
|
| 81 |
+
|
| 82 |
|
| 83 |
# define gradio interface
|
| 84 |
iface = gr.Interface(
|