Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,37 @@ from transformers import AutoTokenizer, AutoModel
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
model = AutoModel.from_pretrained("THUDM/chatglm2-6b-int4",trust_remote_code=True).float()
|
| 10 |
model = model.eval()
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
prompt =
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
response, history = model.chat(tokenizer, prompt, history=[])
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
return
|
| 21 |
|
| 22 |
|
| 23 |
css_style = "#fixed_size_img {height: 240px;} "
|
|
@@ -52,15 +69,17 @@ with gr.Blocks(title=title, css=css_style) as demo:
|
|
| 52 |
with gr.Row():
|
| 53 |
radio_style = gr.Radio(label="功能选择", choices=["小红书笔记", "小红书标题", "公众号文案", "朋友圈微商文案", "商品卖点", "商品描述", "商品种草文案", "商品好评", "广告标题", "创意广告", "产品起名", "视频拍摄剧本", "短视频口播稿", "直播脚本", "短视频拍摄提纲", "SEO文章", "产品slogan", "夸夸机器人", "诗词创作", "作文创作"], value="小红书笔记")
|
| 54 |
with gr.Row():
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
with gr.Row():
|
| 58 |
btn_submit = gr.Button(value="一键创作", elem_id="blue_btn")
|
| 59 |
# btn_clear = gr.Button(value="清除")
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
btn_submit.click(text_generation, inputs=[
|
| 64 |
-
# btn_clear清除画布
|
| 65 |
|
| 66 |
-
demo.queue(api_open=False).launch(debug=True)
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
|
| 7 |
+
#model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True, device='cuda')
|
| 8 |
+
#model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).float()
|
| 9 |
model = AutoModel.from_pretrained("THUDM/chatglm2-6b-int4",trust_remote_code=True).float()
|
| 10 |
model = model.eval()
|
| 11 |
|
| 12 |
+
def generate_prompt(prompt: str, style: str) -> str:
|
| 13 |
+
if len(prompt) == 0:
|
| 14 |
+
return "请输入您的提示语"
|
| 15 |
+
|
| 16 |
+
generated_prompt = '以“' + prompt + '”为主题,撰写一段' + style + ',字数在100字左右'
|
| 17 |
+
print('功能:' + style + ',generated_prompt:' + generated_prompt)
|
| 18 |
+
|
| 19 |
+
return generated_prompt
|
| 20 |
+
|
| 21 |
+
def text_generation(original_prompt: str, generated_prompt: str, style: str) -> str:
|
| 22 |
+
if not len(generated_prompt) == 0:
|
| 23 |
+
prompt = generated_prompt
|
| 24 |
+
elif not len(original_prompt) == 0:
|
| 25 |
+
prompt = original_prompt
|
| 26 |
+
else:
|
| 27 |
+
return "请输入您的提示语"
|
| 28 |
+
|
| 29 |
+
print('功能:' + style + ',提示语:' + prompt)
|
| 30 |
|
| 31 |
response, history = model.chat(tokenizer, prompt, history=[])
|
| 32 |
|
| 33 |
+
result = '按照您的提示语:' + prompt + ',生成的文案如下:\n\n' + response
|
| 34 |
+
|
| 35 |
+
print(result)
|
| 36 |
|
| 37 |
+
return result
|
| 38 |
|
| 39 |
|
| 40 |
css_style = "#fixed_size_img {height: 240px;} "
|
|
|
|
| 69 |
with gr.Row():
|
| 70 |
radio_style = gr.Radio(label="功能选择", choices=["小红书笔记", "小红书标题", "公众号文案", "朋友圈微商文案", "商品卖点", "商品描述", "商品种草文案", "商品好评", "广告标题", "创意广告", "产品起名", "视频拍摄剧本", "短视频口播稿", "直播脚本", "短视频拍摄提纲", "SEO文章", "产品slogan", "夸夸机器人", "诗词创作", "作文创作"], value="小红书笔记")
|
| 71 |
with gr.Row():
|
| 72 |
+
original_prompt = gr.Textbox(label="请输入您的提示语", value="探索西夏:沙漠风情与多元文化的西北之旅")
|
| 73 |
+
rich_prompt = gr.Button("一键丰富提示语")
|
| 74 |
+
generated_prompt = gr.Textbox(label="我们为您生成的提示语", value="以“探索西夏:沙漠风情与多元文化的西北之旅”为主题,撰写一段小红书笔记,字数在100字左右")
|
| 75 |
with gr.Row():
|
| 76 |
btn_submit = gr.Button(value="一键创作", elem_id="blue_btn")
|
| 77 |
# btn_clear = gr.Button(value="清除")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
text_output = gr.Textbox(label="生成文案")
|
| 80 |
|
| 81 |
+
examples = gr.Examples(["探索西夏:沙漠风情与多元文化的西北之旅", "个性iPhone手机壳"], inputs=[original_prompt], outputs=generated_prompt)
|
| 82 |
+
rich_prompt.click(generate_prompt, inputs=[original_prompt, radio_style], outputs=[generated_prompt])
|
| 83 |
+
btn_submit.click(text_generation, inputs=[original_prompt, generated_prompt, radio_style], outputs=text_output)
|
|
|
|
| 84 |
|
| 85 |
+
demo.queue(api_open=False).launch(debug=True, share=True)
|