Update app.py
Browse files
app.py
CHANGED
@@ -72,29 +72,6 @@ request_timestamps_day = []
|
|
72 |
token_counts_day = []
|
73 |
data_lock = threading.Lock()
|
74 |
|
75 |
-
def get_credit_summary(api_key):
|
76 |
-
headers = {
|
77 |
-
"Authorization": f"Bearer {api_key}",
|
78 |
-
"Content-Type": "application/json"
|
79 |
-
}
|
80 |
-
max_retries = 3
|
81 |
-
|
82 |
-
for attempt in range(max_retries):
|
83 |
-
try:
|
84 |
-
response = session.get(API_ENDPOINT, headers=headers, timeout=2)
|
85 |
-
response.raise_for_status()
|
86 |
-
data = response.json().get("data", {})
|
87 |
-
total_balance = data.get("totalBalance", 0)
|
88 |
-
logging.info(f"获取额度,API Key:{api_key},当前额度: {total_balance}")
|
89 |
-
return {"total_balance": float(total_balance)}
|
90 |
-
except requests.exceptions.Timeout as e:
|
91 |
-
logging.error(f"获取额度信息失败,API Key:{api_key},尝试次数:{attempt+1}/{max_retries},错误信息:{e} (Timeout)")
|
92 |
-
if attempt >= max_retries - 1:
|
93 |
-
logging.error(f"获取额度信息失败,API Key:{api_key},所有重试次数均已失败 (Timeout)")
|
94 |
-
except requests.exceptions.RequestException as e:
|
95 |
-
logging.error(f"获取额度信息失败,API Key:{api_key},错误信息:{e}")
|
96 |
-
return None
|
97 |
-
|
98 |
def extract_user_content(messages):
|
99 |
user_content = ""
|
100 |
for message in messages:
|
@@ -107,57 +84,10 @@ def extract_user_content(messages):
|
|
107 |
user_content += item.get("text", "") + " "
|
108 |
return user_content.strip()
|
109 |
|
110 |
-
def get_siliconflow_data(model_name, data):
|
111 |
-
siliconflow_data = {
|
112 |
-
"model": model_name,
|
113 |
-
"prompt": data.get("prompt") or "",
|
114 |
-
}
|
115 |
-
|
116 |
-
if model_name == "black-forest-labs/FLUX.1-pro":
|
117 |
-
siliconflow_data.update({
|
118 |
-
"width": max(256, min(1440, (data.get("width", 1024) // 32) * 32)),
|
119 |
-
"height": max(256, min(1440, (data.get("height", 768) // 32) * 32)),
|
120 |
-
"prompt_upsampling": data.get("prompt_upsampling", False),
|
121 |
-
"image_prompt": data.get("image_prompt"),
|
122 |
-
"steps": max(1, min(50, data.get("steps", 20))),
|
123 |
-
"guidance": max(1.5, min(5, data.get("guidance", 3))),
|
124 |
-
"safety_tolerance": max(0, min(6, data.get("safety_tolerance", 2))),
|
125 |
-
"interval": max(1, min(4, data.get("interval", 2))),
|
126 |
-
"output_format": data.get("output_format", "png")
|
127 |
-
})
|
128 |
-
|
129 |
-
seed = data.get("seed")
|
130 |
-
if isinstance(seed, int) and 0 < seed < 9999999999:
|
131 |
-
siliconflow_data["seed"] = seed
|
132 |
-
|
133 |
-
else:
|
134 |
-
siliconflow_data.update({
|
135 |
-
"image_size": data.get("image_size", "1024x1024"),
|
136 |
-
"prompt_enhancement": data.get("prompt_enhancement", False)
|
137 |
-
})
|
138 |
-
|
139 |
-
seed = data.get("seed")
|
140 |
-
if isinstance(seed, int) and 0 < seed < 9999999999:
|
141 |
-
siliconflow_data["seed"] = seed
|
142 |
-
|
143 |
-
if model_name not in ["black-forest-labs/FLUX.1-schnell", "Pro/black-forest-labs/FLUX.1-schnell"]:
|
144 |
-
siliconflow_data.update({
|
145 |
-
"batch_size": max(1, min(4, data.get("n", 1))),
|
146 |
-
"num_inference_steps": max(1, min(50, data.get("steps", 20))),
|
147 |
-
"guidance_scale": max(0, min(100, data.get("guidance_scale", 7.5))),
|
148 |
-
"negative_prompt": data.get("negative_prompt")
|
149 |
-
})
|
150 |
-
|
151 |
-
valid_sizes = ["1024x1024", "512x1024", "768x512", "768x1024", "1024x576", "576x1024", "960x1280", "720x1440", "720x1280"]
|
152 |
-
if "image_size" in siliconflow_data and siliconflow_data["image_size"] not in valid_sizes:
|
153 |
-
siliconflow_data["image_size"] = "1024x1024"
|
154 |
-
|
155 |
-
return siliconflow_data
|
156 |
-
|
157 |
def refresh_models():
|
158 |
global models
|
159 |
|
160 |
-
models["text"] = get_all_models(
|
161 |
|
162 |
for model_type in ["text"]:
|
163 |
logging.info(f"所有{model_type}模型列表:{models[model_type]}")
|
|
|
72 |
token_counts_day = []
|
73 |
data_lock = threading.Lock()
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
def extract_user_content(messages):
|
76 |
user_content = ""
|
77 |
for message in messages:
|
|
|
84 |
user_content += item.get("text", "") + " "
|
85 |
return user_content.strip()
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
def refresh_models():
|
88 |
global models
|
89 |
|
90 |
+
models["text"] = get_all_models(keys_str[0])
|
91 |
|
92 |
for model_type in ["text"]:
|
93 |
logging.info(f"所有{model_type}模型列表:{models[model_type]}")
|