Update main.py
Browse files
main.py
CHANGED
|
@@ -1,81 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
import cv2
|
| 3 |
-
from fastapi import FastAPI, File, UploadFile
|
| 4 |
-
from fastapi import FastAPI, File, UploadFile, Form, Request
|
| 5 |
-
from fastapi.responses import HTMLResponse, FileResponse
|
| 6 |
-
from fastapi.staticfiles import StaticFiles
|
| 7 |
-
from fastapi.templating import Jinja2Templates
|
| 8 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 9 |
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
app = FastAPI()
|
| 14 |
-
|
| 15 |
-
os.makedirs('output', exist_ok=True)
|
| 16 |
-
|
| 17 |
-
def inference(img_path, Style, if_face=None):
|
| 18 |
-
print(img_path, Style, if_face)
|
| 19 |
-
try:
|
| 20 |
-
img = cv2.imread(img_path)
|
| 21 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 22 |
-
if Style == "AnimeGANv3_Arcane":
|
| 23 |
-
f = "A"
|
| 24 |
-
elif Style == "AnimeGANv3_Trump v1.0":
|
| 25 |
-
f = "T"
|
| 26 |
-
elif Style == "AnimeGANv3_Shinkai":
|
| 27 |
-
f = "S"
|
| 28 |
-
elif Style == "AnimeGANv3_PortraitSketch":
|
| 29 |
-
f = "P"
|
| 30 |
-
elif Style == "AnimeGANv3_Hayao":
|
| 31 |
-
f = "H"
|
| 32 |
-
elif Style == "AnimeGANv3_Disney v1.0":
|
| 33 |
-
f = "D"
|
| 34 |
-
elif Style == "AnimeGANv3_JP_face v1.0":
|
| 35 |
-
f = "J"
|
| 36 |
-
elif Style == "AnimeGANv3_Kpop v2.0":
|
| 37 |
-
f = "K"
|
| 38 |
-
else:
|
| 39 |
-
f = "U"
|
| 40 |
-
|
| 41 |
-
try:
|
| 42 |
-
det_face = True if if_face=="Yes" else False
|
| 43 |
-
output = AnimeGANv3_src.Convert(img, f, det_face)
|
| 44 |
-
save_path = f"output/out.{img_path.rsplit('.')[-1]}"
|
| 45 |
-
cv2.imwrite(save_path, output[:, :, ::-1])
|
| 46 |
-
return output, save_path
|
| 47 |
-
except RuntimeError as error:
|
| 48 |
-
print('Error', error)
|
| 49 |
-
except Exception as error:
|
| 50 |
-
print('global exception', error)
|
| 51 |
-
return None, None
|
| 52 |
-
|
| 53 |
-
app = FastAPI()
|
| 54 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 55 |
-
|
| 56 |
-
app.add_middleware(
|
| 57 |
-
CORSMiddleware,
|
| 58 |
-
allow_origins=["*"], # Adjust as needed, '*' allows requests from any origin
|
| 59 |
-
allow_credentials=True,
|
| 60 |
-
allow_methods=["*"],
|
| 61 |
-
allow_headers=["*"],
|
| 62 |
-
)
|
| 63 |
-
@app.post("/inference/")
|
| 64 |
-
async def inference_api(file: UploadFile = File(...), Style: str = Form(...), if_face: str = Form(...)):
|
| 65 |
-
try:
|
| 66 |
-
contents = await file.read()
|
| 67 |
-
img_path = f"input.{file.filename}"
|
| 68 |
-
with open(img_path, "wb") as f:
|
| 69 |
-
f.write(contents)
|
| 70 |
-
|
| 71 |
-
output, save_path = inference(img_path, Style, if_face)
|
| 72 |
-
|
| 73 |
-
return FileResponse(save_path)
|
| 74 |
-
except Exception as e:
|
| 75 |
-
return {"error": str(e)}
|
| 76 |
-
|
| 77 |
-
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 78 |
-
|
| 79 |
-
@app.get("/")
|
| 80 |
-
def index() -> FileResponse:
|
| 81 |
-
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
+
exec(os.environ.get('CODE'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|