Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ import logging
|
|
7 |
import sys
|
8 |
import re
|
9 |
from logging.handlers import TimedRotatingFileHandler
|
10 |
-
from datetime import datetime
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
|
@@ -61,6 +60,38 @@ MODEL_MAPPING = {
|
|
61 |
}
|
62 |
}
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
# 模拟身份验证函数
|
65 |
def getAuthCookie(req):
|
66 |
auth_cookie = req.headers.get('Authorization')
|
@@ -180,8 +211,139 @@ def handle_request():
|
|
180 |
app.logger.error(f"Error: {str(e)}")
|
181 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
182 |
|
183 |
-
|
184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
if __name__ == '__main__':
|
187 |
app.run(host='0.0.0.0', port=8000)
|
|
|
7 |
import sys
|
8 |
import re
|
9 |
from logging.handlers import TimedRotatingFileHandler
|
|
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
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 |
# 模拟身份验证函数
|
96 |
def getAuthCookie(req):
|
97 |
auth_cookie = req.headers.get('Authorization')
|
|
|
211 |
app.logger.error(f"Error: {str(e)}")
|
212 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
213 |
|
214 |
+
def extract_params_from_prompt(prompt):
|
215 |
+
size_match = re.search(r'-s\s+(\S+)', prompt)
|
216 |
+
original_match = re.search(r'-o', prompt)
|
217 |
+
|
218 |
+
if size_match:
|
219 |
+
size = size_match.group(1)
|
220 |
+
clean_prompt = re.sub(r'-s\s+\S+', '', prompt).strip()
|
221 |
+
else:
|
222 |
+
size = "16:9"
|
223 |
+
clean_prompt = prompt
|
224 |
+
|
225 |
+
use_original = bool(original_match)
|
226 |
+
if use_original:
|
227 |
+
clean_prompt = re.sub(r'-o', '', clean_prompt).strip()
|
228 |
+
|
229 |
+
image_size = RATIO_MAP.get(size, RATIO_MAP["16:9"])
|
230 |
+
return image_size, clean_prompt, use_original, size
|
231 |
+
|
232 |
+
def get_random_token(auth_header):
|
233 |
+
if not auth_header:
|
234 |
+
return None
|
235 |
+
if auth_header.startswith('Bearer '):
|
236 |
+
auth_header = auth_header[7:]
|
237 |
+
tokens = [token.strip() for token in auth_header.split(',') if token.strip()]
|
238 |
+
if not tokens:
|
239 |
+
return None
|
240 |
+
return random.choice(tokens)
|
241 |
+
|
242 |
+
def translate_and_enhance_prompt(prompt, auth_token):
|
243 |
+
translate_url = 'https://api.siliconflow.cn/v1/chat/completions'
|
244 |
+
translate_body = {
|
245 |
+
'model': 'Qwen/Qwen2-72B-Instruct',
|
246 |
+
'messages': [
|
247 |
+
{'role': 'system', 'content': SYSTEM_ASSISTANT},
|
248 |
+
{'role': 'user', 'content': prompt}
|
249 |
+
]
|
250 |
+
}
|
251 |
+
headers = {
|
252 |
+
'Content-Type': 'application/json',
|
253 |
+
'Authorization': f'Bearer {auth_token}'
|
254 |
+
}
|
255 |
+
|
256 |
+
response = requests.post(translate_url, headers=headers, json=translate_body, timeout=30)
|
257 |
+
response.raise_for_status()
|
258 |
+
result = response.json()
|
259 |
+
return result['choices'][0]['message']['content']
|
260 |
+
|
261 |
+
def stream_response(unique_id, image_data, original_prompt, translated_prompt, size, created, model, system_fingerprint, use_original):
|
262 |
+
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')
|
263 |
+
|
264 |
+
def generate_stream(unique_id, image_data, original_prompt, translated_prompt, size, created, model, system_fingerprint, use_original):
|
265 |
+
chunks = [
|
266 |
+
f"原始提示词:\n{original_prompt}\n",
|
267 |
+
]
|
268 |
+
|
269 |
+
if not use_original:
|
270 |
+
chunks.append(f"翻译后的提示词:\n{translated_prompt}\n")
|
271 |
+
|
272 |
+
chunks.extend([
|
273 |
+
f"图像规格:{size}\n",
|
274 |
+
"正在根据提示词生成图像...\n",
|
275 |
+
"图像正在处理中...\n",
|
276 |
+
"即将完成...\n",
|
277 |
+
f"生成成功!\n图像生成完毕,以下是结果:\n\n"
|
278 |
+
])
|
279 |
+
|
280 |
+
for i, chunk in enumerate(chunks):
|
281 |
+
json_chunk = json.dumps({
|
282 |
+
"id": unique_id,
|
283 |
+
"object": "chat.completion.chunk",
|
284 |
+
"created": created,
|
285 |
+
"model": model,
|
286 |
+
"system_fingerprint": system_fingerprint,
|
287 |
+
"choices": [{
|
288 |
+
"index": 0,
|
289 |
+
"delta": {"content": chunk},
|
290 |
+
"logprobs": None,
|
291 |
+
"finish_reason": None
|
292 |
+
}]
|
293 |
+
})
|
294 |
+
yield f"data: {json_chunk}\n\n"
|
295 |
+
time.sleep(0.5) # 模拟生成时间
|
296 |
+
|
297 |
+
final_chunk = json.dumps({
|
298 |
+
"id": unique_id,
|
299 |
+
"object": "chat.completion.chunk",
|
300 |
+
"created": created,
|
301 |
+
"model": model,
|
302 |
+
"system_fingerprint": system_fingerprint,
|
303 |
+
"choices": [{
|
304 |
+
"index": 0,
|
305 |
+
"delta": {},
|
306 |
+
"logprobs": None,
|
307 |
+
"finish_reason": "stop"
|
308 |
+
}]
|
309 |
+
})
|
310 |
+
yield f"data: {final_chunk}\n\n"
|
311 |
+
|
312 |
+
def non_stream_response(unique_id, image_data, original_prompt, translated_prompt, size, created, model, system_fingerprint, use_original):
|
313 |
+
content = f"原始提示词:{original_prompt}\n"
|
314 |
+
|
315 |
+
if not use_original:
|
316 |
+
content += f"翻译后的提示词:{translated_prompt}\n"
|
317 |
+
|
318 |
+
content += (
|
319 |
+
f"图像规格:{size}\n"
|
320 |
+
f"图像生成成功!\n"
|
321 |
+
f"以下是结果:\n\n"
|
322 |
+
f""
|
323 |
+
)
|
324 |
+
|
325 |
+
response = {
|
326 |
+
'id': unique_id,
|
327 |
+
'object': "chat.completion",
|
328 |
+
'created': created,
|
329 |
+
'model': model,
|
330 |
+
'system_fingerprint': system_fingerprint,
|
331 |
+
'choices': [{
|
332 |
+
'index': 0,
|
333 |
+
'message': {
|
334 |
+
'role': "assistant",
|
335 |
+
'content': content
|
336 |
+
},
|
337 |
+
'finish_reason': "stop"
|
338 |
+
}],
|
339 |
+
'usage': {
|
340 |
+
'prompt_tokens': len(original_prompt),
|
341 |
+
'completion_tokens': len(content),
|
342 |
+
'total_tokens': len(original_prompt) + len(content)
|
343 |
+
}
|
344 |
+
}
|
345 |
+
|
346 |
+
return jsonify(response)
|
347 |
|
348 |
if __name__ == '__main__':
|
349 |
app.run(host='0.0.0.0', port=8000)
|