File size: 2,374 Bytes
2cfeb94
 
 
 
a50edcb
2f068c3
7ede5d0
 
 
 
 
 
 
64c7e4d
a17ca27
3dfb63b
a17ca27
 
7ede5d0
a17ca27
64c7e4d
3dfb63b
 
2cfeb94
bb1772f
50c95fd
2cfeb94
52c0ef5
 
3f0fa7e
3dfb63b
 
 
 
52c0ef5
 
 
 
2cfeb94
 
50c95fd
d38cd8b
52c0ef5
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
import gc 
import gradio as gr
from transformers import pipeline

pipe = pipeline('text-generation', framework='pt', model='akhooli/ap2023', tokenizer='akhooli/ap2023')
#gc.collect()
samples = [['أنت'
           ,.96],['هل غادر'
             , 1.0],['ألا ليت'
                    ,0.99],['يا قدس'
                           , 0.98],['عيد بأية حال'
                                   ,0.94],['لكل شيء إذا ما'
                                          ,0.92],['.',0.99]]
notes = """
- Enter a short prompt or select (click) one of the examples and click SEND 
- Adjust parameters (temperture, top k, top p and penalty) through the slider (keep close to default values).
- Clear and enter new prompt or select another example and SEND to regenerate
- The '.' means start a new line from no prompt (your prompt need not be long) 
- Be patient: this runs on CPU (free tier)
- Feedback (Twitter): @akhooli (https://twitter.com/akhooli/status/1611025232201977859)
"""
def sayPoetry(prompt, temp=0.95, topk = 50, topp = 0.92, penalty=1.0):
    gen = pipe(prompt, max_length=96, temperature = temp, top_k=topk, top_p=topp, repetition_penalty=penalty)[0]["generated_text"]
    poetry =""
    for line in gen.split('.')[:-1]:
        poetry += line + "\n"
    return poetry
poetry = gr.Interface(fn=sayPoetry, 
                    inputs=[
                        gr.inputs.Textbox(label="Enter short prompt or select from examples:", placeholder="أنا الذي"), 
                        gr.inputs.Slider(0.80, 1.0, step=0.01,default=0.95, label='control  temperature'),
                        gr.inputs.Slider(20, 100, step=1,default=50, label='control  top k'),
                        gr.inputs.Slider(0.80, 1.0, step=0.01,default=1.0, label='control  top p'),
                        gr.inputs.Slider(0.89, 1.0, step=0.01,default=1.20, label='control  penalty'),
                    ], 
                    outputs=[gr.outputs.Textbox(label="Generated Poetry:")], 
                    
                    allow_flagging='never',
                   title='Arabic Poetry Generation Demo (updated Jan. 2023)',
                   description = "a simple demo of AI generated poetry based on 1M poems fine-tuned using AraGPT2 (be patient, runs on cpu)",
                   examples=samples,
                article = notes)
poetry.launch()