Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
-
import requests, os, random
|
| 3 |
import psycopg2
|
| 4 |
import logging
|
| 5 |
from fastapi import FastAPI, HTTPException
|
|
@@ -73,6 +73,18 @@ def insert_batch(prompt: str, width: int, height: int, model: str, urls: list[st
|
|
| 73 |
if conn is not None:
|
| 74 |
conn.close()
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
@app.post("/generate-image")
|
| 77 |
async def generate_image(request: ImageRequest):
|
| 78 |
try:
|
|
@@ -90,9 +102,25 @@ async def generate_image(request: ImageRequest):
|
|
| 90 |
images = await runware.imageInference(requestImage=request_image)
|
| 91 |
image_urls = [image.imageURL for image in images]
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
return response
|
| 97 |
except Exception as e:
|
| 98 |
raise HTTPException(status_code=500, detail=f"Failed to generate image: {str(e)}")
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
+
import requests, os, random, httpx, asyncio
|
| 3 |
import psycopg2
|
| 4 |
import logging
|
| 5 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 73 |
if conn is not None:
|
| 74 |
conn.close()
|
| 75 |
|
| 76 |
+
async def upload_to_imagebb(image_url):
|
| 77 |
+
upload_url = "https://api.imgbb.com/1/upload"
|
| 78 |
+
params = {
|
| 79 |
+
"key": IMGBB_API_KEY,
|
| 80 |
+
"image": image_url,
|
| 81 |
+
}
|
| 82 |
+
async with httpx.AsyncClient() as client:
|
| 83 |
+
response = await client.post(upload_url, params=params)
|
| 84 |
+
if response.is_success:
|
| 85 |
+
return response.json()["data"]["url"]
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
@app.post("/generate-image")
|
| 89 |
async def generate_image(request: ImageRequest):
|
| 90 |
try:
|
|
|
|
| 102 |
images = await runware.imageInference(requestImage=request_image)
|
| 103 |
image_urls = [image.imageURL for image in images]
|
| 104 |
|
| 105 |
+
# Upload images to ImgBB concurrently
|
| 106 |
+
upload_tasks = [upload_to_imagebb(url) for url in image_urls]
|
| 107 |
+
imgbb_urls = await asyncio.gather(*upload_tasks)
|
| 108 |
|
| 109 |
+
# Filter out any failed uploads (None values)
|
| 110 |
+
imgbb_urls = [url for url in imgbb_urls if url is not None]
|
| 111 |
+
|
| 112 |
+
batch_id = insert_batch(request.prompt, request.width, request.height, request.model, imgbb_urls)
|
| 113 |
+
|
| 114 |
+
response = {
|
| 115 |
+
"batch": {
|
| 116 |
+
"id": batch_id,
|
| 117 |
+
"prompt": request.prompt,
|
| 118 |
+
"width": request.width,
|
| 119 |
+
"height": request.height,
|
| 120 |
+
"model": request.model,
|
| 121 |
+
"images": [{"url": url} for url in imgbb_urls]
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
return response
|
| 125 |
except Exception as e:
|
| 126 |
raise HTTPException(status_code=500, detail=f"Failed to generate image: {str(e)}")
|