File size: 1,282 Bytes
7971d45
 
4bad5f1
7971d45
14c1ab3
7971d45
 
 
 
7886f0d
 
4bad5f1
7886f0d
14c1ab3
7886f0d
4bad5f1
14c1ab3
 
 
 
 
65e1e1f
 
14c1ab3
 
 
 
 
65e1e1f
 
4bad5f1
 
7886f0d
 
 
4bad5f1
7886f0d
 
4bad5f1
 
7886f0d
 
65e1e1f
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
import os
from huggingface_hub import login
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import torch

# 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).to("cuda")

def chat_with_gpt2_spanish(input_text):
    # Comprobar si la GPU está disponible
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print(f"Using device: {device}")

    inputs = tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512).to(device)
    outputs = model.generate(
        **inputs, 
        max_length=100,
        num_beams=1,
        temperature=0.7,
        top_p=0.9,
        no_repeat_ngram_size=2,
        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()