File size: 23,988 Bytes
f89848b
95792cc
 
f89848b
 
 
 
 
 
7e65dc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f89848b
 
 
95792cc
f89848b
 
 
 
 
09379f0
 
 
 
 
 
f89848b
95166f2
f89848b
e50471b
 
 
 
f89848b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e50471b
f89848b
 
 
 
 
 
 
 
e50471b
95792cc
e50471b
 
95792cc
f89848b
 
25436e1
 
08a4871
25436e1
e50471b
25436e1
 
 
 
 
 
 
 
e50471b
 
95792cc
 
 
 
 
 
f89848b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95166f2
f89848b
e50471b
 
 
f89848b
95166f2
 
f89848b
09379f0
f89848b
 
 
 
 
 
 
 
 
 
 
 
 
38c5edb
e50471b
 
 
 
 
 
 
 
38c5edb
e50471b
 
 
f89848b
38c5edb
e50471b
 
 
 
 
 
f89848b
e50471b
38c5edb
f89848b
e50471b
 
38c5edb
 
e50471b
bed30b4
f89848b
38c5edb
 
e50471b
38c5edb
 
 
f89848b
38c5edb
 
f89848b
 
 
25436e1
e50471b
f89848b
25436e1
 
 
 
f89848b
 
 
 
 
 
 
 
 
 
 
 
 
 
e50471b
f89848b
 
 
 
e50471b
 
f89848b
e50471b
 
 
f89848b
 
 
 
 
e50471b
25436e1
 
 
 
e50471b
 
25436e1
e50471b
 
 
25436e1
 
 
38c5edb
25436e1
e50471b
95792cc
 
 
e50471b
95792cc
 
e50471b
 
95792cc
e50471b
 
 
95792cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed30b4
 
95792cc
bed30b4
15c5469
 
 
 
 
 
 
bed30b4
15c5469
95792cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e50471b
95792cc
 
 
 
ad4cf83
 
f89848b
 
e50471b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95792cc
 
 
 
 
 
 
31722dc
 
 
 
e50471b
 
31722dc
95792cc
 
e50471b
95792cc
e50471b
95792cc
 
 
 
 
 
 
 
e50471b
95792cc
 
 
 
38c5edb
e50471b
95792cc
 
 
e50471b
f89848b
 
e50471b
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import os
import random
import string

import gradio as gr
import torch
from transformers import pipeline, set_seed
from transformers import AutoTokenizer, AutoModelForCausalLM
import logging


# Monkey patch
import inspect
from gradio import routes
from typing import List, Type

def get_types(cls_set: List[Type], component: str):
    docset = []
    types = []
    if component == "input":
        for cls in cls_set:
            doc = inspect.getdoc(cls)
            doc_lines = doc.split("\n")
            docset.append(doc_lines[1].split(":")[-1])
            types.append(doc_lines[1].split(")")[0].split("(")[-1])
    else:
        for cls in cls_set:
            doc = inspect.getdoc(cls)
            doc_lines = doc.split("\n")
            docset.append(doc_lines[-1].split(":")[-1])
            types.append(doc_lines[-1].split(")")[0].split("(")[-1])
    return docset, types
routes.get_types = get_types

logger = logging.getLogger()
logger.addHandler(logging.StreamHandler())

DEBUG = os.environ.get("DEBUG", "false")[0] in "ty1"
HF_AUTH_TOKEN = os.environ.get("HF_AUTH_TOKEN", None)
DEVICE = os.environ.get("DEVICE", "cpu")  # cuda:0
if DEVICE != "cpu" and not torch.cuda.is_available():
    DEVICE = "cpu"
