File size: 1,167 Bytes
fc3cf6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# Cargar el modelo y el tokenizador
model_name = "microsoft/DialoGPT-medium"  # Puedes cambiar esto por otro modelo de chatbot
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

def chatbot(input, history=[]):
    # Agregar el input del usuario al historial
    history.append(input)
    
    # Tokenizar la conversación
    input_ids = tokenizer.encode(" ".join(history), return_tensors="pt")
    
    # Generar una respuesta
    output = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
    
    # Decodificar la respuesta
    response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
    
    # Agregar la respuesta al historial
    history.append(response)
    
    return history, history

# Crear la interfaz de Gradio
iface = gr.Interface(
    fn=chatbot,
    inputs=["text", "state"],
    outputs=["chatbot", "state"],
    title="Tu Compañero AI",
    description="Un chatbot de IA diseñado para simular conversaciones personales.",
)

iface.launch()