from transformers import pipeline model1 = pipeline("text-generation",model="uer/gpt2-chinese-poem") from opencc import OpenCC t2s=OpenCC('t2s').convert s2t=OpenCC('s2t').convert import jiagu def poem_generator(txt,n=100): model_input=t2s(txt) poem=model1(model_input, max_length=n)[0]['generated_text'] poem="".join(poem.split()) sentiment = jiagu.sentiment(poem) return s2t(poem),sentiment[0] import gradio as gr inp1=gr.inputs.Textbox(label="請輸入一段詩詞") inp2=gr.inputs.Slider(minimum=50,maximum=250,step=5,default=150, label="生成詩詞長度") op1=gr.outputs.Textbox(label="生成詩詞結果") op2=gr.outputs.Textbox(label="情感分析(positive=偏正面, negative=偏負面)") iface=gr.Interface(fn=poem_generator, inputs=[inp1,inp2], outputs=[op1,op2], title="繁體中文詩詞生成Web APP", description="請輸入一段文字,我會幫你繼續生成詩詞,還會幫你分析這整段詩詞是偏正面或負面喔!") iface.launch()