mateoluksenberg commited on
Commit
69669db
·
verified ·
1 Parent(s): 9c11fe7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -17
app.py CHANGED
@@ -211,7 +211,6 @@ EXAMPLES = [
211
  [{"text": "Quiero armar un JSON, solo el JSON sin texto, que contenga los datos de la primera mitad de la tabla de la imagen (las primeras 10 jurisdicciones 901-910). Ten en cuenta que los valores numéricos son decimales de cuatro dígitos. La tabla contiene las siguientes columnas: Codigo, Nombre, Fecha Inicio, Fecha Cese, Coeficiente Ingresos, Coeficiente Gastos y Coeficiente Unificado. La tabla puede contener valores vacíos, en ese caso dejarlos como null. Cada fila de la tabla representa una jurisdicción con sus respectivos valores.", }]
212
  ]
213
 
214
-
215
  @spaces.GPU()
216
  def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096, top_p: float = 1, top_k: int = 10, penalty: float = 1.0):
217
  try:
@@ -240,14 +239,12 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
240
  elif choice == "doc":
241
  message['text'] = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
242
  conversation.append({"role": "user", "content": message['text']})
243
- # format_msg = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
244
- # conversation.append({"role": "user", "content": format_msg})
245
  else:
246
  conversation.append({"role": "user", "content": message['text']})
247
 
248
- input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
249
-
250
- streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
251
 
252
  generate_kwargs = dict(
253
  max_length=max_length,
@@ -259,30 +256,92 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
259
  eos_token_id=[151329, 151336, 151338],
260
  )
261
 
262
- gen_kwargs = {**input_ids, **generate_kwargs}
263
-
264
- for entry in conversation:
265
- print(f"Role: {entry['role']}, Content: {entry.get('content', '')}")
266
-
267
  with torch.no_grad():
268
  generated_ids = model.generate(input_ids['input_ids'], **generate_kwargs)
269
  generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
270
 
271
- text_original = message['text'].strip()
272
- generated_text_cleaned = generated_text.replace(text_original, "").strip()
273
-
274
  print(" ")
275
  print("---------")
276
  print("Text: ")
277
  print(" ")
278
- print(generated_text_cleaned)
279
-
280
 
281
- return PlainTextResponse(generated_text_cleaned)
282
  except Exception as e:
283
  return PlainTextResponse(f"Error: {str(e)}")
284
 
285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
 
287
  # def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096, top_p: float = 1, top_k: int = 10, penalty: float = 1.0):
288
  # try:
 
211
  [{"text": "Quiero armar un JSON, solo el JSON sin texto, que contenga los datos de la primera mitad de la tabla de la imagen (las primeras 10 jurisdicciones 901-910). Ten en cuenta que los valores numéricos son decimales de cuatro dígitos. La tabla contiene las siguientes columnas: Codigo, Nombre, Fecha Inicio, Fecha Cese, Coeficiente Ingresos, Coeficiente Gastos y Coeficiente Unificado. La tabla puede contener valores vacíos, en ese caso dejarlos como null. Cada fila de la tabla representa una jurisdicción con sus respectivos valores.", }]
212
  ]
213
 
 
214
  @spaces.GPU()
215
  def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096, top_p: float = 1, top_k: int = 10, penalty: float = 1.0):
216
  try:
 
239
  elif choice == "doc":
240
  message['text'] = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
241
  conversation.append({"role": "user", "content": message['text']})
 
 
242
  else:
243
  conversation.append({"role": "user", "content": message['text']})
244
 
245
+ # Convierte solo el contenido relevante a IDs
246
+ # input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
247
+ input_ids = tokenizer(conversation[-1]['content'], return_tensors="pt").to(model.device)
248
 
249
  generate_kwargs = dict(
250
  max_length=max_length,
 
256
  eos_token_id=[151329, 151336, 151338],
257
  )
258
 
 
 
 
 
 
259
  with torch.no_grad():
260
  generated_ids = model.generate(input_ids['input_ids'], **generate_kwargs)
261
  generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
262
 
 
 
 
263
  print(" ")
264
  print("---------")
265
  print("Text: ")
266
  print(" ")
267
+ print(generated_text)
 
268
 
269
+ return PlainTextResponse(generated_text)
270
  except Exception as e:
271
  return PlainTextResponse(f"Error: {str(e)}")
272
 
273
 
274
+ # @spaces.GPU()
275
+ # def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096, top_p: float = 1, top_k: int = 10, penalty: float = 1.0):
276
+ # try:
277
+ # model = AutoModelForCausalLM.from_pretrained(
278
+ # MODEL_ID,
279
+ # torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
280
+ # low_cpu_mem_usage=True,
281
+ # trust_remote_code=True
282
+ # )
283
+
284
+ # tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
285
+
286
+ # conversation = []
287
+
288
+ # if "file_content" in message and message["file_content"]:
289
+ # file_content = message["file_content"]
290
+ # file_name = message["file_name"]
291
+
292
+ # with open(file_name, "wb") as f:
293
+ # f.write(file_content.read())
294
+
295
+ # choice, contents = mode_load(file_name)
296
+
297
+ # if choice == "image":
298
+ # conversation.append({"role": "user", "image": contents, "content": message['text']})
299
+ # elif choice == "doc":
300
+ # message['text'] = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
301
+ # conversation.append({"role": "user", "content": message['text']})
302
+ # # format_msg = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
303
+ # # conversation.append({"role": "user", "content": format_msg})
304
+ # else:
305
+ # conversation.append({"role": "user", "content": message['text']})
306
+
307
+ # input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
308
+
309
+ # streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
310
+
311
+ # generate_kwargs = dict(
312
+ # max_length=max_length,
313
+ # do_sample=True,
314
+ # top_p=top_p,
315
+ # top_k=top_k,
316
+ # temperature=temperature,
317
+ # repetition_penalty=penalty,
318
+ # eos_token_id=[151329, 151336, 151338],
319
+ # )
320
+
321
+ # gen_kwargs = {**input_ids, **generate_kwargs}
322
+
323
+ # for entry in conversation:
324
+ # print(f"Role: {entry['role']}, Content: {entry.get('content', '')}")
325
+
326
+ # with torch.no_grad():
327
+ # generated_ids = model.generate(input_ids['input_ids'], **generate_kwargs)
328
+ # generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
329
+
330
+ # text_original = message['text'].strip()
331
+ # generated_text_cleaned = generated_text.replace(text_original, "").strip()
332
+
333
+ # print(" ")
334
+ # print("---------")
335
+ # print("Text: ")
336
+ # print(" ")
337
+ # print(generated_text_cleaned)
338
+
339
+
340
+ # return PlainTextResponse(generated_text_cleaned)
341
+ # except Exception as e:
342
+ # return PlainTextResponse(f"Error: {str(e)}")
343
+
344
+
345
 
346
  # def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096, top_p: float = 1, top_k: int = 10, penalty: float = 1.0):
347
  # try: