File size: 990 Bytes
7971d45
 
4bad5f1
7971d45
 
 
 
 
7886f0d
 
4bad5f1
7886f0d
4bad5f1
7886f0d
4bad5f1
3f0376d
4bad5f1
 
 
7886f0d
 
 
4bad5f1
7886f0d
 
4bad5f1
 
7886f0d
 
4bad5f1
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
import os
from huggingface_hub import login
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr

# Autenticar usando el token almacenado como secreto
hf_token = os.getenv("HF_API_TOKEN")
login(hf_token)

# Cargar el modelo y el tokenizador
model_name = "DeepESP/gpt2-spanish"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

def chat_with_gpt2_spanish(input_text):
    inputs = tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512)
    outputs = model.generate(**inputs, max_length=200, num_beams=4, early_stopping=True)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return response

# Crear la interfaz con Gradio
iface = gr.Interface(
    fn=chat_with_gpt2_spanish,
    inputs="text",
    outputs="text",
    title="Chat con GPT-2 en Español",
    description="Interfaz simple para comunicarte con el modelo GPT-2 en español."
)

iface.launch()