Update app.py
Browse files
app.py
CHANGED
|
@@ -2,64 +2,93 @@ from fastapi import FastAPI, File, UploadFile, Form
|
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
-
import io
|
| 6 |
-
import requests
|
| 7 |
from io import BytesIO
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
def load_model(style_name):
|
| 13 |
-
|
| 14 |
-
"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# پردازش تصویر
|
| 22 |
def animegan2_transform(input_img, style_name):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# پردازش تصویر
|
| 54 |
-
output_img = animegan2_transform(input_img, style_name)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
import uvicorn
|
|
|
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import logging
|
| 8 |
|
| 9 |
+
# تنظیم لاگگیری
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# بارگذاری مدلها
|
| 16 |
def load_model(style_name):
|
| 17 |
+
try:
|
| 18 |
+
logger.info(f"Loading model for style: {style_name}")
|
| 19 |
+
model = torch.hub.load(
|
| 20 |
+
"bryandlee/animegan2-pytorch:main",
|
| 21 |
+
"generator",
|
| 22 |
+
pretrained=style_name,
|
| 23 |
+
verbose=False
|
| 24 |
+
).eval()
|
| 25 |
+
return model
|
| 26 |
+
except Exception as e:
|
| 27 |
+
logger.error(f"Error loading model: {str(e)}")
|
| 28 |
+
raise
|
| 29 |
|
| 30 |
+
# پردازش تصویر
|
| 31 |
def animegan2_transform(input_img, style_name):
|
| 32 |
+
try:
|
| 33 |
+
logger.info("Processing image...")
|
| 34 |
+
if isinstance(input_img, str):
|
| 35 |
+
logger.info("Input is URL, fetching image...")
|
| 36 |
+
input_img = Image.open(BytesIO(requests.get(input_img).content)).convert("RGB")
|
| 37 |
+
elif isinstance(input_img, Image.Image):
|
| 38 |
+
input_img = input_img.convert("RGB")
|
| 39 |
+
else:
|
| 40 |
+
raise ValueError("فرمت تصویر ورودی صحیح نیست!")
|
| 41 |
+
|
| 42 |
+
# تغییر رزولوشن
|
| 43 |
+
input_img = input_img.resize((460, 460))
|
| 44 |
+
|
| 45 |
+
# بارگذاری مدل
|
| 46 |
+
model = load_model(style_name)
|
| 47 |
+
face2paint_func = torch.hub.load(
|
| 48 |
+
"bryandlee/animegan2-pytorch:main",
|
| 49 |
+
"face2paint",
|
| 50 |
+
size=1024,
|
| 51 |
+
verbose=False
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# اجرای مدل
|
| 55 |
+
output_img = face2paint_func(model, input_img)
|
| 56 |
+
logger.info("Image processed successfully")
|
| 57 |
+
return output_img
|
| 58 |
+
except Exception as e:
|
| 59 |
+
logger.error(f"Error processing image: {str(e)}")
|
| 60 |
+
raise
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Endpoint برای API
|
| 63 |
+
@app.post("/transform")
|
| 64 |
+
async def transform_image(file: UploadFile = File(...), style: str = Form(default="face_paint_512_v2")):
|
| 65 |
+
try:
|
| 66 |
+
logger.info(f"Received image with style: {style}")
|
| 67 |
+
image = Image.open(file.file).convert("RGB")
|
| 68 |
+
output_img = animegan2_transform(image, style)
|
| 69 |
+
output_buffer = BytesIO()
|
| 70 |
+
output_img.save(output_buffer, format="PNG")
|
| 71 |
+
output_buffer.seek(0)
|
| 72 |
+
return StreamingResponse(output_buffer, media_type="image/png")
|
| 73 |
+
except Exception as e:
|
| 74 |
+
logger.error(f"API error: {str(e)}")
|
| 75 |
+
return {"error": str(e)}
|
| 76 |
|
| 77 |
+
# رابط Gradio (برای تست در Hugging Face)
|
| 78 |
+
iface = gr.Interface(
|
| 79 |
+
fn=animegan2_transform,
|
| 80 |
+
inputs=[
|
| 81 |
+
gr.Image(type="pil", label="آپلود تصویر یا وارد کردن URL"),
|
| 82 |
+
gr.Dropdown(
|
| 83 |
+
["face_paint_512_v1", "face_paint_512_v2", "paprika", "celeba_distill"],
|
| 84 |
+
value="face_paint_512_v2",
|
| 85 |
+
label="انتخاب استایل"
|
| 86 |
+
)
|
| 87 |
+
],
|
| 88 |
+
outputs=gr.Image(type="pil", label="تصویر انیمه (کیفیت بالا)"),
|
| 89 |
+
title="AnimeGANv2 - تبدیل تصویر به انیمه با انتخاب استایل",
|
| 90 |
+
description="تصاویر خود را با کیفیت بالا و استایلهای مختلف به انیمه تبدیل کنید."
|
| 91 |
+
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
import uvicorn
|