mateoluksenberg commited on
Commit
4986999
·
verified ·
1 Parent(s): 803aa5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -93
app.py CHANGED
@@ -211,77 +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
- @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:
217
- model = AutoModelForCausalLM.from_pretrained(
218
- MODEL_ID,
219
- torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
220
- low_cpu_mem_usage=True,
221
- trust_remote_code=True
222
- )
223
-
224
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
225
-
226
- if "file_content" in message and message["file_content"]:
227
- file_content = message["file_content"]
228
- file_name = message["file_name"]
229
-
230
- with open(file_name, "wb") as f:
231
- f.write(file_content.read())
232
-
233
- choice, contents = mode_load(file_name)
234
-
235
- if choice == "image":
236
- #input_text = message['text']
237
- conversation.append({"role": "user", "image": contents, "content": message['text']})
238
- elif choice == "doc":
239
- #input_text = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
240
- format_msg = contents + "\n\n\n" + "{} files uploaded.\n" + message['text']
241
- conversation.append({"role": "user", "content": format_msg})
242
- else:
243
- #input_text = message['text']
244
- contents = None
245
- conversation.append({"role": "user", "content": message['text']})
246
-
247
- # conversation = [{"role": "user", "content": input_text}]
248
- input_ids = tokenizer(conversation[-1]['content'], return_tensors="pt").to(model.device)
249
-
250
- generate_kwargs = dict(
251
- max_length=max_length,
252
- do_sample=True,
253
- top_p=top_p,
254
- top_k=top_k,
255
- temperature=temperature,
256
- repetition_penalty=penalty,
257
- eos_token_id=[151329, 151336, 151338]
258
- )
259
-
260
- with torch.no_grad():
261
- generated_ids = model.generate(input_ids['input_ids'], **generate_kwargs)
262
- generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
263
-
264
- # Process to remove any prefix or unwanted prompt
265
- # text_original = input_text.strip()
266
- # results_text = generated_text[len(text_original):].strip()
267
-
268
- text_original = message['text'].strip()
269
- results_text = generated_text.replace(text_original, "").strip()
270
-
271
- print(" ")
272
- print("------")
273
- print(" ")
274
- print(generated_text)
275
- print(" ")
276
- print("------")
277
-
278
- return PlainTextResponse(results_text)
279
- except Exception as e:
280
- return PlainTextResponse(f"Error: {str(e)}")
281
-
282
-
283
-
284
-
285
  # @spaces.GPU()
286
  # 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):
287
  # try:
@@ -294,8 +223,6 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
294
 
295
  # tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
296
 
297
- # conversation = []
298
-
299
  # if "file_content" in message and message["file_content"]:
300
  # file_content = message["file_content"]
301
  # file_name = message["file_name"]
@@ -306,18 +233,19 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
306
  # choice, contents = mode_load(file_name)
307
 
308
  # if choice == "image":
 
309
  # conversation.append({"role": "user", "image": contents, "content": message['text']})
310
  # elif choice == "doc":
311
- # message['text'] = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
312
- # conversation.append({"role": "user", "content": message['text']})
313
- # # format_msg = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
314
- # # conversation.append({"role": "user", "content": format_msg})
315
  # else:
 
 
316
  # conversation.append({"role": "user", "content": message['text']})
317
 
318
- # input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
319
-
320
- # streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
321
 
322
  # generate_kwargs = dict(
323
  # max_length=max_length,
@@ -326,34 +254,106 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
326
  # top_k=top_k,
327
  # temperature=temperature,
328
  # repetition_penalty=penalty,
329
- # eos_token_id=[151329, 151336, 151338],
330
  # )
331
 
332
- # gen_kwargs = {**input_ids, **generate_kwargs}
333
-
334
- # for entry in conversation:
335
- # print(f"Role: {entry['role']}, Content: {entry.get('content', '')}")
336
-
337
  # with torch.no_grad():
338
  # generated_ids = model.generate(input_ids['input_ids'], **generate_kwargs)
339
  # generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
340
 
 
 
 
 
341
  # text_original = message['text'].strip()
342
- # generated_text_cleaned = generated_text.replace(text_original, "").strip()
343
 
344
- # print(" ")
345
- # print("---------")
346
- # print("Text: ")
347
  # print(" ")
348
- # print(generated_text_cleaned)
349
-
 
 
 
350
 
351
- # return PlainTextResponse(generated_text_cleaned)
352
  # except Exception as e:
353
  # return PlainTextResponse(f"Error: {str(e)}")
354
 
355
 
356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
  @app.post("/chat/")
