Spaces:
Running
Running
Seeded
Browse files
app.py
CHANGED
@@ -1,47 +1,53 @@
|
|
1 |
import gc
|
2 |
import gradio as gr
|
3 |
-
from transformers import pipeline
|
4 |
|
5 |
pipe = pipeline('text-generation', framework='pt', model='akhooli/ap2023', tokenizer='akhooli/ap2023')
|
6 |
#gc.collect()
|
7 |
samples = [['أنت'
|
8 |
-
,1.0, 50, 1.0, 1.0],['هل غادر'
|
9 |
-
,1.0, 50, 1.0, 1.0 ],['ألا ليت'
|
10 |
-
,1.0, 50, 1.0, 1.0 ],['يا قدس'
|
11 |
-
,1.0, 50, 1.0, 1.0],['عيد بأية حال'
|
12 |
-
,1.0, 50, 1.0, 1.0],['لكل شيء إذا ما'
|
13 |
-
,1.0, 50, 1.0, 1.0 ],['.'
|
14 |
-
,1.0, 50, 1.0, 1.0]]
|
15 |
|
16 |
notes = """
|
17 |
- Enter a short prompt or select (click) one of the examples and click SEND
|
18 |
-
- Adjust parameters (temperture, top k, top p and penalty) through the
|
19 |
-
-
|
|
|
20 |
- The '.' means start a new line from no prompt (your prompt need not be long)
|
21 |
-
- Be patient: this
|
22 |
- Feedback (Twitter): @akhooli (https://twitter.com/akhooli/status/1611025232201977859)
|
23 |
-
- Note/Disclaimer:
|
24 |
"""
|
25 |
-
def sayPoetry(prompt, temp=1.0, topk = 50, topp = 1.0, penalty=1.0):
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
poetry =""
|
28 |
for line in gen.split('.')[:-1]:
|
29 |
poetry += line #+ "\n"
|
30 |
return poetry
|
31 |
poetry = gr.Interface(fn=sayPoetry,
|
32 |
inputs=[
|
33 |
-
gr.Textbox(label="Enter
|
34 |
-
gr.Slider(0.
|
35 |
gr.Slider(25, 100, step=1,value=50, label='control top k'),
|
36 |
gr.Slider(0.80, 1.0, step=0.01,value=1.0, label='control top p'),
|
37 |
-
gr.Slider(0.90, 1.
|
|
|
38 |
],
|
39 |
outputs=[gr.Textbox(label="Generated Poetry:")],
|
40 |
|
41 |
allow_flagging='never',
|
42 |
-
title='Arabic Poetry Generation Demo (updated Jan.
|
43 |
-
description = "A simple demo of AI generated poetry based on
|
44 |
examples=samples,
|
45 |
-
|
46 |
article = notes)
|
47 |
-
poetry.launch(
|
|
|
1 |
import gc
|
2 |
import gradio as gr
|
3 |
+
from transformers import pipeline, set_seed
|
4 |
|
5 |
pipe = pipeline('text-generation', framework='pt', model='akhooli/ap2023', tokenizer='akhooli/ap2023')
|
6 |
#gc.collect()
|
7 |
samples = [['أنت'
|
8 |
+
,1.0, 50, 1.0, 1.0, 114],['هل غادر'
|
9 |
+
,1.0, 50, 1.0, 1.0, 114 ],['ألا ليت'
|
10 |
+
,1.0, 50, 1.0, 1.0, 114 ],['يا قدس'
|
11 |
+
,1.0, 50, 1.0, 1.0, 114],['عيد بأية حال'
|
12 |
+
,1.0, 50, 1.0, 1.0, 114],['لكل شيء إذا ما'
|
13 |
+
,1.0, 50, 1.0, 1.0, 114 ],['.'
|
14 |
+
,1.0, 50, 1.0, 1.0, 114]]
|
15 |
|
16 |
notes = """
|
17 |
- Enter a short prompt or select (click) one of the examples and click SEND
|
18 |
+
- Adjust parameters (temperture, top k, top p and penalty) through the slider (keep close to default values).
|
19 |
+
- For the same seed (randomness), the same output is regenerated if other parameters are fixed. Seed should be 0 or more (not empty)
|
20 |
+
- Clear and enter new prompt or select another example and SEND to regenerate
|
21 |
- The '.' means start a new line from no prompt (your prompt need not be long)
|
22 |
+
- Be patient: this runs on CPU (free tier)
|
23 |
- Feedback (Twitter): @akhooli (https://twitter.com/akhooli/status/1611025232201977859)
|
24 |
+
- Note/Disclaimer: may generate unaccepted or inappropriate content. Use at your own risk.
|
25 |
"""
|
26 |
+
def sayPoetry(prompt, temp=1.0, topk = 50, topp = 1.0, penalty=1.0, seed=114):
|
27 |
+
if not int(seed) >= 0: seed=114
|
28 |
+
set_seed(seed)
|
29 |
+
gen = pipe(prompt, max_length=96, do_sample=True, temperature=temp, top_k=topk, top_p=topp, repetition_penalty=penalty,
|
30 |
+
min_length = 64, no_repeat_ngram_size = 3, return_full_text=True,
|
31 |
+
num_beams=5, num_return_sequences=1)[0]["generated_text"]
|
32 |
poetry =""
|
33 |
for line in gen.split('.')[:-1]:
|
34 |
poetry += line #+ "\n"
|
35 |
return poetry
|
36 |
poetry = gr.Interface(fn=sayPoetry,
|
37 |
inputs=[
|
38 |
+
gr.Textbox(label="Enter short prompt or select from examples:"),
|
39 |
+
gr.Slider(0.70, 1.2, step=0.01,value=1.0, label='control temperature'),
|
40 |
gr.Slider(25, 100, step=1,value=50, label='control top k'),
|
41 |
gr.Slider(0.80, 1.0, step=0.01,value=1.0, label='control top p'),
|
42 |
+
gr.Slider(0.90, 1.50, step=0.01,value=1.0, label='control penalty'),
|
43 |
+
gr.Number(value=139750, precision=0, label='Seed'),
|
44 |
],
|
45 |
outputs=[gr.Textbox(label="Generated Poetry:")],
|
46 |
|
47 |
allow_flagging='never',
|
48 |
+
title='Arabic Poetry Generation Demo (updated Jan. 2023)',
|
49 |
+
description = "A simple demo of AI generated poetry based on 1M poems fine-tuned using AraGPT2 (be patient, runs on cpu)",
|
50 |
examples=samples,
|
51 |
+
cache_examples=False,
|
52 |
article = notes)
|
53 |
+
poetry.launch()
|