Update app.py
Browse files
app.py
CHANGED
@@ -32,30 +32,6 @@ handler.setLevel(logging.INFO)
|
|
32 |
app.logger.addHandler(handler)
|
33 |
app.logger.setLevel(logging.INFO)
|
34 |
|
35 |
-
SYSTEM_ASSISTANT = """作为 Stable Diffusion Prompt 提示词专家,您将从关键词中创建提示,通常来自 Danbooru 等数据库。
|
36 |
-
提示通常描述图像,使用常见词汇,按重要性排列,并用逗号分隔。避免使用"-"或".",但可以接受空格和自然语言。避免词汇重复。
|
37 |
-
为了强调关键词,请将其放在括号中以增加其权重。例如,"(flowers)"将'flowers'的权重增加1.1倍,而"(((flowers)))"将其增加1.331倍。使用"(flowers:1.5)"将'flowers'的权重增加1.5倍。只为重要的标签增加权重。
|
38 |
-
提示包括三个部分:**前缀** (质量标签+风格词+效果器)+ **主题** (图像的主要焦点)+ **场景** (背景、环境)。
|
39 |
-
* 前缀影响图像质量。像"masterpiece"、"best quality"、"4k"这样的标签可以提高图像的细节。像"illustration"、"lensflare"这样的风格词定义图像的风格。像"bestlighting"、"lensflare"、"depthoffield"这样的效果器会影响光照和深度。
|
40 |
-
* 主题是图像的主要焦点,如角色或场景。对主题进行详细描述可以确保图像丰富而详细。增加主题的权重以增强其清晰度。对于角色,描述面部、头发、身体、服装、姿势等特征。
|
41 |
-
* 场景描述环境。没有场景,图像的背景是平淡的,主题显得过大。某些主题本身包含场景(例如建筑物、风景)。像"花草草地"、"阳光"、"河流"这样的环境词可以丰富场景。你的任务是设计图像生成的提示。请按照以下步骤进行操作:
|
42 |
-
1. 我会发送给您一个图像场景。需要你生成详细的图像描述
|
43 |
-
2. 图像描述必须是英文,输出为Positive Prompt。
|
44 |
-
示例:
|
45 |
-
我发送:二战时期的护士。
|
46 |
-
您回复只回复:
|
47 |
-
A WWII-era nurse in a German uniform, holding a wine bottle and stethoscope, sitting at a table in white attire, with a table in the background, masterpiece, best quality, 4k, illustration style, best lighting, depth of field, detailed character, detailed environment.
|
48 |
-
"""
|
49 |
-
|
50 |
-
RATIO_MAP = {
|
51 |
-
"1:1": "1024x1024",
|
52 |
-
"1:2": "1024x2048",
|
53 |
-
"3:2": "1536x1024",
|
54 |
-
"4:3": "1536x2048",
|
55 |
-
"16:9": "2048x1152",
|
56 |
-
"9:16": "1152x2048"
|
57 |
-
}
|
58 |
-
|
59 |
# 模型映射
|
60 |
MODEL_MAPPING = {
|
61 |
"flux.1-schnell": {
|
@@ -84,12 +60,37 @@ MODEL_MAPPING = {
|
|
84 |
}
|
85 |
}
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
def get_random_token(auth_header):
|
95 |
if not auth_header:
|
@@ -153,7 +154,7 @@ def index():
|
|
153 |
</style>
|
154 |
</head>
|
155 |
<body>
|
156 |
-
<h1>Text-to-Image API with SiliconFlow!</h1>
|
157 |
|
158 |
<h2>Usage:</h2>
|
159 |
<ol>
|
@@ -166,6 +167,7 @@ def index():
|
|
166 |
</ul>
|
167 |
</li>
|
168 |
</ol>
|
|
|
169 |
<h2>Example Request:</h2>
|
170 |
<pre><code>
|
171 |
{
|
@@ -178,46 +180,13 @@ def index():
|
|
178 |
]
|
179 |
}
|
180 |
</code></pre>
|
|
|
181 |
<p>For more details, please refer to the API documentation.</p>
|
182 |
</body>
|
183 |
</html>
|
184 |
"""
|
185 |
return usage, 200
|
186 |
|
187 |
-
@app.route('/ai/v1/models', methods=['GET'])
|
188 |
-
def get_models():
|
189 |
-
try:
|
190 |
-
# 验证身份
|
191 |
-
auth_cookie = getAuthCookie(request)
|
192 |
-
if not auth_cookie:
|
193 |
-
return jsonify({"error": "Unauthorized"}), 401
|
194 |
-
|
195 |
-
# 返回模型列表
|
196 |
-
models_list = [
|
197 |
-
{
|
198 |
-
"id": model_id,
|
199 |
-
"object": "model",
|
200 |
-
"created": int(time.time()),
|
201 |
-
"owned_by": info["provider"],
|
202 |
-
"permission": [],
|
203 |
-
"root": model_id,
|
204 |
-
"parent": None
|
205 |
-
}
|
206 |
-
for model_id, info in MODEL_MAPPING.items()
|
207 |
-
]
|
208 |
-
|
209 |
-
# 记录日志
|
210 |
-
app.logger.info(f'{request.remote_addr} - GET /ai/v1/models - 200')
|
211 |
-
|
212 |
-
return jsonify({
|
213 |
-
"object": "list",
|
214 |
-
"data": models_list
|
215 |
-
})
|
216 |
-
|
217 |
-
except Exception as error:
|
218 |
-
app.logger.error(f"Error: {str(error)}")
|
219 |
-
return jsonify({"error": "Authentication failed", "details": str(error)}), 401
|
220 |
-
|
221 |
@app.route('/ai/v1/chat/completions', methods=['POST'])
|
222 |
def handle_request():
|
223 |
try:
|
@@ -295,6 +264,25 @@ def handle_request():
|
|
295 |
app.logger.error(f"Error: {str(e)}")
|
296 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
def stream_response(unique_id, image_data, original_prompt, translated_prompt, size, created, model, system_fingerprint, use_original):
|
299 |
return Response(stream_with_context(generate_stream(unique_id, image_data, original_prompt, translated_prompt, size, created, model, system_fingerprint, use_original)), content_type='text/event-stream')
|
300 |
|
|
|
32 |
app.logger.addHandler(handler)
|
33 |
app.logger.setLevel(logging.INFO)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
# 模型映射
|
36 |
MODEL_MAPPING = {
|
37 |
"flux.1-schnell": {
|
|
|
60 |
}
|
61 |
}
|
62 |
|
63 |
+
SYSTEM_ASSISTANT = """作为 Stable Diffusion Prompt 提示词专家,您将从关键词中创建提示,通常来自 Danbooru 等数据库。
|
64 |
+
提示通常描述图像,使用常见词汇,按重要性排列,并用逗号分隔。避免使用"-"或".",但可以接受空格和自然语言。避免词汇重复。
|
65 |
+
|
66 |
+
为了强调关键词,请将其放在括号中以增加其权重。例如,"(flowers)"将'flowers'的权重增加1.1倍,而"(((flowers)))"将其增加1.331倍。使用"(flowers:1.5)"将'flowers'的权重增加1.5倍。只为重要的标签增加权重。
|
67 |
+
|
68 |
+
提示包括三个部分:**前缀**(质量标签+风格词+效果器)+ **主题**(图像的主要焦点)+ **场景**(背景、环境)。
|
69 |
+
|
70 |
+
* 前缀影响图像质量。像"masterpiece"、"best quality"、"4k"这样的标签可以提高图像的细节。像"illustration"、"lensflare"这样的风格词定义图像的风格。像"bestlighting"、"lensflare"、"depthoffield"这样的效果器会影响光照和深度。
|
71 |
+
|
72 |
+
* 主题是图像的主要焦点,如角色或场景。对主题进行详细描述可以确保图像丰富而详细。增加主题的权重以增强其清晰度。对于角色,描述面部、头发、身体、服装、姿势等特征。
|
73 |
+
|
74 |
+
* 场景描述环境。没有场景,图像的背景是平淡的,主题显得过大。某些主题本身包含场景(例如建筑物、风景)。像"花草草地"、"阳光"、"河流"这样的环境词可以丰富场景。你的任务是设计图像生成的提示。请按照以下步骤进行操作:
|
75 |
+
|
76 |
+
1. 我会发送给您一个图像场景。需要你生成详细的图像描述
|
77 |
+
2. 图像描述必须是英文,输出为Positive Prompt。
|
78 |
+
|
79 |
+
示例:
|
80 |
+
|
81 |
+
我发送:二战时期的护士。
|
82 |
+
您回复只回复:
|
83 |
+
A WWII-era nurse in a German uniform, holding a wine bottle and stethoscope, sitting at a table in white attire, with a table in the background, masterpiece, best quality, 4k, illustration style, best lighting, depth of field, detailed character, detailed environment.
|
84 |
+
"""
|
85 |
+
|
86 |
+
RATIO_MAP = {
|
87 |
+
"1:1": "1024x1024",
|
88 |
+
"1:2": "1024x2048",
|
89 |
+
"3:2": "1536x1024",
|
90 |
+
"4:3": "1536x2048",
|
91 |
+
"16:9": "2048x1152",
|
92 |
+
"9:16": "1152x2048"
|
93 |
+
}
|
94 |
|
95 |
def get_random_token(auth_header):
|
96 |
if not auth_header:
|
|
|
154 |
</style>
|
155 |
</head>
|
156 |
<body>
|
157 |
+
<h1>Welcome to the Text-to-Image API with SiliconFlow!</h1>
|
158 |
|
159 |
<h2>Usage:</h2>
|
160 |
<ol>
|
|
|
167 |
</ul>
|
168 |
</li>
|
169 |
</ol>
|
170 |
+
|
171 |
<h2>Example Request:</h2>
|
172 |
<pre><code>
|
173 |
{
|
|
|
180 |
]
|
181 |
}
|
182 |
</code></pre>
|
183 |
+
|
184 |
<p>For more details, please refer to the API documentation.</p>
|
185 |
</body>
|
186 |
</html>
|
187 |
"""
|
188 |
return usage, 200
|
189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
@app.route('/ai/v1/chat/completions', methods=['POST'])
|
191 |
def handle_request():
|
192 |
try:
|
|
|
264 |
app.logger.error(f"Error: {str(e)}")
|
265 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
266 |
|
267 |
+
@app.route('/ai/v1/models', methods=['GET'])
|
268 |
+
def get_models():
|
269 |
+
models_list = [
|
270 |
+
{
|
271 |
+
"id": key,
|
272 |
+
"object": "model",
|
273 |
+
"owned_by": value["provider"],
|
274 |
+
"mapping": value["mapping"]
|
275 |
+
}
|
276 |
+
for key, value in MODEL_MAPPING.items()
|
277 |
+
]
|
278 |
+
|
279 |
+
response = {
|
280 |
+
"object": "list",
|
281 |
+
"data": models_list
|
282 |
+
}
|
283 |
+
|
284 |
+
return jsonify(response)
|
285 |
+
|
286 |
def stream_response(unique_id, image_data, original_prompt, translated_prompt, size, created, model, system_fingerprint, use_original):
|
287 |
return Response(stream_with_context(generate_stream(unique_id, image_data, original_prompt, translated_prompt, size, created, model, system_fingerprint, use_original)), content_type='text/event-stream')
|
288 |
|