Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -211,6 +211,10 @@ 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:
|
|
@@ -223,8 +227,6 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
|
|
| 223 |
|
| 224 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 225 |
|
| 226 |
-
conversation = []
|
| 227 |
-
|
| 228 |
if "file_content" in message and message["file_content"]:
|
| 229 |
file_content = message["file_content"]
|
| 230 |
file_name = message["file_name"]
|
|
@@ -235,14 +237,15 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
|
|
| 235 |
choice, contents = mode_load(file_name)
|
| 236 |
|
| 237 |
if choice == "image":
|
| 238 |
-
|
| 239 |
elif choice == "doc":
|
| 240 |
-
|
| 241 |
-
conversation.append({"role": "user", "content": message['text']})
|
| 242 |
else:
|
| 243 |
-
|
| 244 |
|
| 245 |
-
|
|
|
|
|
|
|
| 246 |
|
| 247 |
generate_kwargs = dict(
|
| 248 |
max_length=max_length,
|
|
@@ -252,23 +255,35 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
|
|
| 252 |
temperature=temperature,
|
| 253 |
repetition_penalty=penalty,
|
| 254 |
eos_token_id=[151329, 151336, 151338],
|
|
|
|
| 255 |
)
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
print(" ")
|
| 265 |
-
print(
|
|
|
|
|
|
|
| 266 |
|
| 267 |
-
return PlainTextResponse(generated_text)
|
| 268 |
except Exception as e:
|
| 269 |
return PlainTextResponse(f"Error: {str(e)}")
|
| 270 |
|
| 271 |
|
|
|
|
| 272 |
# @spaces.GPU()
|
| 273 |
# 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):
|
| 274 |
# 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 |
+
from threading import Thread
|
| 215 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 216 |
+
import torch
|
| 217 |
+
|
| 218 |
@spaces.GPU()
|
| 219 |
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):
|
| 220 |
try:
|
|
|
|
| 227 |
|
| 228 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 229 |
|
|
|
|
|
|
|
| 230 |
if "file_content" in message and message["file_content"]:
|
| 231 |
file_content = message["file_content"]
|
| 232 |
file_name = message["file_name"]
|
|
|
|
| 237 |
choice, contents = mode_load(file_name)
|
| 238 |
|
| 239 |
if choice == "image":
|
| 240 |
+
input_text = message['text']
|
| 241 |
elif choice == "doc":
|
| 242 |
+
input_text = contents + "\n\n\n" + "{} files uploaded.\n".format(1) + message['text']
|
|
|
|
| 243 |
else:
|
| 244 |
+
input_text = message['text']
|
| 245 |
|
| 246 |
+
conversation = [{"role": "user", "content": input_text}]
|
| 247 |
+
input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
|
| 248 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 249 |
|
| 250 |
generate_kwargs = dict(
|
| 251 |
max_length=max_length,
|
|
|
|
| 255 |
temperature=temperature,
|
| 256 |
repetition_penalty=penalty,
|
| 257 |
eos_token_id=[151329, 151336, 151338],
|
| 258 |
+
streamer=streamer
|
| 259 |
)
|
| 260 |
|
| 261 |
+
def generate_text():
|
| 262 |
+
with torch.no_grad():
|
| 263 |
+
model.generate(input_ids['input_ids'], **generate_kwargs)
|
| 264 |
+
|
| 265 |
+
# Run generation in a separate thread
|
| 266 |
+
thread = Thread(target=generate_text)
|
| 267 |
+
thread.start()
|
| 268 |
|
| 269 |
+
# Collect generated text in real-time
|
| 270 |
+
buffer = ""
|
| 271 |
+
for new_text in streamer:
|
| 272 |
+
buffer += new_text
|
| 273 |
+
yield buffer
|
| 274 |
+
|
| 275 |
+
print(" ")
|
| 276 |
+
print("------")
|
| 277 |
print(" ")
|
| 278 |
+
print(buffer)
|
| 279 |
+
print(" ")
|
| 280 |
+
print("------")
|
| 281 |
|
|
|
|
| 282 |
except Exception as e:
|
| 283 |
return PlainTextResponse(f"Error: {str(e)}")
|
| 284 |
|
| 285 |
|
| 286 |
+
|
| 287 |
# @spaces.GPU()
|
| 288 |
# 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):
|
| 289 |
# try:
|