Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import os
|
|
| 2 |
import time
|
| 3 |
import logging
|
| 4 |
import requests
|
|
|
|
|
|
|
| 5 |
import json
|
| 6 |
import uuid
|
| 7 |
import concurrent.futures
|
|
@@ -28,6 +30,24 @@ MODELS_ENDPOINT = "https://api-st.siliconflow.cn/v1/models"
|
|
| 28 |
EMBEDDINGS_ENDPOINT = "https://api-st.siliconflow.cn/v1/embeddings"
|
| 29 |
IMAGE_ENDPOINT = "https://api-st.siliconflow.cn/v1/images/generations"
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
app = Flask(__name__)
|
| 32 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1)
|
| 33 |
|
|
@@ -61,7 +81,7 @@ def get_credit_summary(api_key):
|
|
| 61 |
|
| 62 |
for attempt in range(max_retries):
|
| 63 |
try:
|
| 64 |
-
response =
|
| 65 |
response.raise_for_status()
|
| 66 |
data = response.json().get("data", {})
|
| 67 |
total_balance = data.get("totalBalance", 0)
|
|
@@ -93,7 +113,7 @@ def test_model_availability(api_key, model_name):
|
|
| 93 |
"Content-Type": "application/json"
|
| 94 |
}
|
| 95 |
try:
|
| 96 |
-
response =
|
| 97 |
TEST_MODEL_ENDPOINT,
|
| 98 |
headers=headers,
|
| 99 |
json={
|
|
@@ -117,7 +137,7 @@ def test_model_availability(api_key, model_name):
|
|
| 117 |
|
| 118 |
def create_base64_markdown_image(image_url):
|
| 119 |
try:
|
| 120 |
-
response =
|
| 121 |
response.raise_for_status()
|
| 122 |
|
| 123 |
image_data = response.content
|
|
@@ -250,7 +270,7 @@ def test_embedding_model_availability(api_key, model_name):
|
|
| 250 |
"Content-Type": "application/json"
|
| 251 |
}
|
| 252 |
try:
|
| 253 |
-
response =
|
| 254 |
EMBEDDINGS_ENDPOINT,
|
| 255 |
headers=headers,
|
| 256 |
json={
|
|
@@ -354,7 +374,7 @@ def get_all_models(api_key, sub_type):
|
|
| 354 |
"Content-Type": "application/json"
|
| 355 |
}
|
| 356 |
try:
|
| 357 |
-
response =
|
| 358 |
MODELS_ENDPOINT,
|
| 359 |
headers=headers,
|
| 360 |
params={"sub_type": sub_type}
|
|
@@ -765,7 +785,7 @@ def handsome_images_generations():
|
|
| 765 |
print(f"image_url: {image_url}")
|
| 766 |
if data.get("response_format") == "b64_json":
|
| 767 |
try:
|
| 768 |
-
image_data =
|
| 769 |
image = Image.open(image_data)
|
| 770 |
buffered = io.BytesIO()
|
| 771 |
image.save(buffered, format="PNG")
|
|
@@ -1363,4 +1383,4 @@ if __name__ == '__main__':
|
|
| 1363 |
debug=False,
|
| 1364 |
host='0.0.0.0',
|
| 1365 |
port=int(os.environ.get('PORT', 7860))
|
| 1366 |
-
)
|
|
|
|
| 2 |
import time
|
| 3 |
import logging
|
| 4 |
import requests
|
| 5 |
+
from requests.adapters import HTTPAdapter
|
| 6 |
+
from requests.packages.urllib3.util.retry import Retry
|
| 7 |
import json
|
| 8 |
import uuid
|
| 9 |
import concurrent.futures
|
|
|
|
| 30 |
EMBEDDINGS_ENDPOINT = "https://api-st.siliconflow.cn/v1/embeddings"
|
| 31 |
IMAGE_ENDPOINT = "https://api-st.siliconflow.cn/v1/images/generations"
|
| 32 |
|
| 33 |
+
def requests_session_with_retries(
|
| 34 |
+
retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504)
|
| 35 |
+
):
|
| 36 |
+
session = requests.Session()
|
| 37 |
+
retry = Retry(
|
| 38 |
+
total=retries,
|
| 39 |
+
read=retries,
|
| 40 |
+
connect=retries,
|
| 41 |
+
backoff_factor=backoff_factor,
|
| 42 |
+
status_forcelist=status_forcelist,
|
| 43 |
+
)
|
| 44 |
+
adapter = HTTPAdapter(max_retries=retry)
|
| 45 |
+
session.mount("http://", adapter)
|
| 46 |
+
session.mount("https://", adapter)
|
| 47 |
+
return session
|
| 48 |
+
|
| 49 |
+
session = requests_session_with_retries()
|
| 50 |
+
|
| 51 |
app = Flask(__name__)
|
| 52 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1)
|
| 53 |
|
|
|
|
| 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)
|
|
|
|
| 113 |
"Content-Type": "application/json"
|
| 114 |
}
|
| 115 |
try:
|
| 116 |
+
response = session.post(
|
| 117 |
TEST_MODEL_ENDPOINT,
|
| 118 |
headers=headers,
|
| 119 |
json={
|
|
|
|
| 137 |
|
| 138 |
def create_base64_markdown_image(image_url):
|
| 139 |
try:
|
| 140 |
+
response = session.get(image_url, stream=True)
|
| 141 |
response.raise_for_status()
|
| 142 |
|
| 143 |
image_data = response.content
|
|
|
|
| 270 |
"Content-Type": "application/json"
|
| 271 |
}
|
| 272 |
try:
|
| 273 |
+
response = session.post(
|
| 274 |
EMBEDDINGS_ENDPOINT,
|
| 275 |
headers=headers,
|
| 276 |
json={
|
|
|
|
| 374 |
"Content-Type": "application/json"
|
| 375 |
}
|
| 376 |
try:
|
| 377 |
+
response = session.get(
|
| 378 |
MODELS_ENDPOINT,
|
| 379 |
headers=headers,
|
| 380 |
params={"sub_type": sub_type}
|
|
|
|
| 785 |
print(f"image_url: {image_url}")
|
| 786 |
if data.get("response_format") == "b64_json":
|
| 787 |
try:
|
| 788 |
+
image_data = session.get(image_url, stream=True).raw
|
| 789 |
image = Image.open(image_data)
|
| 790 |
buffered = io.BytesIO()
|
| 791 |
image.save(buffered, format="PNG")
|
|
|
|
| 1383 |
debug=False,
|
| 1384 |
host='0.0.0.0',
|
| 1385 |
port=int(os.environ.get('PORT', 7860))
|
| 1386 |
+
)
|