logger.info(f"DEVICE {DEVICE}")
DTYPE = getattr(
    torch,
    os.environ.get("DTYPE", ""),
    torch.float32 if DEVICE == "cpu" else torch.float16
)
LOW_CPU_MEM = bool(os.environ.get("LOW_CPU_MEM", False if DEVICE == "cpu" else True))
MODEL_NAME = os.environ.get("MODEL_NAME", "bertin-project/bertin-gpt-j-6B")
MODEL_REVISION = os.environ.get("MODEL_REVISION", "main")
MAX_LENGTH = int(os.environ.get("MAX_LENGTH", 1024))
display_model_name = "BERTIN GPT-J-6B" if MODEL_NAME == "bertin-project/bertin-gpt-j-6B" else MODEL_NAME.upper()
HEADER_INFO = f"""
# {display_model_name}
Spanish {display_model_name} Model.
""".strip()
LOGO = "https://huggingface.co/bertin-project/bertin-roberta-base-spanish/resolve/main/images/bertin.png"
HEADER = f"""
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap%22%20rel=%22stylesheet%22" rel="stylesheet">
<style>
.ltr,
textarea {{
    font-family: Roboto !important;
    text-align: left;
    direction: ltr !important;
}}
.ltr-box {{
    border-bottom: 1px solid #ddd;
    padding-bottom: 20px;
}}
.rtl {{
    text-align: left;
    direction: ltr !important;
}}
span.result-text {{
    padding: 3px 3px;
    line-height: 32px;
}}
span.generated-text {{
    background-color: rgb(118 200 147 / 13%);
}}
</style>
<div align=center>
<img src="{LOGO}" width=150/>

# {display_model_name}

BERTIN proporciona una serie de modelos de lenguaje en Español entrenados en abierto.

Este modelo ha sido entrenado con [Mesh Transformer JAX](https://github.com/kingoflolz/mesh-transformer-jax) en TPUs proporcionadas por Google a través del programa Tensor Research Cloud, a partir del modelo [GPT-J de EleutherAI](https://huggingface.co/EleutherAI/gpt-j-6B) con el corpus [mC4-es-sampled (gaussian)](https://huggingface.co/datasets/bertin-project/mc4-es-sampled). Esta demo funciona sobre una GPU proporcionada por HuggingFace.

</div>
"""

FOOTER = f"""
<div align=center>
Para más información, visite el repositorio del modelo: <a href="https://huggingface.co/{MODEL_NAME}">{display_model_name}</a>.
<img src="https://visitor-badge.glitch.me/badge?page_id=spaces/{MODEL_NAME}"/>
<div align=center>
""".strip()

EXAMPLES = [
    "",
    "Érase una vez,",
    "¿Cuál es la capital de Francia? Respuesta:",
    "En un lugar de la Mancha, de cuyo nombre no quiero acordarme, no ha mucho tiempo que vivía un hidalgo de los de lanza en astillero, adarga antigua, rocín flaco y galgo corredor.",
    """Los templos egipcios fueron construidos para el culto oficial de los dioses y la conmemoración de los faraones del Antiguo Egipto en las regiones bajo su dominio. Los templos eran vistos como el hogar de los dioses o faraones deificados a quienes eran dedicados, y en ellos los faraones y el clero egipcio llevaban a cabo diversos rituales, las funciones centrales de la religión egipcia: realizar ofrendas a sus dioses, recrear pasajes mitológicos mediante festivales y protegerse de las fuerzas del caos. Estos rituales eran vistos como necesarios para que los dioses mantuvieran la maat, el orden divino del universo.

El cuidado del hogar de los dioses era obligación de los faraones, que dedicaron ingentes cantidades de recursos para la construcción y el mantenimiento de los templos. Por necesidad, los faraones delegaban la mayoría de los rituales en una amplia casta sacerdotal, aunque la mayor parte del pueblo llano permanecía al margen de la participación directa en las ceremonias por tener prohibido el acceso a las zonas más sagradas de los templos. A pesar de ello, el templo siempre fue un importante centro religioso para todos los egipcios, que iban a ellos a rezar, realizar ofrendas y buscar la guía de los oráculos.

Pregunta: ¿Quién cuidaba del hogar los dioses?
Respuesta:""",
]

AGENT = os.environ.get("AGENT_NAME", "BERTIN") 
PREV = "PREV"
USER = "ENTREVISTADOR"
CONTEXT = """La siguiente conversación es un extracto de una entrevista a {AGENT} celebrada en Madrid para Radio Televisión Española:

{USER}: Bienvenido, {AGENT}. Un placer tenerlo hoy con nosotros.
{AGENT}: Gracias. El placer es mío."""

