File size: 4,442 Bytes
a661d53 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import os
import openai
from openai import OpenAI
import base64
from tqdm import tqdm
import time
import json
from pathlib import Path
from threading import Lock
from typing import Any
_json_write_lock = Lock()
def save_json_file(
data: Any,
file_path: str,
indent: int = 4,
temp_suffix: str = ".tmp"
) -> None:
"""
"""
path = Path(file_path)
path.parent.mkdir(parents=True, exist_ok=True)
temp_path = f"{file_path}{temp_suffix}"
with _json_write_lock:
try:
with open(temp_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=indent)
f.flush()
os.fsync(f.fileno())
os.replace(temp_path, file_path)
except Exception as e:
# 出错则删除临时文件
try:
if os.path.exists(temp_path):
os.remove(temp_path)
except OSError:
pass
raise RuntimeError(f"save json failed: {e}") from e
def read_json_file(file_path):
"""
Reads a JSON file and returns the parsed data as a Python object.
:param file_path: The path to the JSON file
:return: The data parsed from the JSON file
"""
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
return data
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.standard_b64encode(image_file.read()).decode("utf-8")
def merge_json_lists(folder_path):
"""
"""
json_list = [
os.path.join(folder_path, f)
for f in os.listdir(folder_path)
if f.lower().endswith('.json') and os.path.isfile(os.path.join(folder_path, f))
]
merged_list = []
for file_path in json_list:
try:
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
if isinstance(data, list):
merged_list.extend(data)
else:
print(f"waring: {file_path} is not list. skipped")
except Exception as e:
print(f"processing {file_path} error: {str(e)}")
return merged_list
def openai_api(image_path, prompt = None):
if prompt == None:
prompt = "What's in this image?"
base64_image = encode_image(image_path)
client = OpenAI(
base_url='your_url',
api_key='your_key'
)
response = client.chat.completions.create(
model="claude-3-7-sonnet-20250219",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
},
],
}
],
max_tokens=5000,
)
return response
if __name__ == "__main__":
folder_path = r"your_path/poster/data"
save_dir = r"your_path/poster/result"
saved_josn = os.path.join(save_dir,"result.json")
if not os.path.exists(saved_josn):
tasks = merge_json_lists(folder_path)
save_json_file(tasks, saved_josn)
tasks = read_json_file(saved_josn)
max_retries = 4
retry_wait = 10
for item in tqdm(tasks):
if "response" in item: continue
prompt = item["prompt"]
image_path = os.path.join(folder_path, item["path"])
for attempt in range(max_retries):
try:
response = openai_api(image_path, prompt)
item["response"] = response.choices[0].message.content
print(item["response"])
save_json_file(tasks, saved_josn)
break
except Exception as e:
print(f"[Warning] Request failed: {e}")
if attempt < max_retries - 1:
print(f"Retrying in {retry_wait} seconds... (attempt {attempt + 1})")
time.sleep(retry_wait)
else:
print("[Error] Reached max retries. Skipping this item.")
item["error"] = str(e)
|