Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,3 @@
|
|
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
|
@@ -13,12 +6,27 @@ import gradio as gr
|
|
13 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
14 |
encoding = tiktoken.get_encoding("cl100k_base")
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
@@ -27,9 +35,9 @@ def text_generation(prompt: str, style: str) -> str:
|
|
27 |
max_tokens=3580,
|
28 |
top_p=1)
|
29 |
|
30 |
-
result = response["choices"][0]["message"]["content"]
|
31 |
|
32 |
-
print(
|
33 |
|
34 |
return result
|
35 |
|
@@ -65,18 +73,19 @@ with gr.Blocks(title=title, css=css_style) as demo:
|
|
65 |
|
66 |
gr.Markdown(description)
|
67 |
with gr.Row():
|
68 |
-
|
69 |
-
radio_style = gr.Radio(label="功能选择", choices=["小红书笔记", "小红书标题", "公众号文案", "朋友圈微商文案", "商品卖点", "商品描述", "商品种草文案", "商品好评", "广告标题", "创意广告", "产品起名", "视频拍摄剧本", "短视频口播稿", "直播脚本", "短视频拍摄提纲", "SEO文章", "产品slogan", "夸夸机器人", "诗词创作", "作文创作"], value="小红书笔记")
|
70 |
with gr.Row():
|
71 |
-
|
72 |
-
|
|
|
73 |
with gr.Row():
|
74 |
btn_submit = gr.Button(value="一键创作", elem_id="blue_btn")
|
75 |
# btn_clear = gr.Button(value="清除")
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
btn_submit.click(text_generation, inputs=[
|
80 |
-
# btn_clear清除画布
|
81 |
|
82 |
-
demo.queue(api_open=False).launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import tiktoken
|
2 |
import openai
|
3 |
import os
|
|
|
6 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
7 |
encoding = tiktoken.get_encoding("cl100k_base")
|
8 |
|
9 |
+
def generate_prompt(prompt: str, style: str) -> str:
|
10 |
+
if len(prompt) == 0:
|
11 |
+
return "请输入您的提示语"
|
12 |
+
|
13 |
+
generated_prompt = '以“' + prompt + '”为主题,撰写一段' + style + ',字数在100字左右'
|
14 |
+
print('功能:' + style + ',generated_prompt:' + generated_prompt)
|
15 |
+
|
16 |
+
return generated_prompt
|
17 |
+
|
18 |
+
def text_generation(original_prompt: str, generated_prompt: str, style: str) -> str:
|
19 |
+
if not len(generated_prompt) == 0:
|
20 |
+
prompt = generated_prompt
|
21 |
+
elif not len(original_prompt) == 0:
|
22 |
+
prompt = original_prompt
|
23 |
+
else:
|
24 |
+
return "请输入您的提示语"
|
25 |
+
|
26 |
+
print('功能:' + style + ',提示语:' + prompt)
|
27 |
|
28 |
if len(encoding.encode(prompt)) > 512:
|
29 |
+
return "您的提示语过长,请精简后再试"
|
30 |
|
31 |
response = openai.ChatCompletion.create(
|
32 |
model="gpt-3.5-turbo", #gpt-4, gpt-3.5-turbo, text-embedding-ada-002
|
|
|
35 |
max_tokens=3580,
|
36 |
top_p=1)
|
37 |
|
38 |
+
result = '按照您的提示语:' + prompt + ',生成的文案如下:\n\n' + response["choices"][0]["message"]["content"]
|
39 |
|
40 |
+
print(result)
|
41 |
|
42 |
return result
|
43 |
|
|
|
73 |
|
74 |
gr.Markdown(description)
|
75 |
with gr.Row():
|
76 |
+
radio_style = gr.Radio(label="功能选择", choices=["小红书笔记", "小红书标题", "公众号文案", "朋友圈微商文案", "商品卖点", "商品描述", "商品种草文案", "商品好评", "广告标题", "创意广告", "产品起名", "视频拍摄剧本", "短视频口播稿", "直播脚本", "短视频拍摄提纲", "SEO文章", "产品slogan", "夸夸文案", "诗词创作", "作文创作"], value="小红书笔记")
|
|
|
77 |
with gr.Row():
|
78 |
+
original_prompt = gr.Textbox(label="请输入您的提示语", value="探索西夏:沙漠风情与多元文化的西北之旅")
|
79 |
+
rich_prompt = gr.Button("一键丰富提示语")
|
80 |
+
generated_prompt = gr.Textbox(label="我们为您生成的提示语", value="以“探索西夏:沙漠风情与多元文化的西北之旅”为主题,撰写一段小红书笔记,字数在100字左右")
|
81 |
with gr.Row():
|
82 |
btn_submit = gr.Button(value="一键创作", elem_id="blue_btn")
|
83 |
# btn_clear = gr.Button(value="清除")
|
84 |
+
with gr.Row():
|
85 |
+
text_output = gr.Textbox(label="生成文案")
|
86 |
|
87 |
+
examples = gr.Examples(["探索西夏:沙漠风情与多元文化的西北之旅", "个性iPhone手机壳"], inputs=[original_prompt], outputs=generated_prompt)
|
88 |
+
rich_prompt.click(generate_prompt, inputs=[original_prompt, radio_style], outputs=[generated_prompt])
|
89 |
+
btn_submit.click(text_generation, inputs=[original_prompt, generated_prompt, radio_style], outputs=text_output)
|
|
|
90 |
|
91 |
+
demo.queue(api_open=False).launch(debug=True)
|