class Normalizer:
    def remove_repetitions(self, text):
        """Remove repetitions"""
        first_ocurrences = []
        for sentence in text.split("."):
            if sentence not in first_ocurrences:
                first_ocurrences.append(sentence)
        return '.'.join(first_ocurrences)

    def trim_last_sentence(self, text):
        """Trim last sentence if incomplete"""
        return text[:text.rfind(".") + 1]

    def clean_txt(self, text):
        return self.trim_last_sentence(self.remove_repetitions(text))


class TextGeneration:
    def __init__(self):
        self.tokenizer = None
        self.generator = None
        self.task = "text-generation"
        self.model_name_or_path = MODEL_NAME
        set_seed(42)

    def load(self):
        logger.info("Loading model...")
        self.tokenizer = AutoTokenizer.from_pretrained(
            self.model_name_or_path, revision=MODEL_REVISION, use_auth_token=HF_AUTH_TOKEN if HF_AUTH_TOKEN else None,
        )
        self.tokenizer_prefix_space = AutoTokenizer.from_pretrained(
            self.model_name_or_path, add_prefix_space=True, revision=MODEL_REVISION, use_auth_token=HF_AUTH_TOKEN if HF_AUTH_TOKEN else None,
        )
        self.model = AutoModelForCausalLM.from_pretrained(
            self.model_name_or_path, revision=MODEL_REVISION,
            use_auth_token=HF_AUTH_TOKEN if HF_AUTH_TOKEN else None,
            pad_token_id=self.tokenizer.eos_token_id, eos_token_id=self.tokenizer.eos_token_id,
            torch_dtype=DTYPE, low_cpu_mem_usage=LOW_CPU_MEM,
        ).to(device=DEVICE, non_blocking=False)
        _ = self.model.eval()
        device_number = -1 if DEVICE == "cpu" else int(DEVICE.split(":")[-1])
        self.generator = pipeline(self.task, model=self.model, tokenizer=self.tokenizer, device=device_number)
        logger.info("Loading model done.")
        # with torch.no_grad():
        # tokens = tokenizer.encode(prompt, return_tensors='pt').to(device=device, non_blocking=True)
        # gen_tokens = self.model.generate(tokens, do_sample=True, temperature=0.8, max_length=128)
        # generated = tokenizer.batch_decode(gen_tokens)[0]

        # return generated


    def generate(self, text, generation_kwargs, previous_text=None):
        do_clean = generation_kwargs.pop("do_clean", False)
        bad_words = generation_kwargs.pop("bad_words", "")
        if bad_words:
            generation_kwargs["bad_words_ids"] = self.tokenizer_prefix_space(
                [word.strip() for word in bad_words.split(",")], add_special_tokens=False
            ).input_ids
        if "repetition_penalty" in generation_kwargs:
            generation_kwargs["repetition_penalty"] = float(generation_kwargs["repetition_penalty"])
        input_text = previous_text or text
        # max_length = len(self.tokenizer(input_text)["input_ids"]) + generation_kwargs["max_length"]
        # generation_kwargs["max_length"] = min(max_length, self.model.config.n_positions)
        generation_kwargs["max_new_tokens"] = generation_kwargs.pop("max_length", 50)
        generated_text = None
        if input_text:
            pre_input_text = ""
            input_ids = self.tokenizer(input_text).input_ids
            if len(input_ids) + generation_kwargs["max_new_tokens"] >= 2048:
                prompt_cutoff = 2048 - generation_kwargs["max_new_tokens"] + 1
                pre_input_text = self.tokenizer.decode(input_ids[:-prompt_cutoff])
                input_text = self.tokenizer.decode(input_ids[-prompt_cutoff:])
            for _ in range(10):
                generated_text = pre_input_text + (" " if do_clean else "") + self.generator(
                    input_text,
                    **generation_kwargs,
                )[0]["generated_text"]                
                input_text = self.tokenizer.decode(input_ids)
                if generated_text.strip().startswith(input_text):
                    generated_text = generated_text.replace(input_text, "", 1).strip()
                if do_clean:
                    generated_text = cleaner.clean_txt(generated_text)
                if generated_text:
                    if previous_text and previous_text != text:
                        diff = [
                            (text, None), (previous_text.replace(text, " ", 1).strip(), PREV), (generated_text, AGENT)
                        ]
                    else:
                        diff = [(text, None), (generated_text, AGENT)]
                    return (
                        input_text + " " + generated_text,
                        diff
                    )
            if not generated_text:
                return (
                    "",
                    [(f"Tras 10 intentos {AGENT} no generó nada. Pruebe cambiando las opciones.", "ERROR")]
                )
        return (
            "",
            [("Debe escribir algo primero.", "ERROR")]
        )


