Spaces:
Sleeping
Sleeping
File size: 895 Bytes
4a4cab1 da0adc8 4a4cab1 da0adc8 287519a da0adc8 |
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 gradio as gr
from transformers import AutoModelForCausalLM, GPT2Tokenizer
def generate_text(sentence, max_length=100):
model_path = "franzemil/bolivianlm"
model_name = "datificate/gpt2-small-spanish"
# Load the model and the tokenizer
model = AutoModelForCausalLM.from_pretrained(model_path)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
# Generate the ids using the tokenizer
ids = tokenizer.encode(sentence, return_tensors="pt")
# Use the model to generate text
outputs = model.generate(
ids,
do_sample=True,
max_length=max_length,
pad_token_id=model.config.eos_token_id,
top_k=50,
top_p=0.95,
)
# Decode the and return the string
return tokenizer.decode(outputs[0], skip_special_tokens=True)
demo = gr.Interface(fn=generate_text, inputs="text", outputs="text")
demo.launch() |