Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,15 @@ import requests
|
|
3 |
import json
|
4 |
import time
|
5 |
import random
|
|
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
|
|
|
|
|
|
|
|
9 |
SYSTEM_ASSISTANT = """作为 Stable Diffusion Prompt 提示词专家,您将从关键词中创建提示,通常来自 Danbooru 等数据库。
|
10 |
提示通常描述图像,使用常见词汇,按重要性排列,并用逗号分隔。避免使用"-"或".",但可以接受空格和自然语言。避免词汇重复。
|
11 |
|
@@ -52,11 +58,14 @@ def translate_and_enhance_prompt(prompt, auth_token):
|
|
52 |
'Content-Type': 'application/json',
|
53 |
'Authorization': auth_token
|
54 |
}
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
60 |
|
61 |
@app.route('/')
|
62 |
def index():
|
@@ -74,13 +83,15 @@ def handle_request():
|
|
74 |
|
75 |
prompt = messages[-1]['content']
|
76 |
|
77 |
-
# 获取随机token
|
78 |
random_token = get_random_token(request.headers.get('Authorization'))
|
79 |
if not random_token:
|
80 |
return jsonify({"error": "Unauthorized: Invalid or missing Authorization header"}), 401
|
81 |
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
84 |
|
85 |
new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
|
86 |
new_request_body = {
|
@@ -97,9 +108,15 @@ def handle_request():
|
|
97 |
'Authorization': random_token
|
98 |
}
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
unique_id = int(time.time() * 1000)
|
104 |
current_timestamp = unique_id // 1000
|
105 |
|
@@ -147,6 +164,7 @@ def handle_request():
|
|
147 |
data_string = json.dumps(response_payload)
|
148 |
return Response(f"{data_string}\n\n", content_type='text/event-stream')
|
149 |
except Exception as e:
|
|
|
150 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
151 |
|
152 |
if __name__ == '__main__':
|
|
|
3 |
import json
|
4 |
import time
|
5 |
import random
|
6 |
+
import logging
|
7 |
+
from requests.exceptions import RequestException
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
+
# 配置日志
|
12 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
SYSTEM_ASSISTANT = """作为 Stable Diffusion Prompt 提示词专家,您将从关键词中创建提示,通常来自 Danbooru 等数据库。
|
16 |
提示通常描述图像,使用常见词汇,按重要性排列,并用逗号分隔。避免使用"-"或".",但可以接受空格和自然语言。避免词汇重复。
|
17 |
|
|
|
58 |
'Content-Type': 'application/json',
|
59 |
'Authorization': auth_token
|
60 |
}
|
61 |
+
try:
|
62 |
+
response = requests.post(translate_url, headers=headers, json=translate_body, timeout=30)
|
63 |
+
response.raise_for_status()
|
64 |
+
result = response.json()
|
65 |
+
return result['choices'][0]['message']['content']
|
66 |
+
except RequestException as e:
|
67 |
+
logger.error(f"Error in translate_and_enhance_prompt: {str(e)}")
|
68 |
+
raise
|
69 |
|
70 |
@app.route('/')
|
71 |
def index():
|
|
|
83 |
|
84 |
prompt = messages[-1]['content']
|
85 |
|
|
|
86 |
random_token = get_random_token(request.headers.get('Authorization'))
|
87 |
if not random_token:
|
88 |
return jsonify({"error": "Unauthorized: Invalid or missing Authorization header"}), 401
|
89 |
|
90 |
+
try:
|
91 |
+
enhanced_prompt = translate_and_enhance_prompt(prompt, random_token)
|
92 |
+
except Exception as e:
|
93 |
+
logger.error(f"Error in translate_and_enhance_prompt: {str(e)}")
|
94 |
+
return jsonify({"error": "Failed to enhance prompt"}), 500
|
95 |
|
96 |
new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
|
97 |
new_request_body = {
|
|
|
108 |
'Authorization': random_token
|
109 |
}
|
110 |
|
111 |
+
try:
|
112 |
+
response = requests.post(new_url, headers=headers, json=new_request_body, timeout=60)
|
113 |
+
response.raise_for_status()
|
114 |
+
response_body = response.json()
|
115 |
+
image_url = response_body['images'][0]['url']
|
116 |
+
except RequestException as e:
|
117 |
+
logger.error(f"Error in image generation request: {str(e)}")
|
118 |
+
return jsonify({"error": "Failed to generate image"}), 500
|
119 |
+
|
120 |
unique_id = int(time.time() * 1000)
|
121 |
current_timestamp = unique_id // 1000
|
122 |
|
|
|
164 |
data_string = json.dumps(response_payload)
|
165 |
return Response(f"{data_string}\n\n", content_type='text/event-stream')
|
166 |
except Exception as e:
|
167 |
+
logger.error(f"Unexpected error in handle_request: {str(e)}")
|
168 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
169 |
|
170 |
if __name__ == '__main__':
|