Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,18 @@
|
|
| 1 |
-
import
|
| 2 |
-
from telegram import Update
|
| 3 |
-
from telegram.ext import (
|
| 4 |
-
Application,
|
| 5 |
-
CommandHandler,
|
| 6 |
-
MessageHandler,
|
| 7 |
-
filters,
|
| 8 |
-
ContextTypes,
|
| 9 |
-
)
|
| 10 |
-
import torch
|
| 11 |
from PIL import Image
|
| 12 |
-
import
|
| 13 |
from io import BytesIO
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
logging.basicConfig(
|
| 17 |
-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 18 |
-
)
|
| 19 |
-
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# بارگذاری مدلها
|
| 25 |
def load_model(style_name):
|
| 26 |
model = torch.hub.load(
|
| 27 |
"bryandlee/animegan2-pytorch:main",
|
|
@@ -32,85 +23,36 @@ def load_model(style_name):
|
|
| 32 |
return model
|
| 33 |
|
| 34 |
# پردازش تصویر
|
| 35 |
-
def animegan2_transform(
|
| 36 |
-
if isinstance(input_img, str):
|
| 37 |
-
input_img = Image.open(BytesIO(requests.get(input_img).content)).convert("RGB")
|
| 38 |
-
elif isinstance(input_img, Image.Image):
|
| 39 |
-
input_img = input_img.convert("RGB")
|
| 40 |
-
else:
|
| 41 |
-
raise ValueError("فرمت تصویر ورودی صحیح نیست!")
|
| 42 |
-
|
| 43 |
-
input_img = input_img.resize((460, 460))
|
| 44 |
-
model = load_model(style_name)
|
| 45 |
-
face2paint_func = torch.hub.load(
|
| 46 |
-
"bryandlee/animegan2-pytorch:main",
|
| 47 |
-
"face2paint",
|
| 48 |
-
size=1024,
|
| 49 |
-
verbose=False
|
| 50 |
-
)
|
| 51 |
-
output_img = face2paint_func(model, input_img)
|
| 52 |
-
return output_img
|
| 53 |
-
|
| 54 |
-
# دستور شروع ربات
|
| 55 |
-
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 56 |
-
await update.message.reply_text(
|
| 57 |
-
"سلام! من ربات تبدیل عکس به انیمه هستم. یه عکس بفرست تا به استایل انیمه تبدیلش کنم!\n"
|
| 58 |
-
"برای انتخاب استایل، یکی از اینها رو بنویس:\n"
|
| 59 |
-
"- face_paint_512_v1\n- face_paint_512_v2\n- paprika\n- celeba_distill"
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# مدیریت دریافت عکس
|
| 63 |
-
async def handle_photo(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 64 |
-
style = context.user_data.get("style", "face_paint_512_v2") # استایل پیشفرض
|
| 65 |
-
await update.message.reply_text("در حال پردازش تصویر... کمی صبر کن!")
|
| 66 |
-
|
| 67 |
-
# دریافت عکس از پیام
|
| 68 |
-
photo_file = await update.message.photo[-1].get_file()
|
| 69 |
-
photo_bytes = await photo_file.download_as_bytearray()
|
| 70 |
-
input_img = Image.open(BytesIO(photo_bytes)).convert("RGB")
|
| 71 |
-
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
output_buffer = BytesIO()
|
| 78 |
output_img.save(output_buffer, format="PNG")
|
| 79 |
output_buffer.seek(0)
|
| 80 |
-
|
| 81 |
-
# ارسال تصویر به کاربر
|
| 82 |
-
await update.message.reply_photo(photo=output_buffer, caption="تصویر انیمه شما!")
|
| 83 |
except Exception as e:
|
| 84 |
-
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
valid_styles = ["face_paint_512_v1", "face_paint_512_v2", "paprika", "celeba_distill"]
|
| 90 |
-
if style in valid_styles:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# مدیریت خطاها
|
| 97 |
-
async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 98 |
-
logger.error(f"Update {update} caused error {context.error}")
|
| 99 |
-
await update.message.reply_text("یه خطایی پیش اومد. دوباره امتحان کن!")
|
| 100 |
-
|
| 101 |
-
# تابع اصلی
|
| 102 |
-
def main():
|
| 103 |
-
# ساخت اپلیکیشن ربات
|
| 104 |
-
application = Application.builder().token(TOKEN).build()
|
| 105 |
-
|
| 106 |
-
# ثبت دستورات
|
| 107 |
-
application.add_handler(CommandHandler("start", start))
|
| 108 |
-
application.add_handler(MessageHandler(filters.PHOTO, handle_photo))
|
| 109 |
-
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, set_style))
|
| 110 |
-
application.add_error_handler(error_handler)
|
| 111 |
-
|
| 112 |
-
# شروع ربات
|
| 113 |
-
application.run_polling()
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
from io import BytesIO
|
| 5 |
+
import requests
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
|
| 8 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# مدل درخواست ورودی
|
| 11 |
+
class ImageRequest(BaseModel):
|
| 12 |
+
image_url: str
|
| 13 |
+
style: str = "face_paint_512_v2"
|
| 14 |
|
| 15 |
+
# بارگذاری مدلها
|
| 16 |
def load_model(style_name):
|
| 17 |
model = torch.hub.load(
|
| 18 |
"bryandlee/animegan2-pytorch:main",
|
|
|
|
| 23 |
return model
|
| 24 |
|
| 25 |
# پردازش تصویر
|
| 26 |
+
def animegan2_transform(image_url, style_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
+
input_img = Image.open(BytesIO(requests.get(image_url).content)).convert("RGB")
|
| 29 |
+
input_img = input_img.resize((460, 460))
|
| 30 |
+
model = load_model(style_name)
|
| 31 |
+
face2paint_func = torch.hub.load(
|
| 32 |
+
"bryandlee/animegan2-pytorch:main",
|
| 33 |
+
"face2paint",
|
| 34 |
+
size=1024,
|
| 35 |
+
verbose=False
|
| 36 |
+
)
|
| 37 |
+
output_img = face2paint_func(model, input_img)
|
| 38 |
output_buffer = BytesIO()
|
| 39 |
output_img.save(output_buffer, format="PNG")
|
| 40 |
output_buffer.seek(0)
|
| 41 |
+
return output_buffer
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 44 |
|
| 45 |
+
# مسیر API برای پردازش تصویر
|
| 46 |
+
@app.post("/process_image")
|
| 47 |
+
async def process_image(request: ImageRequest):
|
| 48 |
valid_styles = ["face_paint_512_v1", "face_paint_512_v2", "paprika", "celeba_distill"]
|
| 49 |
+
if request.style not in valid_styles:
|
| 50 |
+
raise HTTPException(status_code=400, detail="استایل نامعتبر!")
|
| 51 |
+
|
| 52 |
+
output_buffer = animegan2_transform(request.image_url, request.style)
|
| 53 |
+
return {"image": output_buffer.getvalue(), "content_type": "image/png"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# برای تست ساده
|
| 56 |
+
@app.get("/")
|
| 57 |
+
async def root():
|
| 58 |
+
return {"message": "API برای تبدیل تصویر به انیمه"}
|