Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import time
|
|
5 |
import random
|
6 |
import logging
|
7 |
import sys
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
@@ -41,6 +42,15 @@ SYSTEM_ASSISTANT = """作为 Stable Diffusion Prompt 提示词专家,您将从
|
|
41 |
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.
|
42 |
"""
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
def get_random_token(auth_header):
|
45 |
if not auth_header:
|
46 |
return None
|
@@ -81,6 +91,16 @@ def translate_and_enhance_prompt(prompt, auth_token):
|
|
81 |
logger.error(f"Error in translate_and_enhance_prompt: {str(e)}")
|
82 |
raise
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
@app.route('/')
|
85 |
def index():
|
86 |
return "text-to-image with siliconflow", 200
|
@@ -96,13 +116,14 @@ def handle_request():
|
|
96 |
return jsonify({"error": "Bad Request: Missing required fields"}), 400
|
97 |
|
98 |
prompt = messages[-1]['content']
|
|
|
99 |
|
100 |
random_token = get_random_token(request.headers.get('Authorization'))
|
101 |
if not random_token:
|
102 |
return jsonify({"error": "Unauthorized: Invalid or missing Authorization header"}), 401
|
103 |
|
104 |
try:
|
105 |
-
enhanced_prompt = translate_and_enhance_prompt(
|
106 |
except Exception as e:
|
107 |
logger.error(f"Error in translate_and_enhance_prompt: {str(e)}")
|
108 |
return jsonify({"error": "Failed to enhance prompt"}), 500
|
@@ -110,7 +131,7 @@ def handle_request():
|
|
110 |
new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
|
111 |
new_request_body = {
|
112 |
"prompt": enhanced_prompt,
|
113 |
-
"image_size":
|
114 |
"batch_size": 1,
|
115 |
"num_inference_steps": 4,
|
116 |
"guidance_scale": 1
|
@@ -154,9 +175,9 @@ def handle_request():
|
|
154 |
image_data = {'data': [{'url': image_url}]}
|
155 |
|
156 |
if stream:
|
157 |
-
return stream_response(unique_id, image_data,
|
158 |
else:
|
159 |
-
return non_stream_response(unique_id, image_data,
|
160 |
except Exception as e:
|
161 |
logger.error(f"Unexpected error in handle_request: {str(e)}")
|
162 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
|
|
5 |
import random
|
6 |
import logging
|
7 |
import sys
|
8 |
+
import re
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
|
|
42 |
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.
|
43 |
"""
|
44 |
|
45 |
+
RATIO_MAP = {
|
46 |
+
"1:1": "1024x1024",
|
47 |
+
"1:2": "1024x2048",
|
48 |
+
"3:2": "1536x1024",
|
49 |
+
"4:3": "1536x2048",
|
50 |
+
"16:9": "2048x1152",
|
51 |
+
"9:16": "1152x2048"
|
52 |
+
}
|
53 |
+
|
54 |
def get_random_token(auth_header):
|
55 |
if not auth_header:
|
56 |
return None
|
|
|
91 |
logger.error(f"Error in translate_and_enhance_prompt: {str(e)}")
|
92 |
raise
|
93 |
|
94 |
+
def extract_size_from_prompt(prompt):
|
95 |
+
size_match = re.search(r'-s\s+(\S+)', prompt)
|
96 |
+
if size_match:
|
97 |
+
size = size_match.group(1)
|
98 |
+
clean_prompt = re.sub(r'-s\s+\S+', '', prompt).strip()
|
99 |
+
else:
|
100 |
+
size = "16:9"
|
101 |
+
clean_prompt = prompt
|
102 |
+
return RATIO_MAP.get(size, RATIO_MAP["16:9"]), clean_prompt
|
103 |
+
|
104 |
@app.route('/')
|
105 |
def index():
|
106 |
return "text-to-image with siliconflow", 200
|
|
|
116 |
return jsonify({"error": "Bad Request: Missing required fields"}), 400
|
117 |
|
118 |
prompt = messages[-1]['content']
|
119 |
+
image_size, clean_prompt = extract_size_from_prompt(prompt)
|
120 |
|
121 |
random_token = get_random_token(request.headers.get('Authorization'))
|
122 |
if not random_token:
|
123 |
return jsonify({"error": "Unauthorized: Invalid or missing Authorization header"}), 401
|
124 |
|
125 |
try:
|
126 |
+
enhanced_prompt = translate_and_enhance_prompt(clean_prompt, random_token)
|
127 |
except Exception as e:
|
128 |
logger.error(f"Error in translate_and_enhance_prompt: {str(e)}")
|
129 |
return jsonify({"error": "Failed to enhance prompt"}), 500
|
|
|
131 |
new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
|
132 |
new_request_body = {
|
133 |
"prompt": enhanced_prompt,
|
134 |
+
"image_size": image_size,
|
135 |
"batch_size": 1,
|
136 |
"num_inference_steps": 4,
|
137 |
"guidance_scale": 1
|
|
|
175 |
image_data = {'data': [{'url': image_url}]}
|
176 |
|
177 |
if stream:
|
178 |
+
return stream_response(unique_id, image_data, clean_prompt, enhanced_prompt, image_size, current_timestamp, model, system_fingerprint)
|
179 |
else:
|
180 |
+
return non_stream_response(unique_id, image_data, clean_prompt, enhanced_prompt, image_size, current_timestamp, model, system_fingerprint)
|
181 |
except Exception as e:
|
182 |
logger.error(f"Unexpected error in handle_request: {str(e)}")
|
183 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|