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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import io
|
| 4 |
+
import subprocess
|
| 5 |
+
import requests
|
| 6 |
+
import pdb
|
| 7 |
+
import time
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
def read_image_base64(file_path):
|
| 11 |
+
with open(file_path, "rb") as f:
|
| 12 |
+
image_data = f.read()
|
| 13 |
+
encoded_string = base64.b64encode(image_data).decode('utf-8')
|
| 14 |
+
return encoded_string
|
| 15 |
+
|
| 16 |
+
image_paths = [
|
| 17 |
+
"extra_data/ImageNet21k_Gen/FLUX/render_output/baby/7/random_white5.jpg",
|
| 18 |
+
"extra_data/ImageNet21k_Gen/FLUX/render_output/pace_car/6/random_white41.jpg",
|
| 19 |
+
"extra_data/ImageNet21k_Gen/FLUX/render_output/walker/2/random_white10.jpg"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
content = [{
|
| 23 |
+
"type": "text",
|
| 24 |
+
"text": "生成一张正方形图像,在合适的背景下融合这些图片中的物体,物体间保持着合理的尺寸大小关系,并存在形成合理的空间遮挡关系。"
|
| 25 |
+
}]
|
| 26 |
+
|
| 27 |
+
for path in image_paths:
|
| 28 |
+
content.append({
|
| 29 |
+
"type": "image_url",
|
| 30 |
+
"image_url": {
|
| 31 |
+
"url": f"data:image/jpg;base64,{read_image_base64(path)}"},
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
headers = {
|
| 35 |
+
"Content-Type": "application/json",
|
| 36 |
+
"Authorization": "Bearer sk-vp48fZYFsrrZGKTBySDquUO7ntYQFHLt7hUhTf1xNhJT2GDJ"
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
payload = {
|
| 40 |
+
"model": "gpt-4o-image",
|
| 41 |
+
"messages": [
|
| 42 |
+
{
|
| 43 |
+
"role": "user",
|
| 44 |
+
"content": content
|
| 45 |
+
}
|
| 46 |
+
],
|
| 47 |
+
"max_tokens": 300
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
url = "https://chatapi.onechats.top/v1/chat/completions"
|
| 51 |
+
max_retries = 10
|
| 52 |
+
|
| 53 |
+
for attempt in range(max_retries):
|
| 54 |
+
start_time = time.time()
|
| 55 |
+
try:
|
| 56 |
+
response = requests.post(url, headers=headers, json=payload, timeout=900)
|
| 57 |
+
end_time = time.time()
|
| 58 |
+
execution_time = end_time - start_time
|
| 59 |
+
if response.status_code != 200:
|
| 60 |
+
print(f"第 {attempt + 1} 次请求失败,耗时: {execution_time} 秒,正在重试...")
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
print(f"第 {attempt + 1} 次请求成功,耗时: {execution_time} 秒")
|
| 64 |
+
break # 请求成功则跳出循环
|
| 65 |
+
except requests.exceptions.Timeout:
|
| 66 |
+
end_time = time.time()
|
| 67 |
+
execution_time = end_time - start_time
|
| 68 |
+
print(f"第 {attempt + 1} 次请求超时,耗时: {execution_time} 秒,正在重试...")
|
| 69 |
+
if attempt == max_retries - 1:
|
| 70 |
+
print("请求失败,已达到最大重试次数")
|
| 71 |
+
response = None
|
| 72 |
+
|
| 73 |
+
pdb.set_trace()
|
| 74 |
+
|
| 75 |
+
if response.status_code == 200:
|
| 76 |
+
content = response.content.decode('utf-8')
|
| 77 |
+
json_part1, json_part2 = content[:content.rfind('}')+1], content[content.rfind('}')+1:]
|
| 78 |
+
json_part1 = json_part1.replace('\\u0026', '&') + + '}'
|
| 79 |
+
data = json.loads(json_part1)
|
| 80 |
+
image_url = data['choices'][0]['message']['content']
|
| 81 |
+
|
| 82 |
+
response_image = requests.get(image_url)
|
| 83 |
+
if response_image.status_code == 200:
|
| 84 |
+
with open("generated_image.jpg", "wb") as f:
|
| 85 |
+
f.write(response_image.content)
|
| 86 |
+
print("图片下载成功")
|
| 87 |
+
else:
|
| 88 |
+
print(f"图片下载失败,状态码:{response_image.status_code}")
|
| 89 |
+
|
| 90 |
+
pdb.set_trace()
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# curl https://api.onechats.ai/v1/chat/completions \
|
| 94 |
+
# -H "Content-Type: application/json" \
|
| 95 |
+
# -H "Authorization: Bearer sk-vp48fZYFsrrZGKTBySDquUO7ntYQFHLt7hUhTf1xNhJT2GDJ" \
|
| 96 |
+
# -d '{
|
| 97 |
+
# "model": "gpt-4o-image",
|
| 98 |
+
# "messages": [
|
| 99 |
+
# {
|
| 100 |
+
# "role": "user",
|
| 101 |
+
# "content": [
|
| 102 |
+
# {
|
| 103 |
+
# "type": "text",
|
| 104 |
+
# "text": "帮我把这张图片中的草地换成沼泽"
|
| 105 |
+
# },
|
| 106 |
+
# {
|
| 107 |
+
# "type": "image_url",
|
| 108 |
+
# "image_url": {
|
| 109 |
+
# "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
|
| 110 |
+
# }
|
| 111 |
+
# }
|
| 112 |
+
# ]
|
| 113 |
+
# }
|
| 114 |
+
# ],
|
| 115 |
+
# "max_tokens": 300
|
| 116 |
+
# }'
|
| 117 |
+
|
| 118 |
+
# {
|
| 119 |
+
# "models": [
|
| 120 |
+
# {
|
| 121 |
+
# "title": "DeepSeek Coder",
|
| 122 |
+
# "model": "deepseek-coder",
|
| 123 |
+
# "contextLength": 128000,
|
| 124 |
+
# "apiKey": "sk-3e2fc969465642b8887f04afa0dd89fe",
|
| 125 |
+
# "provider": "deepseek"
|
| 126 |
+
# },
|
| 127 |
+
# {
|
| 128 |
+
# "title": "DeepSeek-v3",
|
| 129 |
+
# "model": "deepseek-ai/DeepSeek-V3",
|
| 130 |
+
# "contextLength": 32000,
|
| 131 |
+
# "apiBase": "https://api.siliconflow.cn/v1",
|
| 132 |
+
# "apiKey": "sk-apdacezvorqcnducqqkhleoiyamgphleouaphgteddnyjvxd",
|
| 133 |
+
# "apiType": "openai",
|
| 134 |
+
# "provider": "openai"
|
| 135 |
+
# },
|
| 136 |
+
# {
|
| 137 |
+
# "title": "8B-DeepSeek-R1",
|
| 138 |
+
# "model": "deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
|
| 139 |
+
# "contextLength": 32000,
|
| 140 |
+
# "apiBase": "https://api.siliconflow.cn/v1",
|
| 141 |
+
# "apiKey": "sk-apdacezvorqcnducqqkhleoiyamgphleouaphgteddnyjvxd",
|
| 142 |
+
# "apiType": "openai",
|
| 143 |
+
# "provider": "openai"
|
| 144 |
+
# },
|
| 145 |
+
# {
|
| 146 |
+
# "title": "32B-DeepSeek-R1",
|
| 147 |
+
# "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
| 148 |
+
# "contextLength": 32000,
|
| 149 |
+
# "apiBase": "https://api.siliconflow.cn/v1",
|
| 150 |
+
# "apiKey": "sk-apdacezvorqcnducqqkhleoiyamgphleouaphgteddnyjvxd",
|
| 151 |
+
# "apiType": "openai",
|
| 152 |
+
# "provider": "openai"
|
| 153 |
+
# }
|
| 154 |
+
# ],
|
| 155 |
+
# "tabAutocompleteModel": {
|
| 156 |
+
# "title": "Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 157 |
+
# "model": "Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 158 |
+
# "contextLength": 32000,
|
| 159 |
+
# "apiBase": "https://api.siliconflow.cn/v1",
|
| 160 |
+
# "apiKey": "sk-apdacezvorqcnducqqkhleoiyamgphleouaphgteddnyjvxd",
|
| 161 |
+
# "apiType": "openai",
|
| 162 |
+
# "provider": "openai"
|
| 163 |
+
# },
|
| 164 |
+
# "contextProviders": [
|
| 165 |
+
# {
|
| 166 |
+
# "name": "currentFile"
|
| 167 |
+
# },
|
| 168 |
+
# {
|
| 169 |
+
# "name": "code",
|
| 170 |
+
# "params": {}
|
| 171 |
+
# },
|
| 172 |
+
# {
|
| 173 |
+
# "name": "docs",
|
| 174 |
+
# "params": {}
|
| 175 |
+
# },
|
| 176 |
+
# {
|
| 177 |
+
# "name": "diff",
|
| 178 |
+
# "params": {}
|
| 179 |
+
# },
|
| 180 |
+
# {
|
| 181 |
+
# "name": "terminal",
|
| 182 |
+
# "params": {}
|
| 183 |
+
# },
|
| 184 |
+
# {
|
| 185 |
+
# "name": "problems",
|
| 186 |
+
# "params": {}
|
| 187 |
+
# },
|
| 188 |
+
# {
|
| 189 |
+
# "name": "folder",
|
| 190 |
+
# "params": {}
|
| 191 |
+
# },
|
| 192 |
+
# {
|
| 193 |
+
# "name": "codebase",
|
| 194 |
+
# "params": {}
|
| 195 |
+
# }
|
| 196 |
+
# ],
|
| 197 |
+
# "slashCommands": [
|
| 198 |
+
# {
|
| 199 |
+
# "name": "share",
|
| 200 |
+
# "description": "Export the current chat session to markdown"
|
| 201 |
+
# },
|
| 202 |
+
# {
|
| 203 |
+
# "name": "cmd",
|
| 204 |
+
# "description": "Generate a shell command"
|
| 205 |
+
# },
|
| 206 |
+
# {
|
| 207 |
+
# "name": "commit",
|
| 208 |
+
# "description": "Generate a git commit message"
|
| 209 |
+
# }
|
| 210 |
+
# ],
|
| 211 |
+
# "embeddingsProvider": {
|
| 212 |
+
# "provider": "free-trial"
|
| 213 |
+
# },
|
| 214 |
+
# "reranker": {
|
| 215 |
+
# "name": "free-trial"
|
| 216 |
+
# }
|
| 217 |
+
# }
|