Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -36,32 +36,42 @@ class InputData(BaseModel):
|
|
36 |
max_new_tokens: int = 2000
|
37 |
top_p: float = 0.95
|
38 |
repetition_penalty: float = 1.0
|
39 |
-
|
40 |
-
class InputDataAsync(InputData):
|
41 |
NumeroGenerazioni: int = 1
|
42 |
StringaSplit: str = '********'
|
43 |
NumeroCaratteriSplitInstruction: int = 30000
|
|
|
|
|
44 |
|
45 |
class PostSpazio(BaseModel):
|
46 |
nomeSpazio: str
|
47 |
input: str = ''
|
48 |
api_name: str = "/chat"
|
|
|
|
|
|
|
49 |
|
50 |
#--------------------------------------------------- Generazione TESTO ------------------------------------------------------
|
51 |
@app.post("/Genera")
|
52 |
def generate_text(request: Request, input_data: InputData):
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
def generate_input_text(input_data):
|
66 |
if input_data.instruction.startswith("http"):
|
67 |
try:
|
@@ -124,7 +134,6 @@ def format_prompt(message, history):
|
|
124 |
#--------------------------------------------------- Generazione TESTO ASYNC ------------------------------------------------------
|
125 |
@app.post("/GeneraAsync")
|
126 |
def generate_textAsync(request: Request, input_data: InputDataAsync):
|
127 |
-
print(input_data.input)
|
128 |
result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", input_data))
|
129 |
return {"response": result_data}
|
130 |
|
@@ -144,13 +153,10 @@ async def make_request(session, token, data, url, max_retries=3):
|
|
144 |
print(result_data)
|
145 |
return result_data
|
146 |
except (asyncio.TimeoutError, aiohttp.ClientError, requests.exceptions.HTTPError) as e:
|
147 |
-
|
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 |
|
@@ -165,7 +171,6 @@ async def make_request_old(session, token, data, url):
|
|
165 |
result_data = await response.json()
|
166 |
except aiohttp.ContentTypeError:
|
167 |
result_data = await response.text()
|
168 |
-
print(result_data)
|
169 |
return result_data
|
170 |
|
171 |
async def CreaListaInput(input_data):
|
@@ -176,7 +181,6 @@ async def CreaListaInput(input_data):
|
|
176 |
input_data.instruction = resp.text
|
177 |
except requests.exceptions.RequestException as e:
|
178 |
input_data.instruction = ""
|
179 |
-
print(input_data.instruction)
|
180 |
try:
|
181 |
lista_dizionari = []
|
182 |
nuova_lista_dizionari = []
|
@@ -187,7 +191,6 @@ async def CreaListaInput(input_data):
|
|
187 |
nuova_lista_dizionari = DividiInstructionText(input_data)
|
188 |
except json.JSONDecodeError:
|
189 |
nuova_lista_dizionari = DividiInstructionText(input_data)
|
190 |
-
print(nuova_lista_dizionari)
|
191 |
return nuova_lista_dizionari
|
192 |
|
193 |
def split_at_space_or_dot(input_string, length):
|
@@ -196,7 +199,6 @@ def split_at_space_or_dot(input_string, length):
|
|
196 |
valid_positions = [pos for pos in positions if pos >= 0]
|
197 |
lastpos = max(valid_positions) if valid_positions else length
|
198 |
indice_divisione = int(lastpos)
|
199 |
-
print(indice_divisione)
|
200 |
return indice_divisione + 1
|
201 |
|
202 |
def DividiInstructionJSON(lista_dizionari, input_data):
|
@@ -271,8 +273,7 @@ async def GeneraTestoAsync(url, input_data):
|
|
271 |
tasks = []
|
272 |
ListaInput = await CreaListaInput(input_data)
|
273 |
for data in ListaInput:
|
274 |
-
|
275 |
-
print(data)
|
276 |
tasks.extend([make_request(session, token, data, url) for _ in range(input_data.NumeroGenerazioni)])
|
277 |
return await asyncio.gather(*tasks)
|
278 |
|
|
|
36 |
max_new_tokens: int = 2000
|
37 |
top_p: float = 0.95
|
38 |
repetition_penalty: float = 1.0
|
39 |
+
Async: bool = false
|
|
|
40 |
NumeroGenerazioni: int = 1
|
41 |
StringaSplit: str = '********'
|
42 |
NumeroCaratteriSplitInstruction: int = 30000
|
43 |
+
|
44 |
+
class InputDataAsync(InputData):
|
45 |
|
46 |
class PostSpazio(BaseModel):
|
47 |
nomeSpazio: str
|
48 |
input: str = ''
|
49 |
api_name: str = "/chat"
|
50 |
+
|
51 |
+
def LoggaTesto(type, text)
|
52 |
+
print(f"{datetime.now()}: ----------------------------------| {type} |-----------------------------------\n{text}\n\n")
|
53 |
|
54 |
#--------------------------------------------------- Generazione TESTO ------------------------------------------------------
|
55 |
@app.post("/Genera")
|
56 |
def generate_text(request: Request, input_data: InputData):
|
57 |
+
LoggaTesto("INPUT", input_data.input)
|
58 |
+
if not input_data.Async:
|
59 |
+
temperature = input_data.temperature
|
60 |
+
max_new_tokens = input_data.max_new_tokens
|
61 |
+
top_p = input_data.top_p
|
62 |
+
repetition_penalty = input_data.repetition_penalty
|
63 |
+
input_text = generate_input_text(input_data)
|
64 |
+
max_new_tokens = min(max_new_tokens, 29500 - len(input_text))
|
65 |
+
history = []
|
66 |
+
generated_response = generate(input_text, history, temperature, max_new_tokens, top_p, repetition_penalty)
|
67 |
+
LoggaTesto("RISPOSTA SINCRONA", generated_response)
|
68 |
+
return {"response": generated_response}
|
69 |
+
else:
|
70 |
+
input_data.Async = false
|
71 |
+
result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", input_data))
|
72 |
+
LoggaTesto("RISPOSTA ASINCRONA FINALE", result_data)
|
73 |
+
return {"response": result_data}
|
74 |
+
|
75 |
def generate_input_text(input_data):
|
76 |
if input_data.instruction.startswith("http"):
|
77 |
try:
|
|
|
134 |
#--------------------------------------------------- Generazione TESTO ASYNC ------------------------------------------------------
|
135 |
@app.post("/GeneraAsync")
|
136 |
def generate_textAsync(request: Request, input_data: InputDataAsync):
|
|
|
137 |
result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", input_data))
|
138 |
return {"response": result_data}
|
139 |
|
|
|
153 |
print(result_data)
|
154 |
return result_data
|
155 |
except (asyncio.TimeoutError, aiohttp.ClientError, requests.exceptions.HTTPError) as e:
|
156 |
+
LoggaTesto("ERRORE ASYNC", {e})
|
|
|
157 |
if isinstance(e, (asyncio.TimeoutError, requests.exceptions.HTTPError)) and e.response.status in [502, 504]:
|
|
|
158 |
break
|
159 |
|
|
|
160 |
await asyncio.sleep(1)
|
161 |
raise Exception("Max retries reached or skipping retries. Unable to make the request.")
|
162 |
|
|
|
171 |
result_data = await response.json()
|
172 |
except aiohttp.ContentTypeError:
|
173 |
result_data = await response.text()
|
|
|
174 |
return result_data
|
175 |
|
176 |
async def CreaListaInput(input_data):
|
|
|
181 |
input_data.instruction = resp.text
|
182 |
except requests.exceptions.RequestException as e:
|
183 |
input_data.instruction = ""
|
|
|
184 |
try:
|
185 |
lista_dizionari = []
|
186 |
nuova_lista_dizionari = []
|
|
|
191 |
nuova_lista_dizionari = DividiInstructionText(input_data)
|
192 |
except json.JSONDecodeError:
|
193 |
nuova_lista_dizionari = DividiInstructionText(input_data)
|
|
|
194 |
return nuova_lista_dizionari
|
195 |
|
196 |
def split_at_space_or_dot(input_string, length):
|
|
|
199 |
valid_positions = [pos for pos in positions if pos >= 0]
|
200 |
lastpos = max(valid_positions) if valid_positions else length
|
201 |
indice_divisione = int(lastpos)
|
|
|
202 |
return indice_divisione + 1
|
203 |
|
204 |
def DividiInstructionJSON(lista_dizionari, input_data):
|
|
|
273 |
tasks = []
|
274 |
ListaInput = await CreaListaInput(input_data)
|
275 |
for data in ListaInput:
|
276 |
+
LoggaTesto("RICHIESTA ASINCRONA", data)
|
|
|
277 |
tasks.extend([make_request(session, token, data, url) for _ in range(input_data.NumeroGenerazioni)])
|
278 |
return await asyncio.gather(*tasks)
|
279 |
|