| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import torch | |
| import gradio as gr | |
| import random | |
| tokenizer = AutoTokenizer.from_pretrained('TrLOX/gpt2-tdk') | |
| model = AutoModelForCausalLM.from_pretrained('TrLOX/gpt2-tdk') | |
| def text_generation(keywords, domain): | |
| input_ids = tokenizer('keyword ' + keywords + ' domain ' + domain + ' title', return_tensors="pt").input_ids | |
| torch.manual_seed(random.randint(0,18446744073709551615)) | |
| outputs = model.generate(input_ids, do_sample=True, min_length=50, max_length=250) | |
| generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True) | |
| title_description_arr = generated_text[0].split(' title ')[1].split('description') | |
| title = title_description_arr[0].strip() | |
| description = title_description_arr[1].strip() | |
| return title + "\r\n\r\n" + description | |
| title = "TDK GPT2" | |
| description = "Title and description generation by keywords" | |
| gr.Interface( | |
| text_generation, | |
| [gr.inputs.Textbox(default='test 1,test 2',lines=2, label="Enter keywords"), gr.inputs.Textbox(lines=2, default='test.com',label="Enter domain")], | |
| [gr.outputs.Textbox(type="auto", label="Text Generated")], | |
| title=title, | |
| description=description, | |
| theme="huggingface" | |
| ).launch() |