Update main.py
Browse files
main.py
CHANGED
@@ -5,6 +5,7 @@ from typing import List, Optional
|
|
5 |
import time
|
6 |
import json
|
7 |
import os
|
|
|
8 |
import httpx
|
9 |
from dotenv import load_dotenv
|
10 |
|
@@ -14,6 +15,9 @@ from models import AVAILABLE_MODELS, MODEL_ALIASES
|
|
14 |
|
15 |
# Require IMAGE_API_URL strictly from environment
|
16 |
IMAGE_API_URL = os.environ["IMAGE_API_URL"]
|
|
|
|
|
|
|
17 |
|
18 |
app = FastAPI()
|
19 |
|
@@ -149,6 +153,7 @@ class ImageGenerationRequest(BaseModel):
|
|
149 |
aspect_ratio: Optional[str] = "1:1"
|
150 |
n: Optional[int] = 1
|
151 |
user: Optional[str] = None
|
|
|
152 |
|
153 |
|
154 |
@app.post("/v1/images/generations")
|
@@ -157,26 +162,79 @@ async def generate_images(request: ImageGenerationRequest):
|
|
157 |
|
158 |
async with httpx.AsyncClient(timeout=60) as client:
|
159 |
for _ in range(request.n):
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
return {
|
182 |
"created": int(time.time()),
|
|
|
5 |
import time
|
6 |
import json
|
7 |
import os
|
8 |
+
import base64
|
9 |
import httpx
|
10 |
from dotenv import load_dotenv
|
11 |
|
|
|
15 |
|
16 |
# Require IMAGE_API_URL strictly from environment
|
17 |
IMAGE_API_URL = os.environ["IMAGE_API_URL"]
|
18 |
+
GPT_IMAGE_API_URL = "https://www.chatwithmono.xyz/api/image"
|
19 |
+
SNAPZION_UPLOAD_URL = "https://upload.snapzion.com/api/public-upload"
|
20 |
+
SNAPZION_API_KEY = os.environ["SNAP"]
|
21 |
|
22 |
app = FastAPI()
|
23 |
|
|
|
153 |
aspect_ratio: Optional[str] = "1:1"
|
154 |
n: Optional[int] = 1
|
155 |
user: Optional[str] = None
|
156 |
+
model: Optional[str] = "default"
|
157 |
|
158 |
|
159 |
@app.post("/v1/images/generations")
|
|
|
162 |
|
163 |
async with httpx.AsyncClient(timeout=60) as client:
|
164 |
for _ in range(request.n):
|
165 |
+
if request.model == "gpt-image-1":
|
166 |
+
headers = {
|
167 |
+
'Content-Type': 'application/json',
|
168 |
+
'User-Agent': 'Mozilla/5.0',
|
169 |
+
'Referer': 'https://www.chatwithmono.xyz/',
|
170 |
+
'sec-ch-ua-platform': '"Windows"',
|
171 |
+
'sec-ch-ua': '"Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
|
172 |
+
'sec-ch-ua-mobile': '?0',
|
173 |
+
}
|
174 |
+
|
175 |
+
payload = {
|
176 |
+
"prompt": request.prompt,
|
177 |
+
"model": "gpt-image-1"
|
178 |
+
}
|
179 |
+
|
180 |
+
resp = await client.post(GPT_IMAGE_API_URL, headers=headers, json=payload)
|
181 |
+
if resp.status_code != 200:
|
182 |
+
return JSONResponse(
|
183 |
+
status_code=502,
|
184 |
+
content={"error": "GPT-Image generation failed", "details": resp.text}
|
185 |
+
)
|
186 |
+
|
187 |
+
data = resp.json()
|
188 |
+
b64_image = data.get("image")
|
189 |
+
|
190 |
+
if not b64_image:
|
191 |
+
return JSONResponse(status_code=502, content={"error": "No base64 image in response"})
|
192 |
+
|
193 |
+
# Reconstruct full data URI
|
194 |
+
full_image_data = f"data:image/png;base64,{b64_image}"
|
195 |
+
|
196 |
+
# Upload to Snapzion
|
197 |
+
upload_headers = {"Authorization": SNAPZION_API_KEY}
|
198 |
+
upload_files = {
|
199 |
+
'file': ('image.png', base64.b64decode(b64_image), 'image/png')
|
200 |
+
}
|
201 |
+
|
202 |
+
upload_resp = await client.post(SNAPZION_UPLOAD_URL, headers=upload_headers, files=upload_files)
|
203 |
+
if upload_resp.status_code != 200:
|
204 |
+
return JSONResponse(
|
205 |
+
status_code=502,
|
206 |
+
content={"error": "Upload failed", "details": upload_resp.text}
|
207 |
+
)
|
208 |
+
|
209 |
+
upload_data = upload_resp.json()
|
210 |
+
image_url = upload_data.get("url")
|
211 |
+
|
212 |
+
results.append({
|
213 |
+
"url": image_url,
|
214 |
+
"b64_json": b64_image,
|
215 |
+
"data_uri": full_image_data
|
216 |
+
})
|
217 |
+
else:
|
218 |
+
# Default image generator
|
219 |
+
params = {
|
220 |
+
"prompt": request.prompt,
|
221 |
+
"aspect_ratio": request.aspect_ratio,
|
222 |
+
"link": "typegpt.net"
|
223 |
+
}
|
224 |
+
|
225 |
+
resp = await client.get(IMAGE_API_URL, params=params)
|
226 |
+
if resp.status_code != 200:
|
227 |
+
return JSONResponse(
|
228 |
+
status_code=502,
|
229 |
+
content={"error": "Image generation failed", "details": resp.text}
|
230 |
+
)
|
231 |
+
|
232 |
+
data = resp.json()
|
233 |
+
results.append({
|
234 |
+
"url": data.get("image_link"),
|
235 |
+
"b64_json": data.get("base64_output"),
|
236 |
+
"retries": data.get("attempt")
|
237 |
+
})
|
238 |
|
239 |
return {
|
240 |
"created": int(time.time()),
|