Spaces:
Build error
Build error
Update main.py
Browse files160 unmodified lines16116216316416416516616716816917017117217317417517617717817918018118218318418518618718818916516616716816917017117217317417517617717817918018118218318418518618718818919019119219394 unmodified lines288289290291292293294295296295297298299300160 unmodified lines result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", input_data)) return {"response": result_data}
async def make_request(session, token, data, url, index, semaphore, max_retries=3):async def make_request(session, token, data, url, index, max_retries=3): headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } if (int(index)+1) % 3 == 1: data['max_new_tokens'] = data['max_new_tokens'] elif (int(index)+1) % 3 == 2: data['max_new_tokens'] = max(200, data['max_new_tokens'] - 200) else: data['max_new_tokens'] = data['max_new_tokens'] + 200 for _ in range(max_retries): try: async with session.post(url, headers=headers, json=data) as response: response.raise_for_status() try: result_data = await response.json() except aiohttp.ContentTypeError: result_data = await response.text() return result_data except (asyncio.TimeoutError, aiohttp.ClientError, requests.exceptions.HTTPError) as e: LoggaTesto("ERRORE ASYNC", {e}, False) if isinstance(e, (asyncio.TimeoutError, requests.exceptions.HTTPError)) and e.response.status in [502, 504]: break await asyncio.sleep(3) raise Exception("Max retries reached or skipping retries. Unable to make the request.") async with semaphore: headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } if (int(index)+1) % 3 == 1: data['max_new_tokens'] = data['max_new_tokens'] elif (int(index)+1) % 3 == 2: data['max_new_tokens'] = max(200, data['max_new_tokens'] - 200) else: data['max_new_tokens'] = data['max_new_tokens'] + 200 for _ in range(max_retries): try: async with session.post(url, headers=headers, json=data) as response: response.raise_for_status() try: result_data = await response.json() except aiohttp.ContentTypeError: result_data = await response.text() return result_data except (asyncio.TimeoutError, aiohttp.ClientError, requests.exceptions.HTTPError) as e: LoggaTesto("ERRORE ASYNC", {e}, False) if isinstance(e, (asyncio.TimeoutError, requests.exceptions.HTTPError)) and e.response.status in [502, 504]: break await asyncio.sleep(3) raise Exception("Max retries reached or skipping retries. Unable to make the request.")
async def CreaListaInput(input_data): if input_data.instruction.startswith("http"):94 unmodified lines
async def GeneraTestoAsync(url, input_data): token = os.getenv('TOKEN') semaphore = asyncio.Semaphore(20) async with aiohttp.ClientSession() as session: tasks = [] ListaInput = await CreaListaInput(input_data) for data in ListaInput: LoggaTesto("RICHIESTA ASINCRONA", data) tasks.extend([make_request(session, token, data, url, index) for index in range(input_data.NumeroGenerazioni)]) tasks.extend([make_request(session, token, data, url, index, semaphore) for index in range(input_data.NumeroGenerazioni)]) await asyncio.sleep(0.1) return await asyncio.gather(*tasks)
main.py+29-27