import gradio as gr import torch from transformers import GPT2Tokenizer, GPT2LMHeadModel tokenizer = GPT2Tokenizer.from_pretrained('NlpHUST/gpt2-vietnamese') model = GPT2LMHeadModel.from_pretrained('NlpHUST/gpt2-vietnamese') # max_length = 100 def run(text, intensity): res="Tham khảo NlpHUST model \n \n \n" max_length=intensity input_ids = tokenizer.encode(text, return_tensors='pt') sample_outputs = model.generate(input_ids,pad_token_id=tokenizer.eos_token_id, do_sample=True, max_length=max_length, min_length=5, top_k=40, num_beams=5, early_stopping=True, no_repeat_ngram_size=2, num_return_sequences=2) for i, sample_output in enumerate(sample_outputs): res +="Mẫu số {}\n \n{}".format(i+1, tokenizer.decode(sample_output.tolist())) res +='\n \n \n \n' return res # demo = gr.Interface( # fn=run, # inputs=["text", "slider"], # outputs=["text"], # ) demo = gr.Interface(fn=run, inputs=[gr.Textbox(label="Nhập vào nội dung input",value="Con đường xưa em đi"),gr.Slider(label="Độ dài output muốn tạo ra", value=20, minimum=10, maximum=100, step=2)], outputs=gr.Textbox(label="Output"), # <-- Number of output components: 1 ) demo.launch()