C2MV commited on
Commit
b0f15d6
verified
1 Parent(s): a5e45f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -26
app.py CHANGED
@@ -3,20 +3,18 @@
3
  import gradio as gr
4
  import os
5
  from models import load_embedding_model, load_yi_coder_model
6
- from pinecone_utils import connect_to_pinecone, vector_search # Ahora deber铆a funcionar correctamente
7
  from ui import build_interface
8
  from config import SIMILARITY_THRESHOLD_DEFAULT, SYSTEM_PROMPT, MAX_LENGTH_DEFAULT
9
  from decorators import gpu_decorator
10
  import torch
11
 
12
  ########################
13
-
14
  from utils import process_tags_chat
15
-
16
  ########################
17
 
18
-
19
  # Cargar modelos
 
20
  embedding_model = load_embedding_model()
21
  tokenizer, yi_coder_model, yi_coder_device = load_yi_coder_model()
22
 
@@ -26,21 +24,22 @@ index = connect_to_pinecone()
26
  # Funci贸n para generar c贸digo utilizando Yi-Coder
27
  @gpu_decorator(duration=100)
28
  def generate_code(system_prompt, user_prompt, max_length):
 
29
  device = yi_coder_device
30
  model = yi_coder_model
31
- tokenizer_ = tokenizer # Ya lo tenemos cargado
32
 
33
  messages = [
34
  {"role": "system", "content": system_prompt},
35
  {"role": "user", "content": user_prompt}
36
  ]
37
 
38
- # Aplicar la plantilla de chat y preparar el texto
39
  text = tokenizer_.apply_chat_template(
40
  messages,
41
  tokenize=False,
42
  add_generation_prompt=True
43
  )
 
44
  model_inputs = tokenizer_([text], return_tensors="pt").to(device)
45
 
46
  with torch.no_grad():
@@ -50,7 +49,6 @@ def generate_code(system_prompt, user_prompt, max_length):
50
  eos_token_id=tokenizer_.eos_token_id
51
  )
52
 
53
- # Extraer solo la parte generada
54
  generated_ids = [
55
  output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
56
  ]
@@ -58,19 +56,16 @@ def generate_code(system_prompt, user_prompt, max_length):
58
  response = tokenizer_.batch_decode(generated_ids, skip_special_tokens=True)[0]
59
  return response
60
 
61
-
62
  # Funci贸n para combinar b煤squeda vectorial y Yi-Coder
63
  @gpu_decorator(duration=100)
64
  def combined_function(user_prompt, similarity_threshold, selected_option, system_prompt, max_length):
65
  def get_partial_message(response):
66
- """Obtiene el contenido despu茅s de 'Respuesta:' si est谩 presente en la respuesta."""
67
  if "Respuesta:" in response:
68
- return response.split("Respuesta:")[1].strip() # Tomar solo el texto despu茅s de 'Respuesta:'
69
  else:
70
- return response # Devolver la respuesta completa si no contiene 'Respuesta:'
71
 
72
  if selected_option == "Solo B煤squeda Vectorial":
73
- # Realizar b煤squeda vectorial
74
  search_results = vector_search(user_prompt, embedding_model, index)
75
  if search_results:
76
  content = search_results[0]['content']
