glt3953 commited on
Commit
b6fad30
·
1 Parent(s): 3b2b507

Upload app.py

Browse files

Add application file

Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pip install "modelscope[cv]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
2
+ #pip install gradio
3
+ #pip install tensorflow
4
+ #requirements.txt中包含以下依赖库:
5
+ #protobuf==3.20.0
6
+ #transformers==4.27.1
7
+ #icetk
8
+ #cpm_kernels
9
+
10
+ import gradio as gr
11
+ import os
12
+
13
+ os.system(
14
+ 'pip install --upgrade torch'
15
+ )
16
+
17
+ os.system(
18
+ 'pip install "modelscope" --upgrade -f https://pypi.org/project/modelscope/'
19
+ )
20
+
21
+ os.system('pip install transformers -U')
22
+
23
+ from modelscope.pipelines import pipeline
24
+ from modelscope.utils.constant import Tasks
25
+
26
+ #pipe = pipeline(Tasks.text_generation, model='damo/nlp_gpt3_text-generation_13B')
27
+ # pipe = pipeline(task=Tasks.chat, model='ZhipuAI/ChatGLM-6B', model_revision='v1.0.16', device_map='auto')
28
+ pipe = pipeline(task=Tasks.chat, model='ZhipuAI/chatglm2-6b', model_revision='v1.0.2', device_map='auto')
29
+ # import torch
30
+ # model = torch.hub.load('ZhipuAI', 'chatglm2-6b', offload_folder='/root/.cache/modelscope/hub')
31
+ # pipe = pipeline(task=Tasks.chat, model=model, model_revision='v1.0.2',device_map='auto')
32
+
33
+ def text_generation(prompt: str, style: str) -> str:
34
+ prompt = '以“' + prompt + '”为主题,撰写一段' + style + ',字数在100字左右'
35
+ print('功能:' + style + ',提示文案:' + prompt)
36
+
37
+ # result = pipe(prompt)
38
+
39
+ inputs = {'text':prompt, 'history': []}
40
+ result = pipe(inputs)
41
+
42
+ # print('生成文案:' + result['text'])
43
+ print('生成文案:' + result['response'])
44
+
45
+ # return result['text']
46
+ return result['response']
47
+
48
+
49
+ css_style = "#fixed_size_img {height: 240px;} "
50
+
51
+ title = "AI文案 by宁侠"
52
+ description = '''
53
+ 本服务的主要应用场景涵盖多种文案输入生成和续写,例如用户可以自行输入各种内容,之后服务将会对其进行回答、续写或者按照指令进行回复。
54
+ '''
55
+
56
+ with gr.Blocks(title=title, css=css_style) as demo:
57
+ gr.HTML('''
58
+ <div style="text-align: center; max-width: 720px; margin: 0 auto;">
59
+ <div
60
+ style="
61
+ display: inline-flex;
62
+ align-items: center;
63
+ gap: 0.8rem;
64
+ font-size: 1.75rem;
65
+ "
66
+ >
67
+ <h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 32px; margin-bottom: 7px;">
68
+ AI文案
69
+ </h1>
70
+ <h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 16px; margin-bottom: 7px;">
71
+ by宁侠
72
+ </h1>
73
+ </div>
74
+ </div>
75
+ ''')
76
+
77
+ gr.Markdown(description)
78
+ with gr.Row():
79
+ # radio_style = gr.Radio(label="模型选择", choices=["中文-base", "中文-large", "中文-1.3B", "中文-2.7B", "中文-13B", "中文-30B", "广告文案", "夸夸机器人", "诗词创作", "作文创作"], value="中文-base")
80
+ radio_style = gr.Radio(label="功能选择", choices=["小红书笔记", "小红书标题", "公众号文案", "朋友圈微商文案", "商品卖点", "商品描述", "商品种草文案", "商品好评", "广告标题", "创意广告", "产品起名", "视频拍摄剧本", "短视频口播稿", "直播脚本", "短视频拍摄提纲", "SEO文章", "产品slogan", "夸夸机器人", "诗词创作", "作文创作"], value="小红书笔记")
81
+ with gr.Row():
82
+ text_input = gr.Textbox(label="提示文案", value="探索西夏:沙漠风情与多元文化的西北之旅")
83
+ text_output = gr.Textbox(label="AI创作")
84
+ with gr.Row():
85
+ btn_submit = gr.Button(value="一键创作", elem_id="blue_btn")
86
+ # btn_clear = gr.Button(value="清除")
87
+
88
+ # examples = gr.Examples(["以“个性iPhone手机壳”为主题,撰写一段朋友圈微商文案,字数在100字左右", "美化这句话:本服务主要应用于多种场景文案输入的生成和续写,比如用户可以自行尝试输入各种内容,然后让服务去回答、续写或者根据指令回复。"], inputs=[text_input], outputs=text_output)
89
+ examples = gr.Examples(["探索西夏:沙漠风情与多元文化的西北之旅", "个性iPhone手机壳"], inputs=[text_input], outputs=text_output)
90
+ btn_submit.click(text_generation, inputs=[text_input, radio_style], outputs=text_output)
91
+ # btn_clear清除画布
92
+
93
+ demo.queue(api_open=False).launch(debug=True)