Spaces:
Build error
Build error
Update main.py
Browse files160 unmodified lines16116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119294 unmodified lines287288289290291292293294295296297298160 unmodified linesresult_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, 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'] + 200for _ 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_dataexcept (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]:breakawait 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 linesasync def GeneraTestoAsync(url, input_data):token = os.getenv('TOKEN')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)])await asyncio.sleep(0.1)return await asyncio.gather(*tasks)160 unmodified lines16116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219394 unmodified lines288289290291292293294295296297298299300160 unmodified linesresult_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 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'] + 200for _ 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_dataexcept (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]:breakawait 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 linesasync 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, semaphore) for index in range(input_data.NumeroGenerazioni)])await asyncio.sleep(0.1)return await asyncio.gather(*tasks)
main.py+29-27