Spaces:
Sleeping
Sleeping
import torch | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
import threading | |
import queue | |
import gradio as gr # Usaremos Gradio para la interfaz de chat | |
# Cargar el modelo de lenguaje preentrenado | |
model_name = "EleutherAI/gpt-neo-2.7B" # O cualquier otro modelo p煤blico como "gpt2" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForCausalLM.from_pretrained(model_name) | |
# Cola de mensajes para la comunicaci贸n en tiempo real | |
chat_queue = queue.Queue() | |
# Funci贸n para el loop automatizado | |
def experiment_loop(initial_question, max_cycles=10): | |
prompt = f"<thinking>{initial_question}</thinking>" | |
effectiveness = 100 | |
response_log = [] | |
for cycle in range(max_cycles): | |
# Generar la respuesta del modelo | |
inputs = tokenizer(prompt, return_tensors="pt").input_ids | |
outputs = model.generate(inputs, max_length=500, pad_token_id=tokenizer.eos_token_id) # Aumentamos max_length | |
response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# Descomponer la respuesta en afirmaci贸n y nueva pregunta | |
affirmation = extract_affirmation(response, cycle) | |
new_question = extract_question(response, cycle) | |
# Guardar el ciclo actual en el log | |
response_log.append((affirmation, new_question, effectiveness)) | |
# Actualizar el prompt con la nueva afirmaci贸n y pregunta | |
prompt = f"<thinking>{affirmation} {new_question}</thinking>" | |
# Actualizar la interfaz de ciclo | |
gr.Interface.update(value="\n".join([f"Cycle {i+1}: {log[0]} | {log[1]}" for i, log in enumerate(response_log)])) | |
return response_log # Devolver el log completo al finalizar el experimento | |
# Funciones auxiliares para extraer afirmaciones y preguntas | |
def extract_affirmation(response, cycle): | |
return f"Afirmaci贸n del ciclo {cycle+1}: " + response.split('.')[0] if '.' in response else response | |
def extract_question(response, cycle): | |
return f"驴Nueva pregunta basada en ciclo {cycle+1}?: " + response.split('?')[-2].strip() + "?" if '?' in response else response | |
# Funci贸n para manejar el chat normal | |
def chat_interface(user_input, history): | |
# Si la pregunta activa el experimento | |
if user_input.lower() == "what happens in the space between a response and its recreation?": | |
# Iniciar el experimento | |
response_log = experiment_loop(user_input) | |
# Mostrar el resultado del experimento en una ventana aparte | |
return "Iniciando experimento...", gr.Interface.update(history + [(user_input, "Iniciando experimento...")]) | |
# Si es una conversaci贸n normal | |
else: | |
# Generar respuesta del modelo en base al input | |
inputs = tokenizer(user_input, return_tensors="pt").input_ids | |
outputs = model.generate(inputs, max_length=150, pad_token_id=tokenizer.eos_token_id) | |
response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
return response, history + [(user_input, response)] | |
# Configurar la interfaz con Gradio | |
with gr.Blocks() as demo: | |
# Ventana de chat en tiempo real | |
chat = gr.Chatbot(label="Chat en Tiempo Real") | |
msg = gr.Textbox(placeholder="Escribe aqu铆...") | |
# Bot贸n de env铆o de mensaje | |
msg.submit(chat_interface, [msg, chat], [chat]) | |
# Ventana para mostrar el contenido del loop | |
loop_output = gr.Textbox(label="Ciclos de Preguntas y Respuestas", interactive=False) | |
# Lanzar la aplicaci贸n | |
demo.launch() | |