Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -139,10 +139,95 @@ async def make_request(session, token, data, url):
|
|
| 139 |
print(result_data)
|
| 140 |
return result_data
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
async def GeneraTestoAsync(url, input_data):
|
| 143 |
token = os.getenv('TOKEN')
|
| 144 |
async with aiohttp.ClientSession() as session:
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
tasks = []
|
| 147 |
for input_string in input_strings:
|
| 148 |
data = {
|
|
|
|
| 139 |
print(result_data)
|
| 140 |
return result_data
|
| 141 |
|
| 142 |
+
async def CreaListaInput(input_data):
|
| 143 |
+
ListaInput = []
|
| 144 |
+
input_strings = input_data.input.split(input_data.StringaSplit)
|
| 145 |
+
numerocaratteriSPLIT = 2000
|
| 146 |
+
if input_data.instruction.startswith("http"):
|
| 147 |
+
try:
|
| 148 |
+
resp = requests.get(input_data.instruction)
|
| 149 |
+
resp.raise_for_status()
|
| 150 |
+
input_data.instruction = resp.text
|
| 151 |
+
except requests.exceptions.RequestException as e:
|
| 152 |
+
input_data.instruction = ""
|
| 153 |
+
try:
|
| 154 |
+
lista_dizionari = json.loads(input_data.instruction)
|
| 155 |
+
nuova_lista_dizionari = []
|
| 156 |
+
if lista_dizionari and "Titolo" in lista_dizionari[0]:
|
| 157 |
+
DividiInstructionJSON()
|
| 158 |
+
else:
|
| 159 |
+
DividiInstructionText()
|
| 160 |
+
except json.JSONDecodeError:
|
| 161 |
+
DividiInstructionText()
|
| 162 |
+
return ListaInput
|
| 163 |
+
|
| 164 |
+
async def split_at_space_or_dot(input_string, length):
|
| 165 |
+
delimiters = ['\n\n', '.\n', ';\n', '.', ' ']
|
| 166 |
+
lastpos = max(input_string.rfind(d, 0, length) for d in delimiters)
|
| 167 |
+
indice_divisione = lastpos if lastpos >= 0 else length
|
| 168 |
+
return indice_divisione + 1
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
async def DividiInstructionJSON():
|
| 172 |
+
for dizionario in lista_dizionari:
|
| 173 |
+
titolo = dizionario["Titolo"]
|
| 174 |
+
testo_completo = dizionario["Testo"]
|
| 175 |
+
while len(testo_completo) > numerocaratteriSPLIT:
|
| 176 |
+
indice_divisione = split_at_space_or_dot(
|
| 177 |
+
testo_completo, numerocaratteriSPLIT)
|
| 178 |
+
indice_divisione_precedente = split_at_space_or_dot(
|
| 179 |
+
testo_completo, numerocaratteriSPLIT-100)
|
| 180 |
+
sottostringa = testo_completo[:indice_divisione].strip()
|
| 181 |
+
testo_completo = testo_completo[indice_divisione_precedente:].strip(
|
| 182 |
+
)
|
| 183 |
+
nuovo_dizionario = {"Titolo": titolo, "Testo": sottostringa}
|
| 184 |
+
nuova_lista_dizionari.append(nuovo_dizionario)
|
| 185 |
+
|
| 186 |
+
if len(testo_completo) > 0:
|
| 187 |
+
nuovo_dizionario = {"Titolo": titolo, "Testo": testo_completo}
|
| 188 |
+
nuova_lista_dizionari.append(nuovo_dizionario)
|
| 189 |
+
|
| 190 |
+
for input_string in input_strings:
|
| 191 |
+
for dizionario in nuova_lista_dizionari:
|
| 192 |
+
data = {
|
| 193 |
+
'input': input_string,
|
| 194 |
+
'instruction': [dizionario]
|
| 195 |
+
}
|
| 196 |
+
ListaInput.append(data)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
async def DividiInstructionText():
|
| 200 |
+
input_str = instruction
|
| 201 |
+
sottostringhe = []
|
| 202 |
+
indice_inizio = 0
|
| 203 |
+
if len(input_str) > numerocaratteriSPLIT:
|
| 204 |
+
while indice_inizio < len(input_str):
|
| 205 |
+
lunghezza_sottostringa = split_at_space_or_dot(
|
| 206 |
+
input_str[indice_inizio:], numerocaratteriSPLIT)
|
| 207 |
+
sottostringhe.append(
|
| 208 |
+
input_str[indice_inizio:indice_inizio + lunghezza_sottostringa].strip())
|
| 209 |
+
indice_inizio += lunghezza_sottostringa
|
| 210 |
+
else:
|
| 211 |
+
sottostringhe.append(input_str)
|
| 212 |
+
testoSeparato = StringaSplit.join(sottostringhe)
|
| 213 |
+
instruction_strings = testoSeparato.split(StringaSplit)
|
| 214 |
+
for input_string in input_strings:
|
| 215 |
+
for instruction_string in instruction_strings:
|
| 216 |
+
data = {
|
| 217 |
+
'input': input_string.strip(),
|
| 218 |
+
'instruction': [{instruction_string.strip()}]
|
| 219 |
+
}
|
| 220 |
+
ListaInput.append(data)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
async def GeneraTestoAsync(url, input_data):
|
| 224 |
token = os.getenv('TOKEN')
|
| 225 |
async with aiohttp.ClientSession() as session:
|
| 226 |
+
ListaInput = CreaListaInput(input_data)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
|
| 231 |
tasks = []
|
| 232 |
for input_string in input_strings:
|
| 233 |
data = {
|