MatteoScript commited on
Commit
f8ee6ca
·
verified ·
1 Parent(s): 0e1eeff

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -1
main.py CHANGED
@@ -128,7 +128,34 @@ def generate_textAsync(request: Request, input_data: InputDataAsync):
128
  result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", input_data))
129
  return {"response": result_data}
130
 
131
- async def make_request(session, token, data, url):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  headers = {
133
  'Content-Type': 'application/json',
134
  'Authorization': 'Bearer ' + token
 
128
  result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", input_data))
129
  return {"response": result_data}
130
 
131
+ async def make_request(session, token, data, url, max_retries=3):
132
+ headers = {
133
+ 'Content-Type': 'application/json',
134
+ 'Authorization': 'Bearer ' + token
135
+ }
136
+ for _ in range(max_retries):
137
+ try:
138
+ async with session.post(url, headers=headers, json=data) as response:
139
+ response.raise_for_status()
140
+ try:
141
+ result_data = await response.json()
142
+ except aiohttp.ContentTypeError:
143
+ result_data = await response.text()
144
+ print(result_data)
145
+ return result_data
146
+ except (asyncio.TimeoutError, aiohttp.ClientError, requests.exceptions.HTTPError) as e:
147
+ print(f"Error: {e}.")
148
+
149
+ if isinstance(e, (asyncio.TimeoutError, requests.exceptions.HTTPError)) and e.response.status in [502, 504]:
150
+ print("Skipping retries for this error.")
151
+ break
152
+
153
+ print("Retrying...")
154
+ await asyncio.sleep(1)
155
+ raise Exception("Max retries reached or skipping retries. Unable to make the request.")
156
+
157
+
158
+ async def make_request_old(session, token, data, url):
159
  headers = {
160
  'Content-Type': 'application/json',
161
  'Authorization': 'Bearer ' + token