MatteoScript commited on
Commit
7ead637
·
verified ·
1 Parent(s): 6a762d0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +29 -27
main.py CHANGED
@@ -161,32 +161,33 @@ def generate_textAsync(request: Request, input_data: InputDataAsync):
161
  result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", input_data))
162
  return {"response": result_data}
163
 
164
- async def make_request(session, token, data, url, index, max_retries=3):
165
- headers = {
166
- 'Content-Type': 'application/json',
167
- 'Authorization': 'Bearer ' + token
168
- }
169
- if (int(index)+1) % 3 == 1:
170
- data['max_new_tokens'] = data['max_new_tokens']
171
- elif (int(index)+1) % 3 == 2:
172
- data['max_new_tokens'] = max(200, data['max_new_tokens'] - 200)
173
- else:
174
- data['max_new_tokens'] = data['max_new_tokens'] + 200
175
- for _ in range(max_retries):
176
- try:
177
- async with session.post(url, headers=headers, json=data) as response:
178
- response.raise_for_status()
179
- try:
180
- result_data = await response.json()
181
- except aiohttp.ContentTypeError:
182
- result_data = await response.text()
183
- return result_data
184
- except (asyncio.TimeoutError, aiohttp.ClientError, requests.exceptions.HTTPError) as e:
185
- LoggaTesto("ERRORE ASYNC", {e}, False)
186
- if isinstance(e, (asyncio.TimeoutError, requests.exceptions.HTTPError)) and e.response.status in [502, 504]:
187
- break
188
- await asyncio.sleep(3)
189
- raise Exception("Max retries reached or skipping retries. Unable to make the request.")
 
190
 
191
  async def CreaListaInput(input_data):
192
  if input_data.instruction.startswith("http"):
@@ -287,12 +288,13 @@ def DividiInstructionText(input_data):
287
 
288
  async def GeneraTestoAsync(url, input_data):
289
  token = os.getenv('TOKEN')
 
290
  async with aiohttp.ClientSession() as session:
291
  tasks = []
292
  ListaInput = await CreaListaInput(input_data)
293
  for data in ListaInput:
294
  LoggaTesto("RICHIESTA ASINCRONA", data)
295
- tasks.extend([make_request(session, token, data, url, index) for index in range(input_data.NumeroGenerazioni)])
296
  await asyncio.sleep(0.1)
297
  return await asyncio.gather(*tasks)
298
 
 
161
  result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", input_data))
162
  return {"response": result_data}
163
 
164
+ async def make_request(session, token, data, url, index, semaphore, max_retries=3):
165
+ async with semaphore:
166
+ headers = {
167
+ 'Content-Type': 'application/json',
168
+ 'Authorization': 'Bearer ' + token
169
+ }
170
+ if (int(index)+1) % 3 == 1:
171
+ data['max_new_tokens'] = data['max_new_tokens']
172
+ elif (int(index)+1) % 3 == 2:
173
+ data['max_new_tokens'] = max(200, data['max_new_tokens'] - 200)
174
+ else:
175
+ data['max_new_tokens'] = data['max_new_tokens'] + 200
176
+ for _ in range(max_retries):
177
+ try:
178
+ async with session.post(url, headers=headers, json=data) as response:
179
+ response.raise_for_status()
180
+ try:
181
+ result_data = await response.json()
182
+ except aiohttp.ContentTypeError:
183
+ result_data = await response.text()
184
+ return result_data
185
+ except (asyncio.TimeoutError, aiohttp.ClientError, requests.exceptions.HTTPError) as e:
186
+ LoggaTesto("ERRORE ASYNC", {e}, False)
187
+ if isinstance(e, (asyncio.TimeoutError, requests.exceptions.HTTPError)) and e.response.status in [502, 504]:
188
+ break
189
+ await asyncio.sleep(3)
190
+ raise Exception("Max retries reached or skipping retries. Unable to make the request.")
191
 
192
  async def CreaListaInput(input_data):
193
  if input_data.instruction.startswith("http"):
 
288
 
289
  async def GeneraTestoAsync(url, input_data):
290
  token = os.getenv('TOKEN')
291
+ semaphore = asyncio.Semaphore(20)
292
  async with aiohttp.ClientSession() as session:
293
  tasks = []
294
  ListaInput = await CreaListaInput(input_data)
295
  for data in ListaInput:
296
  LoggaTesto("RICHIESTA ASINCRONA", data)
297
+ tasks.extend([make_request(session, token, data, url, index, semaphore) for index in range(input_data.NumeroGenerazioni)])
298
  await asyncio.sleep(0.1)
299
  return await asyncio.gather(*tasks)
300