Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
import requests
|
| 5 |
from io import BytesIO
|
| 6 |
from fastapi import FastAPI, UploadFile, Form
|
| 7 |
-
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 8 |
from starlette.responses import JSONResponse
|
| 9 |
import base64
|
| 10 |
import io
|
|
@@ -27,7 +25,7 @@ def animegan2_transform(input_img, style_name):
|
|
| 27 |
input_img = input_img.convert("RGB")
|
| 28 |
else:
|
| 29 |
raise ValueError("فرمت تصویر ورودی صحیح نیست!")
|
| 30 |
-
|
| 31 |
# تغییر رزولوشن به 460 برای کیفیت بالا
|
| 32 |
input_img = input_img.resize((460, 460))
|
| 33 |
|
|
@@ -46,10 +44,10 @@ def animegan2_transform(input_img, style_name):
|
|
| 46 |
output_img = face2paint_func(model, input_img)
|
| 47 |
return output_img
|
| 48 |
|
| 49 |
-
# ------------------ API
|
| 50 |
-
|
| 51 |
|
| 52 |
-
@
|
| 53 |
async def animegan_api(
|
| 54 |
file: UploadFile,
|
| 55 |
style: str = Form("face_paint_512_v2")
|
|
@@ -64,25 +62,6 @@ async def animegan_api(
|
|
| 64 |
img_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 65 |
return JSONResponse({"image": img_b64})
|
| 66 |
|
| 67 |
-
# ------------------ Gradio UI ------------------
|
| 68 |
-
iface = gr.Interface(
|
| 69 |
-
fn=animegan2_transform,
|
| 70 |
-
inputs=[
|
| 71 |
-
gr.Image(type="pil", label="آپلود تصویر یا وارد کردن URL"),
|
| 72 |
-
gr.Dropdown(
|
| 73 |
-
["face_paint_512_v1", "face_paint_512_v2", "paprika", "celeba_distill"],
|
| 74 |
-
value="face_paint_512_v2",
|
| 75 |
-
label="انتخاب استایل"
|
| 76 |
-
)
|
| 77 |
-
],
|
| 78 |
-
outputs=gr.Image(type="pil", label="تصویر انیمه (کیفیت بالا)"),
|
| 79 |
-
title="AnimeGANv2 - تبدیل تصویر به انیمه با انتخاب استایل",
|
| 80 |
-
description="تصاویر خود را با کیفیت بالا و استایلهای مختلف به انیمه تبدیل کنید."
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
# ترکیب Gradio با FastAPI
|
| 84 |
-
api.mount("/", WSGIMiddleware(iface.app))
|
| 85 |
-
|
| 86 |
if __name__ == "__main__":
|
| 87 |
import uvicorn
|
| 88 |
-
uvicorn.run(
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from PIL import Image
|
| 3 |
import requests
|
| 4 |
from io import BytesIO
|
| 5 |
from fastapi import FastAPI, UploadFile, Form
|
|
|
|
| 6 |
from starlette.responses import JSONResponse
|
| 7 |
import base64
|
| 8 |
import io
|
|
|
|
| 25 |
input_img = input_img.convert("RGB")
|
| 26 |
else:
|
| 27 |
raise ValueError("فرمت تصویر ورودی صحیح نیست!")
|
| 28 |
+
|
| 29 |
# تغییر رزولوشن به 460 برای کیفیت بالا
|
| 30 |
input_img = input_img.resize((460, 460))
|
| 31 |
|
|
|
|
| 44 |
output_img = face2paint_func(model, input_img)
|
| 45 |
return output_img
|
| 46 |
|
| 47 |
+
# ------------------ FastAPI API ------------------
|
| 48 |
+
app = FastAPI()
|
| 49 |
|
| 50 |
+
@app.post("/api/animegan")
|
| 51 |
async def animegan_api(
|
| 52 |
file: UploadFile,
|
| 53 |
style: str = Form("face_paint_512_v2")
|
|
|
|
| 62 |
img_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 63 |
return JSONResponse({"image": img_b64})
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
import uvicorn
|
| 67 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|