Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,11 @@ import torch
|
|
| 3 |
from PIL import Image
|
| 4 |
import requests
|
| 5 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# بارگذاری مدلها بر اساس استایل انتخابی
|
| 8 |
def load_model(style_name):
|
|
@@ -23,7 +28,7 @@ def animegan2_transform(input_img, style_name):
|
|
| 23 |
else:
|
| 24 |
raise ValueError("فرمت تصویر ورودی صحیح نیست!")
|
| 25 |
|
| 26 |
-
# تغییر رزولوشن به
|
| 27 |
input_img = input_img.resize((460, 460))
|
| 28 |
|
| 29 |
# بارگذاری مدل انتخابی
|
|
@@ -41,7 +46,25 @@ def animegan2_transform(input_img, style_name):
|
|
| 41 |
output_img = face2paint_func(model, input_img)
|
| 42 |
return output_img
|
| 43 |
|
| 44 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=animegan2_transform,
|
| 47 |
inputs=[
|
|
@@ -57,5 +80,9 @@ iface = gr.Interface(
|
|
| 57 |
description="تصاویر خود را با کیفیت بالا و استایلهای مختلف به انیمه تبدیل کنید."
|
| 58 |
)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
if __name__ == "__main__":
|
| 61 |
-
|
|
|
|
|
|
| 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
|
| 11 |
|
| 12 |
# بارگذاری مدلها بر اساس استایل انتخابی
|
| 13 |
def load_model(style_name):
|
|
|
|
| 28 |
else:
|
| 29 |
raise ValueError("فرمت تصویر ورودی صحیح نیست!")
|
| 30 |
|
| 31 |
+
# تغییر رزولوشن به 460 برای کیفیت بالا
|
| 32 |
input_img = input_img.resize((460, 460))
|
| 33 |
|
| 34 |
# بارگذاری مدل انتخابی
|
|
|
|
| 46 |
output_img = face2paint_func(model, input_img)
|
| 47 |
return output_img
|
| 48 |
|
| 49 |
+
# ------------------ API با FastAPI ------------------
|
| 50 |
+
api = FastAPI()
|
| 51 |
+
|
| 52 |
+
@api.post("/api/animegan")
|
| 53 |
+
async def animegan_api(
|
| 54 |
+
file: UploadFile,
|
| 55 |
+
style: str = Form("face_paint_512_v2")
|
| 56 |
+
):
|
| 57 |
+
img_bytes = await file.read()
|
| 58 |
+
input_img = Image.open(BytesIO(img_bytes)).convert("RGB")
|
| 59 |
+
output_img = animegan2_transform(input_img, style)
|
| 60 |
+
|
| 61 |
+
# تبدیل به Base64
|
| 62 |
+
buf = io.BytesIO()
|
| 63 |
+
output_img.save(buf, format="PNG")
|
| 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=[
|
|
|
|
| 80 |
description="تصاویر خود را با کیفیت بالا و استایلهای مختلف به انیمه تبدیل کنید."
|
| 81 |
)
|
| 82 |
|
| 83 |
+
# ترکیب Gradio با FastAPI
|
| 84 |
+
api.mount("/", WSGIMiddleware(iface.app))
|
| 85 |
+
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
import uvicorn
|
| 88 |
+
uvicorn.run(api, host="0.0.0.0", port=7860)
|