Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,13 @@ from flask import Flask, request, jsonify, Response
|
|
2 |
import requests
|
3 |
import json
|
4 |
import time
|
5 |
-
import random
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
@app.route('/')
|
10 |
def index():
|
11 |
return "text-to-image with siliconflow", 200
|
12 |
-
|
13 |
@app.route('/ai/v1/chat/completions', methods=['POST'])
|
14 |
def handle_request():
|
15 |
try:
|
@@ -23,6 +22,7 @@ def handle_request():
|
|
23 |
|
24 |
prompt = messages[-1]['content']
|
25 |
new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
|
|
|
26 |
new_request_body = {
|
27 |
"prompt": prompt,
|
28 |
"image_size": "1024x1024",
|
@@ -31,29 +31,16 @@ def handle_request():
|
|
31 |
"guidance_scale": 1
|
32 |
}
|
33 |
|
34 |
-
# Get the Authorization header
|
35 |
-
auth_header = request.headers.get('Authorization', '')
|
36 |
-
|
37 |
-
# Split the tokens and randomly select one
|
38 |
-
tokens = auth_header.split(',')
|
39 |
-
if tokens:
|
40 |
-
# Remove 'Bearer ' prefix if present
|
41 |
-
tokens = [token.strip().replace('Bearer ', '') for token in tokens]
|
42 |
-
selected_token = random.choice(tokens)
|
43 |
-
auth_header = f'Bearer {selected_token}'
|
44 |
-
else:
|
45 |
-
auth_header = ''
|
46 |
-
|
47 |
headers = {
|
48 |
'accept': 'application/json',
|
49 |
'content-type': 'application/json',
|
50 |
-
'Authorization':
|
51 |
}
|
52 |
|
53 |
response = requests.post(new_url, headers=headers, json=new_request_body)
|
54 |
response_body = response.json()
|
55 |
-
image_url = response_body['images'][0]['url']
|
56 |
|
|
|
57 |
unique_id = int(time.time() * 1000)
|
58 |
current_timestamp = unique_id // 1000
|
59 |
|
@@ -100,6 +87,7 @@ def handle_request():
|
|
100 |
}
|
101 |
data_string = json.dumps(response_payload)
|
102 |
return Response(f"{data_string}\n\n", content_type='text/event-stream')
|
|
|
103 |
except Exception as e:
|
104 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
105 |
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import time
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
@app.route('/')
|
9 |
def index():
|
10 |
return "text-to-image with siliconflow", 200
|
11 |
+
|
12 |
@app.route('/ai/v1/chat/completions', methods=['POST'])
|
13 |
def handle_request():
|
14 |
try:
|
|
|
22 |
|
23 |
prompt = messages[-1]['content']
|
24 |
new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
|
25 |
+
|
26 |
new_request_body = {
|
27 |
"prompt": prompt,
|
28 |
"image_size": "1024x1024",
|
|
|
31 |
"guidance_scale": 1
|
32 |
}
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
headers = {
|
35 |
'accept': 'application/json',
|
36 |
'content-type': 'application/json',
|
37 |
+
'Authorization': request.headers.get('Authorization')
|
38 |
}
|
39 |
|
40 |
response = requests.post(new_url, headers=headers, json=new_request_body)
|
41 |
response_body = response.json()
|
|
|
42 |
|
43 |
+
image_url = response_body['images'][0]['url']
|
44 |
unique_id = int(time.time() * 1000)
|
45 |
current_timestamp = unique_id // 1000
|
46 |
|
|
|
87 |
}
|
88 |
data_string = json.dumps(response_payload)
|
89 |
return Response(f"{data_string}\n\n", content_type='text/event-stream')
|
90 |
+
|
91 |
except Exception as e:
|
92 |
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
93 |
|