Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,96 +1,80 @@
|
|
| 1 |
import os
|
| 2 |
-
import warnings
|
| 3 |
-
# Menghindari error protobuf di beberapa environment
|
| 4 |
-
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
import torch
|
|
|
|
| 8 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 9 |
-
from diffusers import
|
| 10 |
from PIL import Image
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
-
MODEL_ID = "black-forest-labs/FLUX.1-schnell"
|
| 18 |
|
| 19 |
-
print("π
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
#
|
| 23 |
-
pipe =
|
| 24 |
-
MODEL_ID,
|
| 25 |
-
torch_dtype=torch.float32,
|
| 26 |
-
|
| 27 |
)
|
| 28 |
-
# Pindahkan ke CPU
|
| 29 |
pipe.to("cpu")
|
| 30 |
|
| 31 |
-
#
|
| 32 |
pipe.enable_attention_slicing()
|
| 33 |
|
| 34 |
-
print("β
Mamboro AI Engine
|
| 35 |
except Exception as e:
|
| 36 |
print(f"β Gagal memuat model: {e}")
|
| 37 |
|
| 38 |
-
# Fungsi pembantu untuk konversi gambar ke Base64
|
| 39 |
def image_to_base64(image):
|
| 40 |
buffered = io.BytesIO()
|
| 41 |
image.save(buffered, format="JPEG")
|
| 42 |
return f"data:image/jpeg;base64,{base64.b64encode(buffered.getvalue()).decode('utf-8')}"
|
| 43 |
|
| 44 |
-
# --- ENDPOINT 1: GENERATE (Text to Image) ---
|
| 45 |
@app.post("/generate")
|
| 46 |
async def generate(prompt: str = Form(...)):
|
| 47 |
-
if not prompt:
|
| 48 |
-
raise HTTPException(status_code=400, detail="Prompt tidak boleh kosong")
|
| 49 |
-
|
| 50 |
try:
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
result = pipe(
|
| 54 |
prompt=prompt,
|
| 55 |
-
num_inference_steps=
|
| 56 |
guidance_scale=0.0
|
| 57 |
).images[0]
|
| 58 |
-
|
| 59 |
-
return {"image": image_to_base64(result)}
|
| 60 |
except Exception as e:
|
| 61 |
raise HTTPException(status_code=500, detail=str(e))
|
| 62 |
|
| 63 |
-
# --- ENDPOINT 2: EDIT (Image to Image / Reference) ---
|
| 64 |
@app.post("/edit")
|
| 65 |
async def edit(
|
| 66 |
prompt: str = Form(...),
|
| 67 |
image_file: UploadFile = File(...),
|
| 68 |
-
strength: float = Form(0.
|
| 69 |
):
|
| 70 |
try:
|
| 71 |
-
# Membaca gambar referensi dari HP
|
| 72 |
init_content = await image_file.read()
|
| 73 |
init_image = Image.open(io.BytesIO(init_content)).convert("RGB")
|
| 74 |
-
|
| 75 |
-
# Perkecil ukuran gambar agar CPU tidak meledak (PENTING!)
|
| 76 |
-
init_image = init_image.resize((512, 512))
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
prompt=prompt,
|
| 81 |
image=init_image,
|
| 82 |
strength=strength,
|
| 83 |
-
num_inference_steps=
|
| 84 |
).images[0]
|
| 85 |
|
| 86 |
-
return {"image": image_to_base64(
|
| 87 |
except Exception as e:
|
| 88 |
raise HTTPException(status_code=500, detail=str(e))
|
| 89 |
|
| 90 |
@app.get("/")
|
| 91 |
-
def
|
| 92 |
-
return {
|
| 93 |
-
"status": "Online",
|
| 94 |
-
"engine": "Mamboro AI (Flux Schnell CPU)",
|
| 95 |
-
"device": "CPU"
|
| 96 |
-
}
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import io
|
| 3 |
import base64
|
| 4 |
import torch
|
| 5 |
+
import warnings
|
| 6 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 7 |
+
from diffusers import AutoPipelineForImage2Image
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
+
# Sembunyikan warning CUDA karena kita pakai CPU
|
| 11 |
+
warnings.filterwarnings("ignore")
|
| 12 |
+
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
|
| 13 |
+
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
+
# Menggunakan SDXL Turbo: Jauh lebih ringan dari Flux untuk CPU
|
| 17 |
+
MODEL_ID = "stabilityai/sdxl-turbo"
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
print("π Memuat Mamboro AI Engine (Mode Ringan untuk CPU)...")
|
| 20 |
|
| 21 |
try:
|
| 22 |
+
# Memuat model dengan optimasi RAM
|
| 23 |
+
pipe = AutoPipelineForImage2Image.from_pretrained(
|
| 24 |
+
MODEL_ID,
|
| 25 |
+
torch_dtype=torch.float32,
|
| 26 |
+
variant="fp16" # Menggunakan varian yang lebih kecil
|
| 27 |
)
|
|
|
|
| 28 |
pipe.to("cpu")
|
| 29 |
|
| 30 |
+
# Fitur hemat memori
|
| 31 |
pipe.enable_attention_slicing()
|
| 32 |
|
| 33 |
+
print("β
Mamboro AI Engine Online!")
|
| 34 |
except Exception as e:
|
| 35 |
print(f"β Gagal memuat model: {e}")
|
| 36 |
|
|
|
|
| 37 |
def image_to_base64(image):
|
| 38 |
buffered = io.BytesIO()
|
| 39 |
image.save(buffered, format="JPEG")
|
| 40 |
return f"data:image/jpeg;base64,{base64.b64encode(buffered.getvalue()).decode('utf-8')}"
|
| 41 |
|
|
|
|
| 42 |
@app.post("/generate")
|
| 43 |
async def generate(prompt: str = Form(...)):
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
+
# SDXL Turbo hanya butuh 1-2 langkah di CPU, sangat cepat!
|
| 46 |
+
image = pipe(
|
|
|
|
| 47 |
prompt=prompt,
|
| 48 |
+
num_inference_steps=2,
|
| 49 |
guidance_scale=0.0
|
| 50 |
).images[0]
|
| 51 |
+
return {"image": image_to_base64(image)}
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
raise HTTPException(status_code=500, detail=str(e))
|
| 54 |
|
|
|
|
| 55 |
@app.post("/edit")
|
| 56 |
async def edit(
|
| 57 |
prompt: str = Form(...),
|
| 58 |
image_file: UploadFile = File(...),
|
| 59 |
+
strength: float = Form(0.5)
|
| 60 |
):
|
| 61 |
try:
|
|
|
|
| 62 |
init_content = await image_file.read()
|
| 63 |
init_image = Image.open(io.BytesIO(init_content)).convert("RGB")
|
| 64 |
+
init_image = init_image.resize((512, 512)) # Ukuran aman untuk CPU
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# Edit gambar berdasarkan referensi
|
| 67 |
+
image = pipe(
|
| 68 |
prompt=prompt,
|
| 69 |
image=init_image,
|
| 70 |
strength=strength,
|
| 71 |
+
num_inference_steps=2
|
| 72 |
).images[0]
|
| 73 |
|
| 74 |
+
return {"image": image_to_base64(image)}
|
| 75 |
except Exception as e:
|
| 76 |
raise HTTPException(status_code=500, detail=str(e))
|
| 77 |
|
| 78 |
@app.get("/")
|
| 79 |
+
def home():
|
| 80 |
+
return {"status": "Mamboro AI Light-Mode is Ready", "model": "SDXL Turbo"}
|
|
|
|
|
|
|
|
|
|
|
|