Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
#GPT2 FINE TUNE
|
4 |
+
|
5 |
+
# library
|
6 |
+
import gradio as gr
|
7 |
+
import tensorflow as tf
|
8 |
+
from transformers import pipeline, AutoTokenizer, TFGPT2LMHeadModel
|
9 |
+
|
10 |
+
|
11 |
+
# function to run
|
12 |
+
def run_model(input_text,
|
13 |
+
max_length,
|
14 |
+
length_penalty):
|
15 |
+
|
16 |
+
#MBART Transformer
|
17 |
+
model_gpt2 = TFGPT2LMHeadModel.from_pretrained("Sultannn/gpt2-ft-id-puisi")
|
18 |
+
token_gpt2 = AutoTokenizer.from_pretrained("Sultannn/gpt2-ft-id-puisi")
|
19 |
+
|
20 |
+
# preprocessing text input
|
21 |
+
input_text = str(input_text)
|
22 |
+
input_text = ' '.join(input_text.split()).upper() # hapus white space dan ubah judul kata ke huruf besar
|
23 |
+
|
24 |
+
#encode input to vector
|
25 |
+
text_prompt = token_gpt2.encode(input_text,add_special_tokens=True, return_tensors="tf")
|
26 |
+
|
27 |
+
|
28 |
+
#generate input
|
29 |
+
summary_ids = model_gpt2.generate(text_prompt, # raw token
|
30 |
+
# param penting
|
31 |
+
max_length = max_length + len(input_text), # max token to generate
|
32 |
+
do_sample = True, # to sampling text
|
33 |
+
|
34 |
+
length_penalty = length_penalty, #Atur ke nilai <1.0 untuk menghasilkan urutan yang lebih pendek, ke nilai > 1.0 untuk menghasilkan urutan yang lebih panjang)
|
35 |
+
|
36 |
+
# pencarian modern
|
37 |
+
top_k = 50, # ambil top kata dengan probability tertinggi / kata yang paling mungkin
|
38 |
+
top_p = 0.95, # ambil kata dan jumlah kan probalitiy nya sesuai yang di definisikan
|
39 |
+
|
40 |
+
# no_repeat_ngram_size=2 , # agar tidak ada 2 gram/kata yang muncul dua kali:
|
41 |
+
# temperature=1.0, # untuk mengatur probability next word
|
42 |
+
|
43 |
+
# trick / metode pencarian tradisional
|
44 |
+
# num_beams=3, # Pencarian mengurangi risiko hilangnya urutan kata probabilitas tinggi yang tersembunyi dengan menjaga hipotesis yang paling mungkin pada setiap langkah waktu dan akhirnya memilih hipotesis yang memiliki probabilitas tertinggi secara keseluruhan
|
45 |
+
# early_stopping=True, # gunakan jika pakai num_beams > 1
|
46 |
+
# repetition_penalty=1.0, # mencegah kata pengulangan
|
47 |
+
|
48 |
+
# jika ingin lebih dari 1 output
|
49 |
+
# num_return_sequences=3,# num_return_sequences <= num_beams! jumlah balok skor tertinggi yang harus dikembalikan ? jumlah sample yang ingin dikeluarkan sesuai yang didefinisikan
|
50 |
+
)
|
51 |
+
|
52 |
+
|
53 |
+
#decode output to text
|
54 |
+
output = token_gpt2.decode(summary_ids[0], clean_up_tokenization_spaces=False)
|
55 |
+
|
56 |
+
return output # get output to str
|
57 |
+
|
58 |
+
# end
|
59 |
+
|
60 |
+
#example
|
61 |
+
contoh = [["TAMPAN"],["TIDAK JELAS"]]
|
62 |
+
|
63 |
+
#judul
|
64 |
+
title = "Generate Puisi Indonesia"
|
65 |
+
|
66 |
+
#deskripsi
|
67 |
+
description = "Demo for Puisi Generator ID. Models are GPT-2"
|
68 |
+
|
69 |
+
#footer
|
70 |
+
article = "<p style='text-align: center'><a href='https://github.com/sultanbst123/Text_summarization-id2id' target='_blank'><u>Untuk penjelasan lihat di repo ku</u> 😁</a></p>"
|
71 |
+
|
72 |
+
#run gradio
|
73 |
+
gr.Interface(
|
74 |
+
fn=run_model,
|
75 |
+
#input text
|
76 |
+
inputs=[
|
77 |
+
gr.inputs.Textbox(
|
78 |
+
lines=2,
|
79 |
+
placeholder="Ketik disini...",
|
80 |
+
label="Text",
|
81 |
+
),
|
82 |
+
#fine tune
|
83 |
+
|
84 |
+
#max length
|
85 |
+
gr.inputs.Slider(
|
86 |
+
minimum=100,
|
87 |
+
maximum=150,
|
88 |
+
step=5,
|
89 |
+
default=100,
|
90 |
+
label="Max Length(panjang maksimum urutan)",
|
91 |
+
),
|
92 |
+
#length_penalty
|
93 |
+
gr.inputs.Slider(
|
94 |
+
minimum=1,
|
95 |
+
maximum=3,
|
96 |
+
step=1,
|
97 |
+
default=1,
|
98 |
+
label="Length Penalty(pilih 1 jika ingin yang panjang)",
|
99 |
+
),
|
100 |
+
],
|
101 |
+
#output text
|
102 |
+
outputs=
|
103 |
+
gr.outputs.Textbox(
|
104 |
+
label="Output text",
|
105 |
+
),
|
106 |
+
title=title,
|
107 |
+
description=description,
|
108 |
+
article=article,
|
109 |
+
examples=contoh).launch(debug = True)
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
## GOOD LUCK
|