#@st.cache(hash_funcs={torch.nn.parameter.Parameter: lambda _: None})
#@st.cache(allow_output_mutation=True)
#@st.cache(allow_output_mutation=True, hash_funcs={TextGeneration: lambda _: None})
def load_text_generator():
    text_generator = TextGeneration()
    text_generator.load()
    return text_generator

cleaner = Normalizer()
generator = load_text_generator()


def complete_with_gpt(text, max_length, top_k, top_p, penalty_alpha, num_beams, temperature, repetition_penalty, no_repeat_ngram_size, bad_words, do_sample, do_clean):
    generation_kwargs = {
        "max_length": max_length,
        "top_k": top_k,
        "top_p": top_p,
        "penalty_alpha": penalty_alpha,
        "num_beams": num_beams,
        "temperature": temperature,
        "repetition_penalty": repetition_penalty,
        "no_repeat_ngram_size": no_repeat_ngram_size,
        "bad_words": bad_words,
        "do_sample": do_sample,
        "do_clean": do_clean,
    }
    return generator.generate(text, generation_kwargs)

def expand_with_gpt(hidden, text, max_length, top_k, top_p, penalty_alpha, num_beams, temperature, repetition_penalty, no_repeat_ngram_size, bad_words, do_sample, do_clean):
    generation_kwargs = {
        "max_length": max_length,
        "top_k": top_k,
        "top_p": top_p,
        "penalty_alpha": penalty_alpha,
        "num_beams": num_beams,
        "temperature": temperature,
        "repetition_penalty": repetition_penalty,
        "no_repeat_ngram_size": no_repeat_ngram_size,
        "bad_words": bad_words,
        "do_sample": do_sample,
        "do_clean": do_clean,
    }
    return generator.generate(text, generation_kwargs, previous_text=hidden)

def chat_with_gpt(agent, user, context, user_message, history, max_length, top_k, top_p, penalty_alpha, num_beams, temperature, repetition_penalty, no_repeat_ngram_size, bad_words, do_sample, do_clean):
    # agent = AGENT
    # user = USER
    generation_kwargs = {
        "max_length": max_length,
        "top_k": top_k,
        "top_p": top_p,
        "penalty_alpha": penalty_alpha,
        "num_beams": num_beams,
        "temperature": temperature,
        "repetition_penalty": repetition_penalty,
        "no_repeat_ngram_size": no_repeat_ngram_size,
        "bad_words": bad_words,
        "do_sample": do_sample,
        "do_clean": do_clean,
        # "num_return_sequences": 1,
        # "return_full_text": False,
    }
    message = user_message.split(" ", 1)[0].capitalize() + " " + user_message.split(" ", 1)[-1]
    history = history or []  #[(f"{user}: Bienvenido. Encantado de tenerle con nosotros.", f"{agent}: Un placer, muchas gracias por la invitación.")]
    context = context.format(USER=user or USER, AGENT=agent or AGENT).strip()
    if context[-1] not in ".:":
        context += "."
    context_length = len(context.split())
    history_take = 0
    history_context = "\n".join(f"{user}: {history_message.capitalize()}.\n{agent}: {history_response}." for history_message, history_response in history[-len(history) + history_take:])
    while len(history_context.split()) > generator.model.config.n_positions - (generation_kwargs["max_length"] + context_length):
        history_take += 1
        history_context = "\n".join(f"{user}: {history_message.capitalize()}.\n{agent}: {history_response}." for history_message, history_response in history[-len(history) + history_take:])
        if history_take >= generator.model.config.n_positions:
            break
    context += history_context
    for _ in range(5):
        prompt = f"{context}\n\n{user}: {message}.\n"
        response = generator.generate(prompt, generation_kwargs)[0]
        if DEBUG:
            print("\n-----\n" + response + "\n-----\n")
        # response = response.split("\n")[-1]
        # if agent in response and response.split(agent)[-1]:
        #     response = response.split(agent)[-1]
        # if user in response and response.split(user)[-1]:
        #     response = response.split(user)[-1]
        # Take the first response
        response = [
            r for r in response.replace(prompt, "").split(f"{AGENT}:") if r.strip()
        ][0].split(USER)[0].replace(f"{AGENT}:", "\n").strip()
        if response[0] in string.punctuation:
            response = response[1:].strip()
        if response.strip().startswith(f"{user}: {message}"):
            response = response.strip().split(f"{user}: {message}")[-1]
        if response.replace(".", "").strip() and message.replace(".", "").strip() != response.replace(".", "").strip():
            break
    if DEBUG:
        print()
        print("CONTEXT:")
        print(context)
        print()
        print("MESSAGE")
        print(message)
        print()
        print("RESPONSE:")
        print(response)
    if not response.strip():
        response = random.choice(["No sé muy bien cómo contestar a eso.", "No puedo contestar con seguridad.", "Prefiero no contestar.", "Ni idea.", "¿Podemos cambiar de tema?"])
    history.append((user_message, response))
    return history, history, ""


