Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,18 @@ from fastapi.responses import StreamingResponse
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
from io import BytesIO
|
| 6 |
-
import
|
| 7 |
import logging
|
| 8 |
|
| 9 |
# تنظیم لاگگیری
|
| 10 |
-
logging.basicConfig(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
app = FastAPI()
|
|
@@ -40,18 +47,14 @@ def animegan2_transform(input_img, style_name):
|
|
| 40 |
raise ValueError("فرمت تصویر ورودی صحیح نیست!")
|
| 41 |
|
| 42 |
# تغییر رزولوشن
|
| 43 |
-
input_img = input_img.resize((
|
| 44 |
-
|
| 45 |
-
# بارگذاری مدل
|
| 46 |
model = load_model(style_name)
|
| 47 |
face2paint_func = torch.hub.load(
|
| 48 |
"bryandlee/animegan2-pytorch:main",
|
| 49 |
"face2paint",
|
| 50 |
-
size=
|
| 51 |
verbose=False
|
| 52 |
)
|
| 53 |
-
|
| 54 |
-
# اجرای مدل
|
| 55 |
output_img = face2paint_func(model, input_img)
|
| 56 |
logger.info("Image processed successfully")
|
| 57 |
return output_img
|
|
@@ -61,34 +64,31 @@ def animegan2_transform(input_img, style_name):
|
|
| 61 |
|
| 62 |
# Endpoint برای API
|
| 63 |
@app.post("/transform")
|
| 64 |
-
async def transform_image(file: UploadFile = File(
|
| 65 |
try:
|
| 66 |
-
logger.info(f"Received
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 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
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
from io import BytesIO
|
| 6 |
+
import requests
|
| 7 |
import logging
|
| 8 |
|
| 9 |
# تنظیم لاگگیری
|
| 10 |
+
logging.basicConfig(
|
| 11 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 12 |
+
level=logging.INFO,
|
| 13 |
+
handlers=[
|
| 14 |
+
logging.StreamHandler(), # نمایش لاگ در کنسول
|
| 15 |
+
logging.FileHandler("app.log") # ذخیره لاگ در فایل
|
| 16 |
+
]
|
| 17 |
+
)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
app = FastAPI()
|
|
|
|
| 47 |
raise ValueError("فرمت تصویر ورودی صحیح نیست!")
|
| 48 |
|
| 49 |
# تغییر رزولوشن
|
| 50 |
+
input_img = input_img.resize((256, 256)) # رزولوشن کمتر برای تست سریعتر
|
|
|
|
|
|
|
| 51 |
model = load_model(style_name)
|
| 52 |
face2paint_func = torch.hub.load(
|
| 53 |
"bryandlee/animegan2-pytorch:main",
|
| 54 |
"face2paint",
|
| 55 |
+
size=512, # رزولوشن کمتر برای تست
|
| 56 |
verbose=False
|
| 57 |
)
|
|
|
|
|
|
|
| 58 |
output_img = face2paint_func(model, input_img)
|
| 59 |
logger.info("Image processed successfully")
|
| 60 |
return output_img
|
|
|
|
| 64 |
|
| 65 |
# Endpoint برای API
|
| 66 |
@app.post("/transform")
|
| 67 |
+
async def transform_image(file: UploadFile = File(None), style: str = Form(default="face_paint_512_v2"), url: str = Form(None)):
|
| 68 |
try:
|
| 69 |
+
logger.info(f"Received request with style: {style}")
|
| 70 |
+
if file:
|
| 71 |
+
image = Image.open(file.file).convert("RGB")
|
| 72 |
+
elif url:
|
| 73 |
+
image = Image.open(BytesIO(requests.get(url).content)).convert("RGB")
|
| 74 |
+
else:
|
| 75 |
+
logger.error("No file or URL provided")
|
| 76 |
+
return {"error": "لطفاً تصویر یا URL ارائه دهید"}
|
| 77 |
+
|
| 78 |
output_img = animegan2_transform(image, style)
|
| 79 |
output_buffer = BytesIO()
|
| 80 |
output_img.save(output_buffer, format="PNG")
|
| 81 |
output_buffer.seek(0)
|
| 82 |
+
logger.info("Returning processed image")
|
| 83 |
return StreamingResponse(output_buffer, media_type="image/png")
|
| 84 |
except Exception as e:
|
| 85 |
logger.error(f"API error: {str(e)}")
|
| 86 |
return {"error": str(e)}
|
| 87 |
|
| 88 |
+
# سلامتسنجی سرور
|
| 89 |
+
@app.get("/health")
|
| 90 |
+
async def health_check():
|
| 91 |
+
return {"status": "healthy"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
import uvicorn
|