@@ -79,12 +74,10 @@ def combined_function(user_prompt, similarity_threshold, selected_option, system
79
  else:
80
  return "No se encontraron resultados en Pinecone.", None
81
  elif selected_option == "Solo Yi-Coder":
82
- # Generar respuesta usando Yi-Coder
83
  yi_coder_response = generate_code(system_prompt, user_prompt, max_length)
84
  partial_message = get_partial_message(yi_coder_response)
85
  return partial_message, None
86
  elif selected_option == "Ambos (basado en umbral de similitud)":
87
- # Realizar b煤squeda vectorial
88
  search_results = vector_search(user_prompt, embedding_model, index)
89
  if search_results:
90
  top_result = search_results[0]
@@ -103,7 +96,6 @@ def combined_function(user_prompt, similarity_threshold, selected_option, system
103
  else:
104
  return "Opci贸n no v谩lida.", None
105
 
106
-
107
  # Funciones para el procesamiento de entradas y actualizaci贸n de im谩genes
108
  def process_input(message, history, selected_option, similarity_threshold, system_prompt, max_length):
109
  response, image = combined_function(message, similarity_threshold, selected_option, system_prompt, max_length)
@@ -111,15 +103,6 @@ def process_input(message, history, selected_option, similarity_threshold, syste
111
  return history, history, image
112
 
113
  def update_image(image_url):
114
- """
115
- Retorna los datos binarios de la imagen para ser mostrados en Gradio.
116
-
117
- Args:
118
- image_url (str): Ruta de la imagen.
119
-
120
- Returns:
121
- bytes o None: Datos binarios de la imagen si existe, de lo contrario None.
122
- """
123
  if image_url and os.path.exists(image_url):
124
  try:
125
  with open(image_url, "rb") as img_file:
@@ -132,7 +115,6 @@ def update_image(image_url):
132
  print("No se encontr贸 una imagen v谩lida.")
133
  return None
134
 
135
-
136
  def send_preset_question(question, history, selected_option, similarity_threshold, system_prompt, max_length):
137
  return process_input(question, history, selected_option, similarity_threshold, system_prompt, max_length)
138
 
@@ -140,4 +122,4 @@ def send_preset_question(question, history, selected_option, similarity_threshol
140
  demo = build_interface(process_input, send_preset_question, update_image)
141
 
142
  if __name__ == "__main__":
143
- demo.launch()
 
3
  import gradio as gr
4
  import os
5
  from models import load_embedding_model, load_yi_coder_model
6
+ from pinecone_utils import connect_to_pinecone, vector_search
7
  from ui import build_interface
8
  from config import SIMILARITY_THRESHOLD_DEFAULT, SYSTEM_PROMPT, MAX_LENGTH_DEFAULT
9
  from decorators import gpu_decorator
10
  import torch
11
 
12
  ########################
 
13
  from utils import process_tags_chat
 
14
  ########################
15
 
 
16
  # Cargar modelos
17
+ # Estas funciones ahora devuelven los modelos y dispositivos correctamente configurados.
18
  embedding_model = load_embedding_model()
19
  tokenizer, yi_coder_model, yi_coder_device = load_yi_coder_model()
20
 
 
24
  # Funci贸n para generar c贸digo utilizando Yi-Coder
25
  @gpu_decorator(duration=100)
26
  def generate_code(system_prompt, user_prompt, max_length):
27
+ # CORRECTO: yi_coder_device ya es un objeto torch.device, no una cadena.
28
  device = yi_coder_device
29
  model = yi_coder_model
30
+ tokenizer_ = tokenizer
31
 
32
  messages = [
33
  {"role": "system", "content": system_prompt},
34
  {"role": "user", "content": user_prompt}
35
  ]
36
 
 
37
  text = tokenizer_.apply_chat_template(
38
  messages,
39
  tokenize=False,
40
  add_generation_prompt=True
41
  )
42
+ # .to(device) funcionar谩 sin problemas porque device es un objeto torch.device.
43
  model_inputs = tokenizer_([text], return_tensors="pt").to(device)
44
 
45
  with torch.no_grad():
 
49
  eos_token_id=tokenizer_.eos_token_id
50
  )
51
 
 
52
  generated_ids = [
53
  output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
54
  ]
 
56
  response = tokenizer_.batch_decode(generated_ids, skip_special_tokens=True)[0]
57
  return response
58
 
 
59
  # Funci贸n para combinar b煤squeda vectorial y Yi-Coder
60
  @gpu_decorator(duration=100)
61
  def combined_function(user_prompt, similarity_threshold, selected_option, system_prompt, max_length):
62
  def get_partial_message(response):
 
63
  if "Respuesta:" in response:
64
+ return response.split("Respuesta:")[1].strip()
65
  else:
66
+ return response
67
 
68
  if selected_option == "Solo B煤squeda Vectorial":
 
69
  search_results = vector_search(user_prompt, embedding_model, index)
70
  if search_results:
71
  content = search_results[0]['content']
 
74
  else:
75
  return "No se encontraron resultados en Pinecone.", None
76
  elif selected_option == "Solo Yi-Coder":
 
77
  yi_coder_response = generate_code(system_prompt, user_prompt, max_length)
78
  partial_message = get_partial_message(yi_coder_response)
79
  return partial_message, None
80
  elif selected_option == "Ambos (basado en umbral de similitud)":
 
81
  search_results = vector_search(user_prompt, embedding_model, index)
82
  if search_results:
83
  top_result = search_results[0]
 
96
  else:
97
  return "Opci贸n no v谩lida.", None
98
 
 
99
  # Funciones para el procesamiento de entradas y actualizaci贸n de im谩genes
100
  def process_input(message, history, selected_option, similarity_threshold, system_prompt, max_length):
101
  response, image = combined_function(message, similarity_threshold, selected_option, system_prompt, max_length)
 
103
  return history, history, image
104
 
105
  def update_image(image_url):
 
 
 
 
 
 
 
 
 
106
  if image_url and os.path.exists(image_url):
107
  try:
108
  with open(image_url, "rb") as img_file:
 
115
  print("No se encontr贸 una imagen v谩lida.")
116
  return None
117
 
 
118
  def send_preset_question(question, history, selected_option, similarity_threshold, system_prompt, max_length):
119
  return process_input(question, history, selected_option, similarity_threshold, system_prompt, max_length)
120
 
 
122
  demo = build_interface(process_input, send_preset_question, update_image)
123
 
124
  if __name__ == "__main__":
125
+ demo.launch()