358
  async def test_endpoint(
359
  text: str = Form(...),
 
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:
 
223
 
224
  # tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
225
 
 
 
226
  # if "file_content" in message and message["file_content"]:
227
  # file_content = message["file_content"]
228
  # file_name = message["file_name"]
 
233
  # choice, contents = mode_load(file_name)
234
 
235
  # if choice == "image":
236
+ # #input_text = message['text']
237
  # conversation.append({"role": "user", "image": contents, "content": message['text']})
238
  # elif choice == "doc":
239
+ # #input_text = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
240
+ # format_msg = contents + "\n\n\n" + "{} files uploaded.\n" + message['text']
241
+ # conversation.append({"role": "user", "content": format_msg})
 
242
  # else:
243
+ # #input_text = message['text']
244
+ # contents = None
245
  # conversation.append({"role": "user", "content": message['text']})
246
 
247
+ # # conversation = [{"role": "user", "content": input_text}]
248
+ # input_ids = tokenizer(conversation[-1]['content'], return_tensors="pt").to(model.device)
 
249
 
250
  # generate_kwargs = dict(
251
  # max_length=max_length,
 
254
  # top_k=top_k,
255
  # temperature=temperature,
256
  # repetition_penalty=penalty,
257
+ # eos_token_id=[151329, 151336, 151338]
258
  # )
259
 
 
 
 
 
 
260
  # with torch.no_grad():
261
  # generated_ids = model.generate(input_ids['input_ids'], **generate_kwargs)
262
  # generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
263
 
264
+ # # Process to remove any prefix or unwanted prompt
265
+ # # text_original = input_text.strip()
266
+ # # results_text = generated_text[len(text_original):].strip()
267
+
268
  # text_original = message['text'].strip()
269
+ # results_text = generated_text.replace(text_original, "").strip()
270
 
 
 
 
271
  # print(" ")
272
+ # print("------")
273
+ # print(" ")
274
+ # print(generated_text)
275
+ # print(" ")
276
+ # print("------")
277
 
278
+ # return PlainTextResponse(results_text)
279
  # except Exception as e:
280
  # return PlainTextResponse(f"Error: {str(e)}")
281
 
282
 
283
 
284
+
285
+ @spaces.GPU()
286
+ 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):
287
+ try:
288
+ model = AutoModelForCausalLM.from_pretrained(
289
+ MODEL_ID,
290
+ torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
291
+ low_cpu_mem_usage=True,
292
+ trust_remote_code=True
293
+ )
294
+
295
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
296
+
297
+ conversation = []
298
+
299
+ if "file_content" in message and message["file_content"]:
300
+ file_content = message["file_content"]
301
+ file_name = message["file_name"]
302
+
303
+ with open(file_name, "wb") as f:
304
+ f.write(file_content.read())
305
+
306
+ choice, contents = mode_load(file_name)
307
+
308
+ if choice == "image":
309
+ conversation.append({"role": "user", "image": contents, "content": message['text']})
310
+ elif choice == "doc":
311
+ message['text'] = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
312
+ conversation.append({"role": "user", "content": message['text']})
313
+ # format_msg = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
314
+ # conversation.append({"role": "user", "content": format_msg})
315
+ else:
316
+ conversation.append({"role": "user", "content": message['text']})
317
+
318
+ input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
319
+
320
+ streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
321
+
322
+ generate_kwargs = dict(
323
+ max_length=max_length,
324
+ do_sample=True,
325
+ top_p=top_p,
326
+ top_k=top_k,
327
+ temperature=temperature,
328
+ repetition_penalty=penalty,
329
+ eos_token_id=[151329, 151336, 151338],
330
+ )
331
+
332
+ gen_kwargs = {**input_ids, **generate_kwargs}
333
+
334
+ for entry in conversation:
335
+ print(f"Role: {entry['role']}, Content: {entry.get('content', '')}")
336
+
337
+ with torch.no_grad():
338
+ generated_ids = model.generate(input_ids['input_ids'], **generate_kwargs)
339
+ generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
340
+
341
+ text_original = message['text'].strip()
342
+ generated_text_cleaned = generated_text.replace(text_original, "").strip()
343
+
344
+ print(" ")
345
+ print("---------")
346
+ print("Text: ")
347
+ print(" ")
348
+ print(generated_text_cleaned)
349
+
350
+
351
+ return PlainTextResponse(generated_text_cleaned)
352
+ except Exception as e:
353
+ return PlainTextResponse(f"Error: {str(e)}")
354
+
355
+
356
+
357
  @app.post("/chat/")
358
  async def test_endpoint(
359
  text: str = Form(...),