Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,35 +15,42 @@ tokenizer, yi_coder_model, yi_coder_device = load_yi_coder_model()
|
|
| 15 |
# Conectar a Pinecone
|
| 16 |
index = connect_to_pinecone()
|
| 17 |
|
| 18 |
-
# Funci贸n para generar c贸digo
|
| 19 |
@gpu_decorator(duration=100)
|
| 20 |
def generate_code(system_prompt, user_prompt, max_length):
|
| 21 |
device = yi_coder_device
|
| 22 |
model = yi_coder_model
|
| 23 |
-
tokenizer_ = tokenizer
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
with torch.no_grad():
|
| 32 |
-
# Genera la respuesta
|
| 33 |
generated_ids = model.generate(
|
| 34 |
model_inputs.input_ids,
|
| 35 |
max_new_tokens=max_length,
|
| 36 |
-
eos_token_id=tokenizer_.eos_token_id
|
| 37 |
)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
response = generated_text[len(prompt):].strip()
|
| 44 |
|
|
|
|
| 45 |
return response
|
| 46 |
|
|
|
|
| 47 |
# Funci贸n para combinar b煤squeda vectorial y Yi-Coder
|
| 48 |
@gpu_decorator(duration=100)
|
| 49 |
def combined_function(user_prompt, similarity_threshold, selected_option, system_prompt, max_length):
|
|
|
|
| 15 |
# Conectar a Pinecone
|
| 16 |
index = connect_to_pinecone()
|
| 17 |
|
| 18 |
+
# Funci贸n para generar c贸digo utilizando Yi-Coder
|
| 19 |
@gpu_decorator(duration=100)
|
| 20 |
def generate_code(system_prompt, user_prompt, max_length):
|
| 21 |
device = yi_coder_device
|
| 22 |
model = yi_coder_model
|
| 23 |
+
tokenizer_ = tokenizer # Ya lo tenemos cargado
|
| 24 |
+
|
| 25 |
+
messages = [
|
| 26 |
+
{"role": "system", "content": system_prompt},
|
| 27 |
+
{"role": "user", "content": user_prompt}
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
# Aplicar la plantilla de chat y preparar el texto
|
| 31 |
+
text = tokenizer_.apply_chat_template(
|
| 32 |
+
messages,
|
| 33 |
+
tokenize=False,
|
| 34 |
+
add_generation_prompt=True
|
| 35 |
+
)
|
| 36 |
+
model_inputs = tokenizer_([text], return_tensors="pt").to(device)
|
| 37 |
|
| 38 |
with torch.no_grad():
|
|
|
|
| 39 |
generated_ids = model.generate(
|
| 40 |
model_inputs.input_ids,
|
| 41 |
max_new_tokens=max_length,
|
| 42 |
+
eos_token_id=tokenizer_.eos_token_id
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Extraer solo la parte generada
|
| 46 |
+
generated_ids = [
|
| 47 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 48 |
+
]
|
|
|
|
| 49 |
|
| 50 |
+
response = tokenizer_.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 51 |
return response
|
| 52 |
|
| 53 |
+
|
| 54 |
# Funci贸n para combinar b煤squeda vectorial y Yi-Coder
|
| 55 |
@gpu_decorator(duration=100)
|
| 56 |
def combined_function(user_prompt, similarity_threshold, selected_option, system_prompt, max_length):
|