Spaces:
Build error
Build error
mareloraby
commited on
Commit
·
90a2a3f
1
Parent(s):
7af287c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import BertTokenizer, EncoderDecoderModel
|
3 |
+
|
4 |
+
tokenizerM = BertTokenizer.from_pretrained("mareloraby/BERTShared-meter2poem-arV01")
|
5 |
+
bertSharedM = EncoderDecoderModel.from_pretrained("mareloraby/BERTShared-meter2poem-arV01")
|
6 |
+
|
7 |
+
def generate_response(text, k = 70, p = 0.9, nb = 4):
|
8 |
+
# meters = set(['الرمل','البسيط','الخفيف','الكامل','السريع','الطويل','المتقارب','الرجز','المجتث','المنسرح','الوافر','المقتضب','الهزج','المديد','المضارع'])
|
9 |
+
prompt = f"{text}"
|
10 |
+
encoded_prompt = tokenizerM.encode_plus(prompt, return_tensors = 'pt').to(device)
|
11 |
+
gneration = bertSharedM.generate(
|
12 |
+
input_ids = encoded_prompt.input_ids,
|
13 |
+
attention_mask = encoded_prompt.attention_mask,
|
14 |
+
do_sample = True,
|
15 |
+
top_k= k,
|
16 |
+
top_p = p,
|
17 |
+
num_beams= nb,
|
18 |
+
max_length =130,
|
19 |
+
repetition_penalty = 2.0,
|
20 |
+
no_repeat_ngram_size = 2,
|
21 |
+
early_stopping=True)
|
22 |
+
|
23 |
+
generated_text = tokenizerM.decode(gneration[0], skip_special_tokens=True)
|
24 |
+
bayts = generated_text.split("[SOB]")
|
25 |
+
while("BSEP" not in bayts[-1]):
|
26 |
+
bayts = bayts[:-1]
|
27 |
+
# if(len(bayts[-1]) < 2):
|
28 |
+
# bayts = bayts[:-1]
|
29 |
+
bayts = bayts[:-1]
|
30 |
+
temp_poem = ''
|
31 |
+
for b in range(len(bayts)):
|
32 |
+
temp_line = bayts[b].split('[BSEP]')
|
33 |
+
temp_poem = temp_poem + temp_line[1] + ' - ' + temp_line[0] +'\n'
|
34 |
+
|
35 |
+
return temp_poem
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
gr.Interface(fn=generate_response,
|
40 |
+
title = 'BERTShared - meter based generation',
|
41 |
+
# description ='''
|
42 |
+
# topics : ['حزينه','هجاء','عتاب','غزل','مدح','رومنسيه','دينية','وطنيه']
|
43 |
+
# ''',
|
44 |
+
inputs=[
|
45 |
+
gr.inputs.Radio(['الرمل','البسيط','الخفيف','الكامل','السريع','الطويل','المتقارب','الرجز','المجتث','المنسرح','الوافر','المقتضب','الهزج','المديد','المضارع'],label='Choose Meter'),
|
46 |
+
gr.inputs.Slider(10, 200, step=10,default = 70, label='Top-K'),
|
47 |
+
gr.inputs.Slider(0.10, 0.99, step=0.02, default = 0.90, label='Top-P'),
|
48 |
+
gr.inputs.Slider(1, 20, step=1, default = 4, label='Beams'),
|
49 |
+
],
|
50 |
+
outputs="text").launch()
|