Spaces:
Running
on
Zero
Running
on
Zero
| # app.py | |
| import gradio as gr | |
| import os | |
| from models import load_embedding_model, load_yi_coder_model | |
| from pinecone_utils import connect_to_pinecone, vector_search | |
| from ui import build_interface | |
| from config import SIMILARITY_THRESHOLD_DEFAULT, SYSTEM_PROMPT, MAX_LENGTH_DEFAULT | |
| from decorators import gpu_decorator | |
| import torch | |
| ######################## | |
| from utils import process_tags_chat | |
| ######################## | |
| # Cargar modelos | |
| # Estas funciones ahora devuelven los modelos y dispositivos correctamente configurados. | |
| embedding_model = load_embedding_model() | |
| tokenizer, yi_coder_model, yi_coder_device = load_yi_coder_model() | |
| # Conectar a Pinecone | |
| index = connect_to_pinecone() | |
| # Funci贸n para generar c贸digo utilizando Yi-Coder | |
| def generate_code(system_prompt, user_prompt, max_length): | |
| # CORRECTO: yi_coder_device ya es un objeto torch.device, no una cadena. | |
| device = yi_coder_device | |
| model = yi_coder_model | |
| tokenizer_ = tokenizer | |
| messages = [ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": user_prompt} | |
| ] | |
| text = tokenizer_.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True | |
| ) | |
| # .to(device) funcionar谩 sin problemas porque device es un objeto torch.device. | |
| model_inputs = tokenizer_([text], return_tensors="pt").to(device) | |
| with torch.no_grad(): | |
| generated_ids = model.generate( | |
| model_inputs.input_ids, | |
| max_new_tokens=max_length, | |
| eos_token_id=tokenizer_.eos_token_id | |
| ) | |
| generated_ids = [ | |
| output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids) | |
| ] | |
| response = tokenizer_.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
| return response | |
| # Funci贸n para combinar b煤squeda vectorial y Yi-Coder | |
| def combined_function(user_prompt, similarity_threshold, selected_option, system_prompt, max_length): | |
| def get_partial_message(response): | |
| if "Respuesta:" in response: | |
| return response.split("Respuesta:")[1].strip() | |
| else: | |
| return response | |
| if selected_option == "Solo B煤squeda Vectorial": | |
| search_results = vector_search(user_prompt, embedding_model, index) | |
| if search_results: | |
| content = search_results[0]['content'] | |
| partial_message = get_partial_message(content) | |
| return partial_message, None | |
| else: | |
| return "No se encontraron resultados en Pinecone.", None | |
| elif selected_option == "Solo Yi-Coder": | |
| yi_coder_response = generate_code(system_prompt, user_prompt, max_length) | |
| partial_message = get_partial_message(yi_coder_response) | |
| return partial_message, None | |
| elif selected_option == "Ambos (basado en umbral de similitud)": | |
| search_results = vector_search(user_prompt, embedding_model, index) | |
| if search_results: | |
| top_result = search_results[0] | |
| if top_result['score'] >= similarity_threshold: | |
| content = top_result['content'] | |
| partial_message = get_partial_message(content) | |
| return partial_message, None | |
| else: | |
| yi_coder_response = generate_code(system_prompt, user_prompt, max_length) | |
| partial_message = get_partial_message(yi_coder_response) | |
| return partial_message, None | |
| else: | |
| yi_coder_response = generate_code(system_prompt, user_prompt, max_length) | |
| partial_message = get_partial_message(yi_coder_response) | |
| return partial_message, None | |
| else: | |
| return "Opci贸n no v谩lida.", None | |
| # Funciones para el procesamiento de entradas y actualizaci贸n de im谩genes | |
| def process_input(message, history, selected_option, similarity_threshold, system_prompt, max_length): | |
| response, image = combined_function(message, similarity_threshold, selected_option, system_prompt, max_length) | |
| history.append((message, response)) | |
| return history, history, image | |
| def update_image(image_url): | |
| if image_url and os.path.exists(image_url): | |
| try: | |
| with open(image_url, "rb") as img_file: | |
| image_data = img_file.read() | |
| return image_data | |
| except Exception as e: | |
| print(f"Error al leer la imagen: {e}") | |
| return None | |
| else: | |
| print("No se encontr贸 una imagen v谩lida.") | |
| return None | |
| def send_preset_question(question, history, selected_option, similarity_threshold, system_prompt, max_length): | |
| return process_input(question, history, selected_option, similarity_threshold, system_prompt, max_length) | |
| # Construir y lanzar la interfaz | |
| demo = build_interface(process_input, send_preset_question, update_image) | |
| if __name__ == "__main__": | |
| demo.launch() |