File size: 2,388 Bytes
6a0ab84
ea18a0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
import torch

# ---------- MODELO DE SIMPLIFICACIÓN ----------
simplifier_model_name = "google/flan-t5-small"
simplifier_tokenizer = AutoTokenizer.from_pretrained(simplifier_model_name)
simplifier_model = AutoModelForSeq2SeqLM.from_pretrained(simplifier_model_name)

def simplificar_texto(texto):
    prompt = f"Simplify this text: {texto}"
    inputs = simplifier_tokenizer(prompt, return_tensors="pt", truncation=True)
    outputs = simplifier_model.generate(**inputs, max_new_tokens=100)
    resultado = simplifier_tokenizer.decode(outputs[0], skip_special_tokens=True)
    return resultado

# ---------- MODELO DE PREDICCIÓN DE TEXTO ----------
predictor_model_name = "distilgpt2"
predictor_tokenizer = AutoTokenizer.from_pretrained(predictor_model_name)
predictor_model = AutoModelForCausalLM.from_pretrained(predictor_model_name)

def predecir_texto(texto_inicial):
    inputs = predictor_tokenizer.encode(texto_inicial, return_tensors="pt")
    outputs = predictor_model.generate(inputs, max_new_tokens=20, do_sample=True, top_k=50)
    texto_generado = predictor_tokenizer.decode(outputs[0], skip_special_tokens=True)
    return texto_generado[len(texto_inicial):]  # Solo mostrar lo nuevo

# ---------- INTERFAZ GRADIO ----------
with gr.Blocks() as demo:
    gr.Markdown("## 🧠 Chatbot Simplificador y Teclado Predictivo")
    
    with gr.Tab("Simplificación de texto"):
        gr.Markdown("Introduce un texto complejo y obtén una versión más sencilla.")
        entrada_simplificar = gr.Textbox(label="Texto original", lines=4, placeholder="Ej. Un párrafo de un documento legal...")
        salida_simplificar = gr.Textbox(label="Texto simplificado")
        boton_simplificar = gr.Button("Simplificar")
        boton_simplificar.click(fn=simplificar_texto, inputs=entrada_simplificar, outputs=salida_simplificar)

    with gr.Tab("Texto Predictivo"):
        gr.Markdown("Escribe el inicio de una frase y recibe sugerencias.")
        entrada_predecir = gr.Textbox(label="Frase incompleta", placeholder="Ej. Me gustaría ir a la...")
        salida_predecir = gr.Textbox(label="Sugerencia")
        boton_predecir = gr.Button("Predecir")
        boton_predecir.click(fn=predecir_texto, inputs=entrada_predecir, outputs=salida_predecir)

demo.launch()