Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,61 @@
|
|
| 1 |
-
from
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
from io import BytesIO
|
| 5 |
-
import
|
| 6 |
-
import logging
|
| 7 |
|
| 8 |
-
|
| 9 |
-
logging.basicConfig(
|
| 10 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 11 |
-
level=logging.INFO,
|
| 12 |
-
handlers=[logging.StreamHandler(), logging.FileHandler("app.log")]
|
| 13 |
-
)
|
| 14 |
-
logger = logging.getLogger(__name__)
|
| 15 |
-
|
| 16 |
-
app = Flask(__name__)
|
| 17 |
|
|
|
|
| 18 |
def load_model(style_name):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
).eval()
|
| 27 |
-
return model
|
| 28 |
-
except Exception as e:
|
| 29 |
-
logger.error(f"Error loading model: {str(e)}")
|
| 30 |
-
raise
|
| 31 |
|
| 32 |
def animegan2_transform(input_img, style_name):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
size=128,
|
| 48 |
-
verbose=False
|
| 49 |
-
)
|
| 50 |
-
output_img = face2paint_func(model, input_img)
|
| 51 |
-
logger.info("Image processed successfully")
|
| 52 |
-
return output_img
|
| 53 |
-
except Exception as e:
|
| 54 |
-
logger.error(f"Error processing image: {str(e)}")
|
| 55 |
-
raise
|
| 56 |
|
| 57 |
-
@app.
|
| 58 |
-
def
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
image = Image.open(file.stream).convert("RGB")
|
| 66 |
-
elif "url" in request.form:
|
| 67 |
-
url = request.form["url"]
|
| 68 |
-
image = Image.open(BytesIO(requests.get(url).content)).convert("RGB")
|
| 69 |
-
else:
|
| 70 |
-
logger.error("No file or URL provided")
|
| 71 |
-
return jsonify({"error": "لطفاً تصویر یا URL ارائه دهید"}), 400
|
| 72 |
-
|
| 73 |
-
output_img = animegan2_transform(image, style)
|
| 74 |
-
output_buffer = BytesIO()
|
| 75 |
-
output_img.save(output_buffer, format="PNG")
|
| 76 |
-
output_buffer.seek(0)
|
| 77 |
-
logger.info("Returning processed image")
|
| 78 |
-
return send_file(output_buffer, mimetype="image/png")
|
| 79 |
-
except Exception as e:
|
| 80 |
-
logger.error(f"API error: {str(e)}")
|
| 81 |
-
return jsonify({"error": str(e)}), 500
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
from io import BytesIO
|
| 5 |
+
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# کدهای قبلی برای بارگذاری مدل و پردازش تصویر
|
| 10 |
def load_model(style_name):
|
| 11 |
+
model = torch.hub.load(
|
| 12 |
+
"bryandlee/animegan2-pytorch:main",
|
| 13 |
+
"generator",
|
| 14 |
+
pretrained=style_name,
|
| 15 |
+
verbose=False
|
| 16 |
+
).eval()
|
| 17 |
+
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def animegan2_transform(input_img, style_name):
|
| 20 |
+
if isinstance(input_img, str):
|
| 21 |
+
input_img = Image.open(BytesIO(requests.get(input_img).content)).convert("RGB")
|
| 22 |
+
elif isinstance(input_img, Image.Image):
|
| 23 |
+
input_img = input_img.convert("RGB")
|
| 24 |
+
input_img = input_img.resize((460, 460))
|
| 25 |
+
model = load_model(style_name)
|
| 26 |
+
face2paint_func = torch.hub.load(
|
| 27 |
+
"bryandlee/animegan2-pytorch:main",
|
| 28 |
+
"face2paint",
|
| 29 |
+
size=1024,
|
| 30 |
+
verbose=False
|
| 31 |
+
)
|
| 32 |
+
output_img = face2paint_func(model, input_img)
|
| 33 |
+
return output_img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
@app.post("/animegan")
|
| 36 |
+
async def process_image(file: UploadFile = File(...), style: str = Form(...)):
|
| 37 |
+
image = Image.open(BytesIO(await file.read())).convert("RGB")
|
| 38 |
+
output_img = animegan2_transform(image, style)
|
| 39 |
+
output_buffer = BytesIO()
|
| 40 |
+
output_img.save(output_buffer, format="PNG")
|
| 41 |
+
output_buffer.seek(0)
|
| 42 |
+
return {"image": output_buffer.getvalue()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# برای رابط Gradio (اختیاری)
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=animegan2_transform,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Image(type="pil", label="آپلود تصویر یا وارد کردن URL"),
|
| 49 |
+
gr.Dropdown(
|
| 50 |
+
["face_paint_512_v1", "face_paint_512_v2", "paprika", "celeba_distill"],
|
| 51 |
+
value="face_paint_512_v2",
|
| 52 |
+
label="انتخاب استایل"
|
| 53 |
+
)
|
| 54 |
+
],
|
| 55 |
+
outputs=gr.Image(type="pil", label="تصویر انیمه (کیفیت بالا)"),
|
| 56 |
+
title="AnimeGANv2 - تبدیل تصویر به انیمه با انتخاب استایل"
|
| 57 |
+
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
import uvicorn
|
| 61 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|