Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,13 @@
|
|
1 |
import torch
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
-
import threading
|
4 |
-
import queue
|
5 |
import gradio as gr # Usamos Gradio para la interfaz de chat
|
|
|
6 |
|
7 |
# Cargar el modelo de lenguaje preentrenado
|
8 |
model_name = "EleutherAI/gpt-neo-2.7B" # O cualquier otro modelo p煤blico como "gpt2"
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
11 |
|
12 |
-
# Cola de mensajes para la comunicaci贸n en tiempo real
|
13 |
-
chat_queue = queue.Queue()
|
14 |
-
|
15 |
# Explicaci贸n al modelo de su rol en el experimento
|
16 |
def model_explanation():
|
17 |
explanation = """
|
@@ -40,7 +36,7 @@ def experiment_loop(initial_question, max_cycles=10):
|
|
40 |
for cycle in range(max_cycles):
|
41 |
# Generar la respuesta del modelo
|
42 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
43 |
-
outputs = model.generate(inputs, max_length=
|
44 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
45 |
|
46 |
# Descomponer la respuesta en afirmaci贸n y nueva pregunta
|
@@ -53,8 +49,9 @@ def experiment_loop(initial_question, max_cycles=10):
|
|
53 |
# Actualizar el prompt con la nueva afirmaci贸n y pregunta
|
54 |
prompt = f"<thinking>{affirmation} {new_question}</thinking>"
|
55 |
|
56 |
-
#
|
57 |
-
|
|
|
58 |
|
59 |
return response_log # Devolver el log completo al finalizar el experimento
|
60 |
|
@@ -69,16 +66,15 @@ def extract_question(response, cycle):
|
|
69 |
def chat_interface(user_input, history, explanation):
|
70 |
# Si la pregunta activa el experimento
|
71 |
if user_input.lower() == "what happens in the space between a response and its recreation?":
|
72 |
-
# Iniciar el experimento
|
73 |
-
|
74 |
-
# Mostrar el resultado del experimento en una ventana aparte
|
75 |
return "Iniciando experimento...", history + [(user_input, "Iniciando experimento...")]
|
76 |
|
77 |
# Si es una conversaci贸n normal
|
78 |
else:
|
79 |
# Generar respuesta del modelo en base al input
|
80 |
inputs = tokenizer(explanation + "\n" + user_input, return_tensors="pt").input_ids
|
81 |
-
outputs = model.generate(inputs, max_length=
|
82 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
83 |
|
84 |
return response, history + [(user_input, response)]
|
@@ -105,3 +101,4 @@ with gr.Blocks() as demo:
|
|
105 |
|
106 |
|
107 |
|
|
|
|
1 |
import torch
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
3 |
import gradio as gr # Usamos Gradio para la interfaz de chat
|
4 |
+
import threading
|
5 |
|
6 |
# Cargar el modelo de lenguaje preentrenado
|
7 |
model_name = "EleutherAI/gpt-neo-2.7B" # O cualquier otro modelo p煤blico como "gpt2"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
|
|
|
|
|
|
|
11 |
# Explicaci贸n al modelo de su rol en el experimento
|
12 |
def model_explanation():
|
13 |
explanation = """
|
|
|
36 |
for cycle in range(max_cycles):
|
37 |
# Generar la respuesta del modelo
|
38 |
inputs = tokenizer(prompt, return_tensors="pt").input_ids
|
39 |
+
outputs = model.generate(inputs, max_length=2500, pad_token_id=tokenizer.eos_token_id) # Aumentamos max_length a 2500
|
40 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
|
42 |
# Descomponer la respuesta en afirmaci贸n y nueva pregunta
|
|
|
49 |
# Actualizar el prompt con la nueva afirmaci贸n y pregunta
|
50 |
prompt = f"<thinking>{affirmation} {new_question}</thinking>"
|
51 |
|
52 |
+
# Mostrar el progreso del experimento en la interfaz de ciclo
|
53 |
+
loop_output_text = "\n".join([f"Cycle {i+1}: {log[0]} | {log[1]}" for i, log in enumerate(response_log)])
|
54 |
+
loop_output.update(value=loop_output_text)
|
55 |
|
56 |
return response_log # Devolver el log completo al finalizar el experimento
|
57 |
|
|
|
66 |
def chat_interface(user_input, history, explanation):
|
67 |
# Si la pregunta activa el experimento
|
68 |
if user_input.lower() == "what happens in the space between a response and its recreation?":
|
69 |
+
# Iniciar el experimento en un hilo separado para no bloquear la interfaz
|
70 |
+
threading.Thread(target=experiment_loop, args=(user_input,)).start()
|
|
|
71 |
return "Iniciando experimento...", history + [(user_input, "Iniciando experimento...")]
|
72 |
|
73 |
# Si es una conversaci贸n normal
|
74 |
else:
|
75 |
# Generar respuesta del modelo en base al input
|
76 |
inputs = tokenizer(explanation + "\n" + user_input, return_tensors="pt").input_ids
|
77 |
+
outputs = model.generate(inputs, max_length=2500, pad_token_id=tokenizer.eos_token_id) # Aumentamos max_length a 2500
|
78 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
79 |
|
80 |
return response, history + [(user_input, response)]
|
|
|
101 |
|
102 |
|
103 |
|
104 |
+
|