File size: 3,008 Bytes
0fd7b28
e85570c
 
1e30ecb
e85570c
 
 
cabc27e
f065c0b
e85570c
 
 
 
 
 
 
 
 
 
 
dc3475e
 
0fd7b28
dc3475e
e528373
 
dd510c4
42a4fb9
dd510c4
2f1925b
8ed4b31
 
dc3475e
8ed4b31
a90174f
63e1766
 
5a8655c
a90174f
0fd7b28
e85570c
55c27a9
a90174f
b3de19a
e85570c
 
ce452da
22bbf6e
06de096
 
a3ae48f
e85570c
0002bbd
 
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
45
46
47
48
49
50
51
52
53
54
import gradio as gr 
from transformers import AutoTokenizer, TFGPT2LMHeadModel

review_model = TFGPT2LMHeadModel.from_pretrained("kmkarakaya/turkishReviews-ds")
review_tokenizer = AutoTokenizer.from_pretrained("kmkarakaya/turkishReviews-ds")

def generate_review(prompt):
  if prompt=="":
     prompt = " "
  input_ids = review_tokenizer.encode(prompt, return_tensors='tf')
  context_length = 40
  output = review_model.generate(
      input_ids, 
      do_sample=True,
      max_length=context_length, 
      top_k=10,
      no_repeat_ngram_size=2, 
      early_stopping=True
  )
  return(review_tokenizer.decode(output[0], skip_special_tokens=True))
  
  

title="Turkish Review Generator: A GPT2 based Text Generator Trained with a Custom Dataset"
description= """Generate a review in Turkish by providing a prompt or selecting an example prompt below. 
Generation takes <b>15-20 seconds</b> on average.
Enjoy!

<p>NOTE: Examples can sometimes  generate ERROR. When you see ERROR on the screen <b>just click SUBMIT</b>. Model will generate text in 15-20 secs.</p> """
article = """<p style='text-align: center'>On YouTube:</p>
            <p style='text-align: center'><a href='https://youtube.com/playlist?list=PLQflnv_s49v9d9w-L0S8XUXXdNks7vPBL' target='_blank'>How to Train a Hugging Face Causal Language Model from Scratch with a Custom Dataset and a Custom Tokenizer?</a></p> 
            <p style='text-align: center'><a href='https://youtube.com/playlist?list=PLQflnv_s49v8aajw6m9MRNbAAbL63flKD' target='_blank'>Hugging Face kütüphanesini kullanarak bir GPT2 Transformer Dil Modelini Kendi Veri Setimizle nasıl eğitip kullanabiliriz? (in Turkish)</a></p>
            <p style='text-align: center'>On Medium:</p>
            <p style='text-align: center'><a href='https://medium.com/deep-learning-with-keras/how-to-train-a-hugging-face-causal-language-model-from-scratch-8d08d038168f' target='_blank'>How to Train a Hugging Face Causal Language Model from Scratch with a Custom Dataset and a Custom Tokenizer?</a></p>"""                    
examples=["Bir hafta önce aldığım cep telefonu",
          "Tatil için rezervasyon yaptırdım",
          "Geçen ay sipariş verdiğim",
          "Spor salonuna abone oldum"] 


demo = gr.Interface(fn=generate_review, 
                    inputs= gr.Textbox(lines=5, default= "Geçen ay sipariş verdiğim", label="Prompt", placeholder="enter or select a prompt below..."),
                    outputs= gr.Textbox(lines=5, label="Generated Review", placeholder="genereated review will be here..."),
                    examples=examples,
                    title=title,
                    description= description,
                    article = article,
                    #cache_examples = False
                    #allow_flagging="manual",
                    #flagging_options=["good","moderate", "non-sense", ]
                    #flagging_dir='./flags'
                    )
#demo.launch('share=True', 'enable_queue=True')
demo.launch()