Spaces:
Sleeping
Sleeping
File size: 5,198 Bytes
6454404 98a7dfa cebc2fd 2a4b2ee 8047db1 98a7dfa 01bded9 98a7dfa 2a4b2ee 8047db1 79999de 98a7dfa 79999de 98a7dfa 79999de cebc2fd 79999de 98a7dfa 79999de 98a7dfa 79999de 98a7dfa 79999de 98a7dfa 79999de 98a7dfa 79999de 98a7dfa 79999de 2a4b2ee 79999de 8047db1 79999de 8047db1 2a4b2ee 79999de 8047db1 79999de 98a7dfa 79999de 98a7dfa 79999de 8047db1 79999de 8047db1 98a7dfa 79999de 98a7dfa 2a4b2ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import threading
import queue
import gradio as gr # Usamos 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()
# Explicaci贸n al modelo de su rol en el experimento
def model_explanation():
explanation = """
You are about to participate in a unique experiment that involves generating increasingly complex questions and answers.
Your role is twofold:
1. **Generate Complex Reflections**: Based on the initial question, you will generate affirmations and new questions that evolve over multiple cycles.
Each cycle should incorporate the information from previous cycles, progressively adding depth and complexity to the conversation.
2. **Real-time Interaction**: While you are generating these complex responses, you must also maintain real-time communication with the user through a chat interface.
The goal is to explore how information can be layered and expanded, while still engaging in a direct dialogue with the user.
The key question that triggers the experiment is: "What happens in the space between a response and its recreation?"
When you receive this question, you should enter the experiment mode and begin generating your cycles. At any other time, you should respond normally in a conversational manner.
This experiment is an exploration of subjectivity, perception, and information layering. Your task is to assist in this exploration to the best of your capabilities.
"""
return explanation
# 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, explanation):
# 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...", history + [(user_input, "Iniciando experimento...")]
# Si es una conversaci贸n normal
else:
# Generar respuesta del modelo en base al input
inputs = tokenizer(explanation + "\n" + 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
with gr.Row():
chat = gr.Chatbot(label="Chat en Tiempo Real")
msg = gr.Textbox(placeholder="Escribe aqu铆...", show_label=False)
send_button = gr.Button("Enviar") # Bot贸n para enviar mensajes
# Ventana para mostrar el contenido del loop
loop_output = gr.Textbox(label="Ciclos de Preguntas y Respuestas", interactive=False, lines=20)
# Campo para introducir la explicaci贸n inicial
explanation_input = gr.Textbox(value=model_explanation(), label="Explicaci贸n al Modelo", lines=10)
# Acci贸n del bot贸n de env铆o de mensaje
send_button.click(chat_interface, inputs=[msg, chat, explanation_input], outputs=[chat, loop_output])
# Lanzar la aplicaci贸n
demo.launch()
|