# css="#htext span {white-space: pre}"
with gr.Blocks() as demo:
    gr.Markdown(HEADER)
    with gr.Row():
        with gr.Column(scale=1):
            with gr.Group():
                with gr.Box():
                    gr.Markdown("Opciones")
                    with gr.Tabs():
                        with gr.TabItem("Generación"):
                            max_length = gr.Slider(
                                label='Palabras a generar',
                                # help="Número máximo (aproximado) de palabras a generar.",
                                minimum=1,
                                maximum=MAX_LENGTH,
                                value=50,
                                step=1
                            )
                            top_k = gr.Slider(
                                label='Top-k',
                                # help="Número de palabras con alta probabilidad a mantener para el filtrado `top-k`",
                                minimum=0,
                                maximum=80,
                                value=50,
                                step=1
                            )
                            top_p = gr.Slider(
                                label='Top-p',
                                # help="Solo las palabras más probables con probabilidades que sumen `top_p` o más se mantienen para la generación.",
                                minimum=0.01,
                                maximum=5.0,
                                value=0.95,
                                step=0.01
                            )
                            penalty_alpha = gr.Slider(
                                label='Penalización (alpha)',
                                # help="Penalización para contrastive search.",
                                minimum=0.0,
                                maximum=1.0,
                                value=0.0,
                                step=0.01
                            )
                            num_beams = gr.Slider(
                                label='Haces (beams)',
                                # help="Número de beams para búsqueda.",
                                minimum=1,
                                maximum=50,
                                value=1,
                                step=1
                            )
                            temperature = gr.Slider(
                                label='Temperatura',
                                # help="Valor utilizado para modular las probabilidades de las siguientes palabras generadas.",
                                minimum=0.0,
                                maximum=10.0,
                                value=0.8,
                                step=0.05
                            )
                            do_sample = gr.Checkbox(
                                label='¿Muestrear?',
                                value = True,
                                # options=(True, False),
                                # help="Si no se muestrea se usará una decodificación voraz (_greedy_).",
                            )
                            do_clean = gr.Checkbox(
                                label='¿Limpiar texto?',
                                value = False,
                                # options=(True, False),
                                # help="Si eliminar o no las palabras repetidas y recortar las últimas frases sin terminar.",
                            )
                        with gr.TabItem("Control de repetición"):
                            repetition_penalty = gr.Slider(
                                label='Penalización por repetición',
                                help="Un valor de 1 significa no penalización.",
                                minimum=1.0,
                                maximum=10.0,
                                value=1.0,
                                step=0.01
                            )
                            no_repeat_ngram_size = gr.Slider(
                                label='No repetir ngrams de tamaño',
                                minimum=0,
                                maximum=10,
                                value=0,
                                step=1
                            )
                            bad_words = gr.Textbox(
                                label="Palabras a evitar",
                                info="Lista de palabras separadas por comas",
                                lines=1,
                                value="",
                            )
                    with gr.Accordion("Estrategias", open=False):
                        gr.Markdown("""
                        - **greedy decoding** si `num_beams=1` y `do_sample=False`
                        - **contrastive search** si `penalty_alpha>0.0` y `top_k>1`
                        - **multinomial sampling** si `num_beams=1` y `do_sample=True`
                        - **beam-search decoding** si `num_beams>1` y `do_sample=False`
                        - **beam-search multinomial sampling** si `num_beams>1` y `do_sample=True`
                        """)
        with gr.Column(scale=4):
            with gr.Tabs():
                with gr.TabItem("Generar"):
                    textbox = gr.Textbox(label="Texto", placeholder="Escriba algo (o seleccione un ejemplo) y pulse 'Generar'...", lines=8)
                    examples = gr.Dropdown(label="Ejemplos", choices=EXAMPLES, value=None, type="value")
                    hidden = gr.Textbox(visible=False, show_label=False)
                    with gr.Box():
                        # output = gr.Markdown()
                        output = gr.HighlightedText(
                            elem_id="htext",
                            label="Resultado",
                            combine_adjacent=True,
                        ).style(
                            color_map={AGENT: "green", "ERROR": "red", PREV: "blue"},
                        )
                    with gr.Row():
                        generate_btn = gr.Button("Generar")
                        generate_btn.click(complete_with_gpt, inputs=[textbox, max_length, top_k, top_p, penalty_alpha, num_beams, temperature, repetition_penalty, no_repeat_ngram_size, bad_words, do_sample, do_clean], outputs=[hidden, output], api_name="generate")
                        expand_btn = gr.Button("Añadir")
                        expand_btn.click(expand_with_gpt, inputs=[hidden, textbox, max_length, top_k, top_p, penalty_alpha, num_beams, temperature, repetition_penalty, no_repeat_ngram_size, bad_words, do_sample, do_clean], outputs=[hidden, output])

                        edit_btn = gr.Button("Editar", variant="secondary")
                        edit_btn.click(lambda x: (x, "", []), inputs=[hidden], outputs=[textbox, hidden, output])
                        clean_btn = gr.Button("Borrar", variant="secondary")
                        clean_btn.click(lambda: ("", "", [], ""), inputs=[], outputs=[textbox, hidden, output, examples])
                    examples.change(lambda x: x, inputs=[examples], outputs=[textbox])

                with gr.TabItem("Charlar") as tab_chat:
                    # tab_chat.select(lambda: 25, inputs=[], outputs=[max_length])
                    context = gr.Textbox(label="Contexto", value=CONTEXT, lines=5)
                    with gr.Row():
                        agent = gr.Textbox(label="Agente", value=AGENT)
                        user = gr.Textbox(label="Usuario", value=USER)
                    history = gr.Variable(value=[])
                    chatbot = gr.Chatbot().style(color_map=("green", "gray"))
                    with gr.Row():
                        message = gr.Textbox(placeholder="Escriba aquí su mensaje y pulse 'Enviar'", show_label=False)
                        chat_btn = gr.Button("Enviar")
                    chat_btn.click(chat_with_gpt, inputs=[agent, user, context, message, history, max_length, top_k, top_p, penalty_alpha, num_beams, temperature, repetition_penalty, no_repeat_ngram_size, bad_words, do_sample, do_clean], outputs=[chatbot, history, message])
    gr.Markdown(FOOTER)

# with gr.Interface(lambda: None, inputs=["text", max_length, top_k, top_p, penalty_alpha, num_beams, temperature, do_sample, do_clean], outputs=[hidden, output]) as iface:
#     demo.examples = None
#     demo.predict_durations = []
#     demo.input_components = iface.input_components
#     demo.output_components = iface.output_components
demo.queue()
demo.launch(share=True)