Spaces:
Running
Running
PAseer-PromptsApp.py
Browse files
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import openai
|
4 |
+
import os
|
5 |
+
|
6 |
+
# ========== 默认选项和数据 ==========
|
7 |
+
EXPRESSIONS = ["smiling", "determined", "surprised", "serene"]
|
8 |
+
ITEMS = ["magic wand", "sword", "flower", "book of spells", "ancient scroll", "music instrument"]
|
9 |
+
OTHER_DETAILS = ["sparkles", "magical aura", "lens flare", "fireworks in the background"]
|
10 |
+
SCENES = ["sunset beach", "rainy city street at night", "fantasy forest with glowing mushrooms", "futuristic skyline at dawn"]
|
11 |
+
CAMERA_ANGLES = ["low-angle shot", "close-up shot", "bird's-eye view", "wide-angle shot"]
|
12 |
+
QUALITY_PROMPTS = ["8k", "ultra-realistic", "high detail", "cinematic lighting", "award-winning"]
|
13 |
+
|
14 |
+
# ========== 工具函数 ==========
|
15 |
+
def load_candidates_from_file(file):
|
16 |
+
"""
|
17 |
+
从上传的文件中读取候选项。
|
18 |
+
"""
|
19 |
+
if file is None:
|
20 |
+
return []
|
21 |
+
lines = file.decode("utf-8").split("\n")
|
22 |
+
return [line.strip() for line in lines if line.strip()]
|
23 |
+
|
24 |
+
def get_random_item(candidates):
|
25 |
+
"""
|
26 |
+
随机选取候选项。
|
27 |
+
"""
|
28 |
+
return random.choice(candidates) if candidates else ""
|
29 |
+
|
30 |
+
def generate_natural_language_description(tags, api_key=None):
|
31 |
+
"""
|
32 |
+
生成自然语言描述,可以选择使用 GPT 或手写逻辑。
|
33 |
+
"""
|
34 |
+
if api_key:
|
35 |
+
try:
|
36 |
+
openai.api_key = api_key.strip()
|
37 |
+
response = openai.ChatCompletion.create(
|
38 |
+
model="gpt-4",
|
39 |
+
messages=[
|
40 |
+
{"role": "system", "content": (
|
41 |
+
"You are a helpful assistant that generates creative painting/prompt descriptions. "
|
42 |
+
"Write at least three sentences in English, separated by periods."
|
43 |
+
)},
|
44 |
+
{"role": "user", "content": f"Here are the tags: {tags}\nPlease generate a vivid, imaginative scene description."}
|
45 |
+
],
|
46 |
+
max_tokens=300,
|
47 |
+
temperature=0.8,
|
48 |
+
)
|
49 |
+
return response["choices"][0]["message"]["content"].strip()
|
50 |
+
except Exception as e:
|
51 |
+
return f"GPT generation failed. Error: {e}"
|
52 |
+
else:
|
53 |
+
# 本地逻辑生成
|
54 |
+
return (
|
55 |
+
f"In this scene, {tags.get('character_name', 'the character')} appears in a {tags.get('scene', 'mysterious place')}, "
|
56 |
+
f"captured from a {tags.get('camera_angle', 'unique angle')} perspective. "
|
57 |
+
f"The overall style combines {tags.get('artist_prompt', 'an unknown artist')} and {tags.get('style', 'a distinctive aesthetic')}, "
|
58 |
+
f"bringing a captivating atmosphere to the artwork. They are {tags.get('action', 'doing something')} "
|
59 |
+
f"with a {tags.get('expression', 'neutral expression')}, holding {tags.get('items', 'something')} among {tags.get('other_details', 'subtle details')}."
|
60 |
+
)
|
61 |
+
|
62 |
+
def generate_prompt(action_file, style_file, artist_file, character_file, api_key, selected_categories):
|
63 |
+
"""
|
64 |
+
生成随机提示词和描述。
|
65 |
+
"""
|
66 |
+
# 从文件加载候选项
|
67 |
+
actions = load_candidates_from_file(action_file) if action_file else []
|
68 |
+
styles = load_candidates_from_file(style_file) if style_file else []
|
69 |
+
artists = load_candidates_from_file(artist_file) if artist_file else []
|
70 |
+
characters = load_candidates_from_file(character_file) if character_file else []
|
71 |
+
|
72 |
+
# 确定角色类型
|
73 |
+
number_of_characters = ", ".join(selected_categories) if selected_categories else random.choice(["1girl", "1boy"])
|
74 |
+
|
75 |
+
# 随机生成提示词
|
76 |
+
tags = {
|
77 |
+
"number_of_characters": number_of_characters,
|
78 |
+
"character_name": get_random_item(characters),
|
79 |
+
"artist_prompt": get_random_item(artists),
|
80 |
+
"style": get_random_item(styles),
|
81 |
+
"scene": get_random_item(SCENES),
|
82 |
+
"camera_angle": get_random_item(CAMERA_ANGLES),
|
83 |
+
"action": get_random_item(actions),
|
84 |
+
"expression": get_random_item(EXPRESSIONS),
|
85 |
+
"items": get_random_item(ITEMS),
|
86 |
+
"other_details": get_random_item(OTHER_DETAILS),
|
87 |
+
"quality_prompts": get_random_item(QUALITY_PROMPTS),
|
88 |
+
}
|
89 |
+
|
90 |
+
# 生成描述
|
91 |
+
description = generate_natural_language_description(tags, api_key)
|
92 |
+
|
93 |
+
# 返回结果
|
94 |
+
tags_list = [value for value in tags.values() if value]
|
95 |
+
final_tags = ", ".join(tags_list)
|
96 |
+
return final_tags, description
|
97 |
+
|
98 |
+
# ========== Gradio 界面 ==========
|
99 |
+
def gradio_interface():
|
100 |
+
"""
|
101 |
+
定义 Gradio 应用界面。
|
102 |
+
"""
|
103 |
+
with gr.Blocks() as demo:
|
104 |
+
gr.Markdown("## Random Prompt Generator with User-Provided GPT API Key")
|
105 |
+
|
106 |
+
# API Key 输入区
|
107 |
+
api_key_input = gr.Textbox(
|
108 |
+
label="Enter your OpenAI API Key (Optional)",
|
109 |
+
placeholder="sk-...",
|
110 |
+
type="password"
|
111 |
+
)
|
112 |
+
|
113 |
+
# 文件上传
|
114 |
+
with gr.Row():
|
115 |
+
action_file = gr.File(label="Upload Action File (Optional)", file_types=[".txt"])
|
116 |
+
style_file = gr.File(label="Upload Style File (Optional)", file_types=[".txt"])
|
117 |
+
artist_file = gr.File(label="Upload Artist File (Optional)", file_types=[".txt"])
|
118 |
+
character_file = gr.File(label="Upload Character File (Optional)", file_types=[".txt"])
|
119 |
+
|
120 |
+
# 角色类型选择
|
121 |
+
selected_categories = gr.CheckboxGroup(
|
122 |
+
["1boy", "1girl", "furry", "mecha", "fantasy monster", "animal", "still life"],
|
123 |
+
label="Choose Character Categories (Optional)"
|
124 |
+
)
|
125 |
+
|
126 |
+
# 输出区域
|
127 |
+
with gr.Row():
|
128 |
+
tags_output = gr.Textbox(label="Generated Tags")
|
129 |
+
description_output = gr.Textbox(label="Generated Description")
|
130 |
+
|
131 |
+
# 按钮
|
132 |
+
generate_button = gr.Button("Generate Prompt")
|
133 |
+
|
134 |
+
# 按钮动作
|
135 |
+
generate_button.click(
|
136 |
+
generate_prompt,
|
137 |
+
inputs=[action_file, style_file, artist_file, character_file, api_key_input, selected_categories],
|
138 |
+
outputs=[tags_output, description_output],
|
139 |
+
)
|
140 |
+
|
141 |
+
return demo
|
142 |
+
|
143 |
+
# 启动 Gradio 应用
|
144 |
+
if __name__ == "__main__":
|
145 |
+
gradio_interface().launch()
|