Spaces:
Paused
Paused
| import gradio as gr | |
| import torch | |
| from transformers import pipeline, set_seed, AutoTokenizer, AutoModelForCausalLM | |
| import random | |
| title = "מחולל תקצירים פיקטיביים לספרים - מבוסס ג׳פיטי-נאו פצפון" | |
| article = "מודל השפה אומן על ידי <a href=\"https://linktr.ee/Norod78\">דורון אדלר</a>" | |
| description = "<p>Fine tuned <a href=\"https://huggingface.co/Norod78/hebrew-gpt_neo-tiny\">Norod78/hebrew-gpt_neo-tiny</a> upon a book summary dataset<p>" | |
| examples = [ | |
| ['הארי פוטר'], | |
| ['החתול המכאני'], | |
| ['אם מתחשק לכם לפעמים'], | |
| ["מחוללי הטקסט / "], | |
| ['האדם האחרון עלי אדמות ישב לבד בחדרו כשלפתע נשמע דפיקה'], | |
| ] | |
| model_id = "./hebrew-gpt_neo-tiny-HebrewBookSummaries" | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| n_gpu = 0 if torch.cuda.is_available()==False else torch.cuda.device_count() | |
| tokenizer = AutoTokenizer.from_pretrained(model_id, bos_token='<|startoftext|>', eos_token='<|endoftext|>', pad_token='<|pad|>', unknown_token = '<|unknown|>') | |
| model = AutoModelForCausalLM.from_pretrained(model_id).to(device) | |
| model.resize_token_embeddings(len(tokenizer)) | |
| text_generator = pipeline('text-generation', model=model, tokenizer=tokenizer) | |
| max_length = 96 | |
| top_k = 40 | |
| top_p = 0.92 | |
| temperature = 0.8 | |
| max_seed = (2**32)-1 | |
| global_seed = random.randint(0, max_seed) | |
| def text_generation(input_text = ''): | |
| global global_seed | |
| global_seed = global_seed + 1 | |
| if global_seed >= max_seed: | |
| global_seed = 0 | |
| if input_text == None or len(input_text) == 0: | |
| input_text = "<|startoftext|>" | |
| set_seed(global_seed) | |
| generated_text = text_generator(input_text, | |
| max_length=max_length, | |
| top_k=top_k, | |
| top_p=top_p, | |
| temperature=temperature, | |
| do_sample=True, | |
| repetition_penalty=2.0, | |
| num_return_sequences=1) | |
| parsed_text = generated_text[0]["generated_text"].replace("<|startoftext|>", "").replace("\r","\n").replace("\n\n", "\n").replace("\t", " ").replace("<|pad|>", " * ").replace("\"\"", "\"").strip() | |
| print("parsed_text = \"" + parsed_text + "\" (seed = " + str(global_seed) + ")") | |
| return parsed_text | |
| gr.Interface( | |
| text_generation, | |
| inputs=gr.Textbox(lines=1, label=".הזינו פה את מילות הפתיחה של הטקסט, בחרו את אחת מן הדוגמאות המוכנות או השאירו ריק. מה שבא לכם. בכל לחיצה על סאבמיט, יווצר טקסט אחר", elem_id="input_text"), | |
| outputs=gr.Textbox(type="text", label="פה מופיע הטקסט שהמחולל יוצר", elem_id="output_text"), | |
| css="#output_text{direction: rtl} #input_text{direction: rtl}", | |
| title=title, | |
| description=description, | |
| article=article, | |
| examples=examples, | |
| cache_examples=False, | |
| allow_flagging='never', | |
| ).launch() |