Spaces:
Runtime error
Runtime error
Add application file
Browse files- app.py +82 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
#pip install openai
|
5 |
+
#gpt-3.5-turbo
|
6 |
+
#依赖tiktoken:pip install tiktoken,git+https://github.com/openai/tiktoken.git,使用不同的 GPT 模型,对应着不同的 Tiktoken 的编码器模型:https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb。
|
7 |
+
|
8 |
+
import tiktoken
|
9 |
+
import openai
|
10 |
+
import os
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
14 |
+
encoding = tiktoken.get_encoding("cl100k_base")
|
15 |
+
|
16 |
+
def text_generation(prompt: str, style: str) -> str:
|
17 |
+
prompt = '以“' + prompt + '”为主题,撰写一段' + style + ',字数在100字左右'
|
18 |
+
print('功能:' + style + ',提示文案:' + prompt)
|
19 |
+
|
20 |
+
if len(encoding.encode(prompt)) > 512:
|
21 |
+
return "输入的文案过长,请精简后再试"
|
22 |
+
|
23 |
+
response = openai.ChatCompletion.create(
|
24 |
+
model="gpt-3.5-turbo", #gpt-4, gpt-3.5-turbo, text-embedding-ada-002
|
25 |
+
messages=[{"role": "user", "content": prompt}],
|
26 |
+
temperature=0.5,
|
27 |
+
max_tokens=3580,
|
28 |
+
top_p=1)
|
29 |
+
|
30 |
+
result = response["choices"][0]["message"]["content"]
|
31 |
+
|
32 |
+
print('生成文案:' + result)
|
33 |
+
|
34 |
+
return result
|
35 |
+
|
36 |
+
|
37 |
+
css_style = "#fixed_size_img {height: 240px;} "
|
38 |
+
|
39 |
+
|
40 |
+
title = "文案创作 by宁侠"
|
41 |
+
description = '''
|
42 |
+
本服务的主要应用场景涵盖多种文案输入生成和续写,例如用户可以自行输入各种内容,之后服务将会对其进行回答、续写或者按照指令进行回复。
|
43 |
+
'''
|
44 |
+
|
45 |
+
with gr.Blocks(title=title, css=css_style) as demo:
|
46 |
+
gr.HTML('''
|
47 |
+
<div style="text-align: center; max-width: 720px; margin: 0 auto;">
|
48 |
+
<div
|
49 |
+
style="
|
50 |
+
display: inline-flex;
|
51 |
+
align-items: center;
|
52 |
+
gap: 0.8rem;
|
53 |
+
font-size: 1.75rem;
|
54 |
+
"
|
55 |
+
>
|
56 |
+
<h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 32px; margin-bottom: 7px;">
|
57 |
+
文案创作
|
58 |
+
</h1>
|
59 |
+
<h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 16px; margin-bottom: 7px;">
|
60 |
+
by宁侠
|
61 |
+
</h1>
|
62 |
+
</div>
|
63 |
+
</div>
|
64 |
+
''')
|
65 |
+
|
66 |
+
gr.Markdown(description)
|
67 |
+
with gr.Row():
|
68 |
+
# radio_style = gr.Radio(label="模型选择", choices=["中文-base", "中文-large", "中文-1.3B", "中文-2.7B", "中文-13B", "中文-30B", "广告文案", "夸夸机器人", "诗词创作", "作文创作"], value="中文-base")
|
69 |
+
radio_style = gr.Radio(label="功能选择", choices=["小红书笔记", "小红书标题", "公众号文案", "朋友圈微商文案", "商品卖点", "商品描述", "商品种草文案", "商品好评", "广告标题", "创意广告", "产品起名", "视频拍摄剧本", "短视频口播稿", "直播脚本", "短视频拍摄提纲", "SEO文章", "产品slogan", "夸夸机器人", "诗词创作", "作文创作"], value="小红书笔记")
|
70 |
+
with gr.Row():
|
71 |
+
text_input = gr.Textbox(label="提示文案", value="探索西夏:沙漠风情与多元文化的西北之旅")
|
72 |
+
text_output = gr.Textbox(label="生成文案")
|
73 |
+
with gr.Row():
|
74 |
+
btn_submit = gr.Button(value="一键创作", elem_id="blue_btn")
|
75 |
+
# btn_clear = gr.Button(value="清除")
|
76 |
+
|
77 |
+
# examples = gr.Examples(["以“个性iPhone手机壳”为主题,撰写一段朋友圈微商文案,字数在100字左右", "美化这句话:本服务主要应用于多种场景文案输入的生成和续写,比如用户可以自行尝试输入各种内容,然后让服务去回答、续写或者根据指令回复。"], inputs=[text_input], outputs=text_output)
|
78 |
+
examples = gr.Examples(["探索西夏:沙漠风情与多元文化的西北之旅", "个性iPhone手机壳"], inputs=[text_input], outputs=text_output)
|
79 |
+
btn_submit.click(text_generation, inputs=[text_input, radio_style], outputs=text_output)
|
80 |
+
# btn_clear清除画布
|
81 |
+
|
82 |
+
demo.queue(api_open=False).launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
tiktoken
|