userIdc2024 commited on
Commit
1931cca
·
verified ·
1 Parent(s): c00d7dd

Update generator_function/image_function.py

Browse files
Files changed (1) hide show
  1. generator_function/image_function.py +148 -24
generator_function/image_function.py CHANGED
@@ -38,16 +38,137 @@ MAX_WORKERS = min(32, (os.cpu_count() or 1) + 4)
38
  REQUEST_TIMEOUT = 30
39
  RETRY_ATTEMPTS = 3
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  MODEL_REGISTRY: Dict[str, Dict[str, Any]] = {
42
- "imagegen-4-ultra": {"id": "google/imagen-4-ultra","aspect_ratios": ["1:1","16:9","9:16","3:4","4:3"],"param_name":"aspect_ratio"},
43
- "imagen-4": {"id": "google/imagen-4","aspect_ratios": ["1:1","16:9","9:16","3:4","4:3"],"param_name":"aspect_ratio"},
44
- "nano-banana": {"id": "google/nano-banana","aspect_ratios": ["1:1","16:9","9:16","3:4","4:3"],"param_name":"aspect_ratio"},
45
- "qwen": {"id": "qwen/qwen-image","aspect_ratios": ["1:1","16:9","9:16","3:4","4:3","3:2","2:3"],"param_name":"aspect_ratio"},
46
- "seedream-3": {"id": "bytedance/seedream-3","aspect_ratios": ["1:1","16:9","9:16","3:4","4:3","3:2","2:3","21:9"],"param_name":"aspect_ratio"},
47
- "recraft-v3": {"id": "recraft-ai/recraft-v3","aspect_ratios": ["1:1","4:3","3:4","3:2","2:3","16:9","9:16","1:2","2:1","7:5","5:7","4:5","5:4","3:5","5:3"],"param_name":"aspect_ratio"},
48
- "photon": {"id": "luma/photon","aspect_ratios": ["1:1","3:4","4:3","9:16","16:9","9:21","21:9"],"param_name":"aspect_ratio"},
49
- "ideogram-v3-quality": {"id": "ideogram-ai/ideogram-v3-quality","aspect_ratios": ["1:3","3:1","1:2","2:1","9:16","16:9","10:16","16:10","2:3","3:2","3:4","4:3","4:5","5:4","1:1"],"param_name":"aspect_ratio"},
50
- "z-image-turbo": {"id": "prunaai/z-image-turbo","param_name": None,"supports_aspect_ratio": False}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  _thread_local = threading.local()
53
 
@@ -109,24 +230,27 @@ def _generate_one(model_key: str,prompt: str, aspect_ratio: str,params: Dict[str
109
  return []
110
  for attempt in range(RETRY_ATTEMPTS):
111
  try:
112
- input_payload = {"prompt": prompt}
113
- if cfg["id"] == "prunaai/z-image-turbo":
114
- input_payload["width"] = params.get("width", 1024)
115
- input_payload["height"] = params.get("height", 1024)
116
- input_payload["output_quality"] = params.get("output_quality", 90)
117
-
118
- elif cfg.get("param_name"):
119
- input_payload[cfg["param_name"]] = aspect_ratio
 
120
 
121
  output = replicate.run(cfg["id"], input=input_payload)
122
 
 
123
  if isinstance(output, list) and output:
124
- return [getattr(output[0], "url", str(output[0]))]
125
- if isinstance(output, str):
126
- return [output]
127
- if hasattr(output, "url"):
128
- return [output.url]
129
-
 
130
  except Exception as e:
131
  if attempt == RETRY_ATTEMPTS - 1:
132
  logger.error(f"replicate run failed: {e}")
@@ -407,4 +531,4 @@ def generate_image(file_path, size, quality, category, sentiment, user_prompt, p
407
  return image_bytes
408
  except Exception as e:
409
  logger.exception(f"Failed to generate image for {file_path}: {e}")
410
- raise
 
38
  REQUEST_TIMEOUT = 30
39
  RETRY_ATTEMPTS = 3
40
 
41
+
42
+ def get_cost_per_image(
43
+ model_key: str,
44
+ width: int | None = None,
45
+ height: int | None = None,
46
+ ) -> float:
47
+ cfg = MODEL_REGISTRY.get(model_key, {})
48
+ price = cfg.get("price")
49
+ unit = cfg.get("price_unit")
50
+
51
+ if not price:
52
+ return 0.0
53
+
54
+ if unit == "per_image":
55
+ return round(price, 4)
56
+
57
+ return 0.0
58
+
59
  MODEL_REGISTRY: Dict[str, Dict[str, Any]] = {
60
+ "imagegen-4-ultra": {
61
+ "id": "google/imagen-4-ultra",
62
+ "price": 0.06,
63
+ "price_unit": "per_image",
64
+ "aspect_ratios": ["1:1","16:9","9:16","3:4","4:3"],
65
+ "param_name": "aspect_ratio",
66
+ },
67
+
68
+ "imagen-4": {
69
+ "id": "google/imagen-4",
70
+ "price": 0.04,
71
+ "price_unit": "per_image",
72
+ "aspect_ratios": ["1:1","16:9","9:16","3:4","4:3"],
73
+ "param_name": "aspect_ratio",
74
+ },
75
+
76
+ "nano-banana": {
77
+ "id": "google/nano-banana",
78
+ "price": 0.039,
79
+ "price_unit": "per_image",
80
+ "aspect_ratios": ["1:1","16:9","9:16","3:4","4:3"],
81
+ "param_name": "aspect_ratio",
82
+ },
83
+
84
+ "qwen": {
85
+ "id": "qwen/qwen-image",
86
+ "price": 0.025,
87
+ "price_unit": "per_image",
88
+ "aspect_ratios": ["1:1","16:9","9:16","3:4","4:3","3:2","2:3"],
89
+ "param_name": "aspect_ratio",
90
+ },
91
+
92
+ "seedream-3": {
93
+ "id": "bytedance/seedream-3",
94
+ "price": 0.03,
95
+ "price_unit": "per_image",
96
+ "aspect_ratios": ["1:1","16:9","9:16","3:4","4:3","3:2","2:3","21:9"],
97
+ "param_name": "aspect_ratio",
98
+ },
99
+
100
+ "recraft-v3": {
101
+ "id": "recraft-ai/recraft-v3",
102
+ "price": 0.04,
103
+ "price_unit": "per_image",
104
+ "aspect_ratios": [
105
+ "1:1","4:3","3:4","3:2","2:3","16:9","9:16",
106
+ "1:2","2:1","7:5","5:7","4:5","5:4","3:5","5:3"
107
+ ],
108
+ "param_name": "aspect_ratio",
109
+ },
110
+
111
+ "photon": {
112
+ "id": "luma/photon",
113
+ "price": 0.03,
114
+ "price_unit": "per_image",
115
+ "aspect_ratios": ["1:1","3:4","4:3","9:16","16:9","9:21","21:9"],
116
+ "param_name": "aspect_ratio",
117
+ },
118
+
119
+ "ideogram-v3-quality": {
120
+ "id": "ideogram-ai/ideogram-v3-quality",
121
+ "price": 0.09,
122
+ "price_unit": "per_image",
123
+ "aspect_ratios": [
124
+ "1:3","3:1","1:2","2:1","9:16","16:9","10:16",
125
+ "16:10","2:3","3:2","3:4","4:3","4:5","5:4","1:1"
126
+ ],
127
+ "param_name": "aspect_ratio",
128
+ },
129
+
130
+ "ideogram-v3-turbo": {
131
+ "id": "ideogram-ai/ideogram-v3-turbo",
132
+ "price": 0.03,
133
+ "price_unit": "per_image",
134
+ "aspect_ratios": [
135
+ "1:3","3:1","1:2","2:1","9:16","16:9","10:16",
136
+ "16:10","2:3","3:2","3:4","4:3","4:5","5:4","1:1"
137
+ ],
138
+ "param_name": "aspect_ratio",
139
+ },
140
+
141
+ "flux-2-max": {
142
+ "id": "black-forest-labs/flux-2-max",
143
+ "price": 0.04,
144
+ "price_unit": "per_image",
145
+ "aspect_ratios": ["1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9"],
146
+ "param_name": "aspect_ratio",
147
+ },
148
+
149
+ "p-image": {
150
+ "id": "prunaai/p-image",
151
+ "price": 0.005,
152
+ "price_unit": "per_image",
153
+ "type": "custom",
154
+ },
155
+
156
+ "z-image-turbo": {
157
+ "id": "prunaai/z-image-turbo",
158
+ "price": 0.01,
159
+ "price_unit": "per_image",
160
+ "type": "custom",
161
+ },
162
+
163
+ "bytedance/seedream-4.5": {
164
+ "id": "bytedance/seedream-4.5",
165
+ "size": "2k",
166
+ "price": 0.04,
167
+ "price_unit": "per_image",
168
+ "aspect_ratios": ["1:1","2:3","3:2","3:4","4:3","9:16","16:9","21:9"],
169
+ "param_name": "aspect_ratio",
170
+ },
171
+ }
172
 
173
  _thread_local = threading.local()
174
 
 
230
  return []
231
  for attempt in range(RETRY_ATTEMPTS):
232
  try:
233
+ if cfg.get("type") == "custom":
234
+ input_payload = {
235
+ "prompt": prompt,
236
+ "width": 1024,
237
+ "height": 1024,
238
+ }
239
+ else:
240
+ param_name = cfg.get("param_name", "aspect_ratio")
241
+ input_payload = {"prompt": prompt,param_name: aspect_ratio}
242
 
243
  output = replicate.run(cfg["id"], input=input_payload)
244
 
245
+ urls: List[str] = []
246
  if isinstance(output, list) and output:
247
+ urls = [getattr(output[0], "url", str(output[0]))]
248
+ elif isinstance(output, str):
249
+ urls = [output]
250
+ elif hasattr(output, "url"):
251
+ urls = [output.url]
252
+ if urls:
253
+ return urls
254
  except Exception as e:
255
  if attempt == RETRY_ATTEMPTS - 1:
256
  logger.error(f"replicate run failed: {e}")
 
531
  return image_bytes
532
  except Exception as e:
533
  logger.exception(f"Failed to generate image for {file_path}: {e}")
534
+ raise