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 - Control temperture (adjust) through slider (Higher usually better). - Clear and enter new prompt or select another example to regenerate - The '.' means start a new line (your prompt need not be long) - Be patient: this runs on CPU (free tier) - Feedback (Twitter): @akhooli """ def sayPoetry(prompt, temp=0.95): gen = pipe(prompt, max_length=96, temperature = temp)[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') ], 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, theme = 'darkhuggingface') poetry.launch()