Spaces:
Runtime error
Runtime error
import tiktoken | |
import openai | |
import os | |
import gradio as gr | |
openai.api_key = os.environ.get("OPENAI_API_KEY") | |
encoding = tiktoken.get_encoding("cl100k_base") | |
def generate_prompt(prompt: str, style: str) -> str: | |
if len(prompt) == 0: | |
return "请输入您的提示语" | |
generated_prompt = '以“' + prompt + '”为主题,撰写一段' + style + ',字数在100字左右' | |
print('功能:' + style + ',generated_prompt:' + generated_prompt) | |
return generated_prompt | |
def text_generation(original_prompt: str, generated_prompt: str, style: str) -> str: | |
if not len(generated_prompt) == 0: | |
prompt = generated_prompt | |
elif not len(original_prompt) == 0: | |
prompt = original_prompt | |
else: | |
return "请输入您的提示语" | |
print('功能:' + style + ',提示语:' + prompt) | |
if len(encoding.encode(prompt)) > 512: | |
return "您的提示语过长,请精简后再试" | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", #gpt-4, gpt-3.5-turbo, text-embedding-ada-002 | |
messages=[{"role": "user", "content": prompt}], | |
temperature=0.5, | |
max_tokens=3580, | |
top_p=1) | |
result = '按照您的提示语:' + prompt + ',生成的文案如下:\n\n' + response["choices"][0]["message"]["content"] | |
print(result) | |
return result | |
css_style = "#fixed_size_img {height: 240px;} " | |
title = "文案创作 by宁侠" | |
description = ''' | |
本服务的主要应用场景涵盖多种文案输入生成和续写,例如用户可以自行输入各种内容,之后服务将会对其进行回答、续写或者按照指令进行回复。 | |
''' | |
with gr.Blocks(title=title, css=css_style) as demo: | |
gr.HTML(''' | |
<div style="text-align: center; max-width: 720px; margin: 0 auto;"> | |
<div | |
style=" | |
display: inline-flex; | |
align-items: center; | |
gap: 0.8rem; | |
font-size: 1.75rem; | |
" | |
> | |
<h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 32px; margin-bottom: 7px;"> | |
文案创作 | |
</h1> | |
<h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 16px; margin-bottom: 7px;"> | |
by宁侠 | |
</h1> | |
</div> | |
</div> | |
''') | |
gr.Markdown(description) | |
with gr.Row(): | |
radio_style = gr.Radio(label="功能选择", choices=["小红书笔记", "小红书标题", "公众号文案", "朋友圈微商文案", "商品卖点", "商品描述", "商品种草文案", "商品好评", "广告标题", "创意广告", "产品起名", "视频拍摄剧本", "短视频口播稿", "直播脚本", "短视频拍摄提纲", "SEO文章", "产品slogan", "夸夸文案", "诗词创作", "作文创作"], value="小红书笔记") | |
with gr.Row(): | |
original_prompt = gr.Textbox(label="请输入您的提示语", value="探索西夏:沙漠风情与多元文化的西北之旅") | |
rich_prompt = gr.Button("一键丰富提示语") | |
generated_prompt = gr.Textbox(label="我们为您生成的提示语", value="以“探索西夏:沙漠风情与多元文化的西北之旅”为主题,撰写一段小红书笔记,字数在100字左右") | |
with gr.Row(): | |
btn_submit = gr.Button(value="一键创作", elem_id="blue_btn") | |
# btn_clear = gr.Button(value="清除") | |
with gr.Row(): | |
text_output = gr.Textbox(label="生成文案") | |
examples = gr.Examples(["探索西夏:沙漠风情与多元文化的西北之旅", "个性iPhone手机壳"], inputs=[original_prompt], outputs=generated_prompt) | |
rich_prompt.click(generate_prompt, inputs=[original_prompt, radio_style], outputs=[generated_prompt]) | |
btn_submit.click(text_generation, inputs=[original_prompt, generated_prompt, radio_style], outputs=text_output) | |
demo.queue(api_open=False).launch(debug=True) |