Spaces:
Runtime error
Runtime error
use s3 to store files
Browse files- requirements.txt +3 -1
- stablediffusion-infinity/app.py +68 -17
requirements.txt
CHANGED
@@ -9,4 +9,6 @@ opencv-python-headless
|
|
9 |
fastapi
|
10 |
uvicorn
|
11 |
httpx
|
12 |
-
gradio
|
|
|
|
|
|
9 |
fastapi
|
10 |
uvicorn
|
11 |
httpx
|
12 |
+
gradio
|
13 |
+
boto3
|
14 |
+
python-magic
|
stablediffusion-infinity/app.py
CHANGED
@@ -5,8 +5,9 @@ from random import sample
|
|
5 |
from sched import scheduler
|
6 |
|
7 |
import uvicorn
|
8 |
-
from fastapi import FastAPI, Response
|
9 |
from fastapi.staticfiles import StaticFiles
|
|
|
10 |
|
11 |
import httpx
|
12 |
from urllib.parse import urljoin
|
@@ -23,13 +24,29 @@ import base64
|
|
23 |
import skimage
|
24 |
import skimage.measure
|
25 |
from utils import *
|
|
|
|
|
26 |
|
27 |
-
|
|
|
|
|
28 |
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
WHITES = 66846720
|
32 |
MASK = Image.open("mask.png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
try:
|
34 |
SAMPLING_MODE = Image.Resampling.LANCZOS
|
35 |
except Exception as e:
|
@@ -209,20 +226,44 @@ with blocks as demo:
|
|
209 |
|
210 |
blocks.config['dev_mode'] = False
|
211 |
|
212 |
-
S3_HOST = "https://s3.amazonaws.com"
|
213 |
-
|
214 |
-
|
215 |
-
@app.get("/uploads/{path:path}")
|
216 |
-
async def uploads(path: str, response: Response):
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
|
227 |
|
228 |
app = gr.mount_gradio_app(app, blocks, "/gradio",
|
@@ -230,6 +271,16 @@ app = gr.mount_gradio_app(app, blocks, "/gradio",
|
|
230 |
|
231 |
app.mount("/", StaticFiles(directory="../static", html=True), name="static")
|
232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
if __name__ == "__main__":
|
234 |
uvicorn.run(app, host="0.0.0.0", port=7860,
|
235 |
log_level="debug", reload=False)
|
|
|
5 |
from sched import scheduler
|
6 |
|
7 |
import uvicorn
|
8 |
+
from fastapi import FastAPI, Response, BackgroundTasks, HTTPException, UploadFile, File, status
|
9 |
from fastapi.staticfiles import StaticFiles
|
10 |
+
from fastapi.middleware.cors import CORSMiddleware
|
11 |
|
12 |
import httpx
|
13 |
from urllib.parse import urljoin
|
|
|
24 |
import skimage
|
25 |
import skimage.measure
|
26 |
from utils import *
|
27 |
+
import boto3
|
28 |
+
import magic
|
29 |
|
30 |
+
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
|
31 |
+
AWS_SECRET_KEY = os.getenv('AWS_SECRET_KEY')
|
32 |
+
AWS_S3_BUCKET_NAME = os.getenv('AWS_S3_BUCKET_NAME')
|
33 |
|
34 |
+
FILE_TYPES = {
|
35 |
+
'image/png': 'png',
|
36 |
+
'image/jpeg': 'jpg',
|
37 |
+
}
|
38 |
|
39 |
WHITES = 66846720
|
40 |
MASK = Image.open("mask.png")
|
41 |
+
|
42 |
+
app = FastAPI()
|
43 |
+
|
44 |
+
auth_token = os.environ.get("API_TOKEN") or True
|
45 |
+
|
46 |
+
|
47 |
+
s3 = boto3.client(service_name='s3',
|
48 |
+
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
49 |
+
aws_secret_access_key=AWS_SECRET_KEY)
|
50 |
try:
|
51 |
SAMPLING_MODE = Image.Resampling.LANCZOS
|
52 |
except Exception as e:
|
|
|
226 |
|
227 |
blocks.config['dev_mode'] = False
|
228 |
|
229 |
+
# S3_HOST = "https://s3.amazonaws.com"
|
230 |
+
|
231 |
+
|
232 |
+
# @app.get("/uploads/{path:path}")
|
233 |
+
# async def uploads(path: str, response: Response):
|
234 |
+
# async with httpx.AsyncClient() as client:
|
235 |
+
# proxy = await client.get(f"{S3_HOST}/{path}")
|
236 |
+
# response.body = proxy.content
|
237 |
+
# response.status_code = proxy.status_code
|
238 |
+
# response.headers['Access-Control-Allow-Origin'] = '*'
|
239 |
+
# response.headers['Access-Control-Allow-Methods'] = 'POST, GET, DELETE, OPTIONS'
|
240 |
+
# response.headers['Access-Control-Allow-Headers'] = 'Authorization, Content-Type'
|
241 |
+
# response.headers['Cache-Control'] = 'max-age=31536000'
|
242 |
+
# return response
|
243 |
+
|
244 |
+
@app.post('/uploadfile/')
|
245 |
+
async def create_upload_file(background_tasks: BackgroundTasks, file: UploadFile | None = None):
|
246 |
+
contents = await file.read()
|
247 |
+
file_size = len(contents)
|
248 |
+
if not 0 < file_size < 2E+06:
|
249 |
+
raise HTTPException(
|
250 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
251 |
+
detail='Supported file size is less than 2 MB'
|
252 |
+
)
|
253 |
+
file_type = magic.from_buffer(contents, mime=True)
|
254 |
+
if file_type.lower() not in FILE_TYPES:
|
255 |
+
raise HTTPException(
|
256 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
257 |
+
detail=f'Unsupported file type {file_type}. Supported types are {FILE_TYPES}'
|
258 |
+
)
|
259 |
+
temp_file = io.BytesIO()
|
260 |
+
temp_file.write(contents)
|
261 |
+
temp_file.seek(0)
|
262 |
+
s3.upload_fileobj(Fileobj=temp_file, Bucket=AWS_S3_BUCKET_NAME, Key="uploads/" +
|
263 |
+
file.filename, ExtraArgs={"ContentType": file.content_type})
|
264 |
+
temp_file.close()
|
265 |
+
|
266 |
+
return {"url": f'https://d26smi9133w0oo.cloudfront.net/uploads/{file.filename}', "filename": file.filename}
|
267 |
|
268 |
|
269 |
app = gr.mount_gradio_app(app, blocks, "/gradio",
|
|
|
271 |
|
272 |
app.mount("/", StaticFiles(directory="../static", html=True), name="static")
|
273 |
|
274 |
+
origins = ["*"]
|
275 |
+
|
276 |
+
app.add_middleware(
|
277 |
+
CORSMiddleware,
|
278 |
+
allow_origins=origins,
|
279 |
+
allow_credentials=True,
|
280 |
+
allow_methods=["*"],
|
281 |
+
allow_headers=["*"],
|
282 |
+
)
|
283 |
+
|
284 |
if __name__ == "__main__":
|
285 |
uvicorn.run(app, host="0.0.0.0", port=7860,
|
286 |
log_level="debug", reload=False)
|