Spaces:
Running
Running
Update main.py
Browse files
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 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
|
|
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 |
|