Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import Optional
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
class ImageRequest(BaseModel):
|
| 9 |
+
prompt: str
|
| 10 |
+
width: Optional[int] = 720
|
| 11 |
+
height: Optional[int] = 1280
|
| 12 |
+
n: Optional[int] = 1
|
| 13 |
+
|
| 14 |
+
@app.post("/v1/images/generations")
|
| 15 |
+
async def generate_image(request: ImageRequest):
|
| 16 |
+
images = []
|
| 17 |
+
for _ in range(request.n):
|
| 18 |
+
seed = random.randint(0, 1000000)
|
| 19 |
+
url = (
|
| 20 |
+
f"https://image.pollinations.ai/prompt/{request.prompt}"
|
| 21 |
+
f"?width={request.width}&height={request.height}"
|
| 22 |
+
f"&seed={seed}&enhance=true&nologo=true&model=flux"
|
| 23 |
+
)
|
| 24 |
+
images.append({"url": url})
|
| 25 |
+
|
| 26 |
+
return {
|
| 27 |
+
"created": int(random.time()),
|
| 28 |
+
"data": images
|
| 29 |
+
}
|