Spaces:
Sleeping
Sleeping
Commit ·
79043a8
1
Parent(s): 5334fbf
Update cpu fall back
Browse files- app.py +28 -9
- example_client.py +23 -3
- local_test_result.png +0 -0
- test_app.py +0 -1
- test_local.py +45 -0
app.py
CHANGED
|
@@ -26,21 +26,40 @@ pipe = None
|
|
| 26 |
def load_model():
|
| 27 |
global pipe
|
| 28 |
if pipe is None:
|
| 29 |
-
# Use the pre-uploaded model from Hugging Face
|
| 30 |
model_id = "Uminosachi/realisticVisionV51_v51VAE-inpainting"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return pipe
|
| 39 |
|
| 40 |
@app.on_event("startup")
|
| 41 |
async def startup_event():
|
| 42 |
-
|
| 43 |
load_model()
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def image_to_base64(image: Image.Image) -> str:
|
| 46 |
buffered = io.BytesIO()
|
|
|
|
| 26 |
def load_model():
|
| 27 |
global pipe
|
| 28 |
if pipe is None:
|
|
|
|
| 29 |
model_id = "Uminosachi/realisticVisionV51_v51VAE-inpainting"
|
| 30 |
+
try:
|
| 31 |
+
# Try CUDA first
|
| 32 |
+
if torch.cuda.is_available():
|
| 33 |
+
device = "cuda"
|
| 34 |
+
dtype = torch.float16
|
| 35 |
+
else:
|
| 36 |
+
# Fallback to CPU
|
| 37 |
+
device = "cpu"
|
| 38 |
+
dtype = torch.float32
|
| 39 |
+
|
| 40 |
+
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 41 |
+
model_id,
|
| 42 |
+
torch_dtype=dtype,
|
| 43 |
+
safety_checker=None
|
| 44 |
+
).to(device)
|
| 45 |
+
|
| 46 |
+
if device == "cuda":
|
| 47 |
+
pipe.enable_attention_slicing(slice_size="max")
|
| 48 |
+
pipe.enable_sequential_cpu_offload()
|
| 49 |
+
|
| 50 |
+
print(f"Model loaded on {device}")
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"Error loading model: {str(e)}")
|
| 54 |
+
raise
|
| 55 |
return pipe
|
| 56 |
|
| 57 |
@app.on_event("startup")
|
| 58 |
async def startup_event():
|
| 59 |
+
try:
|
| 60 |
load_model()
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Startup error: {str(e)}")
|
| 63 |
|
| 64 |
def image_to_base64(image: Image.Image) -> str:
|
| 65 |
buffered = io.BytesIO()
|
example_client.py
CHANGED
|
@@ -4,8 +4,8 @@ from PIL import Image
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
def call_inpaint_api(image_path, mask_path, prompt):
|
| 7 |
-
# Update
|
| 8 |
-
url = "https://
|
| 9 |
|
| 10 |
files = {
|
| 11 |
'image': open(image_path, 'rb'),
|
|
@@ -26,4 +26,24 @@ def call_inpaint_api(image_path, mask_path, prompt):
|
|
| 26 |
return 'result.png'
|
| 27 |
else:
|
| 28 |
print(f"Error: {response.text}")
|
| 29 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
def call_inpaint_api(image_path, mask_path, prompt):
|
| 7 |
+
# Update with your actual space URL
|
| 8 |
+
url = "https://yaseengoldfinchpc-modeltest.hf.space/inpaint"
|
| 9 |
|
| 10 |
files = {
|
| 11 |
'image': open(image_path, 'rb'),
|
|
|
|
| 26 |
return 'result.png'
|
| 27 |
else:
|
| 28 |
print(f"Error: {response.text}")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
def main():
|
| 32 |
+
# First test health endpoint
|
| 33 |
+
print("Testing health endpoint...")
|
| 34 |
+
response = requests.get("https://yaseengoldfinchpc-modeltest.hf.space/health")
|
| 35 |
+
print("Health Status:", response.json())
|
| 36 |
+
|
| 37 |
+
# Test inpainting
|
| 38 |
+
print("\nTesting inpainting...")
|
| 39 |
+
# Replace these with your actual image paths
|
| 40 |
+
image_path = r"C:\Users\M. Y\Downloads\t2.png" # Replace with your image path
|
| 41 |
+
mask_path = "generated_mask_1.png" # Replace with your mask path
|
| 42 |
+
prompt = "add some flowers and a fountain"
|
| 43 |
+
|
| 44 |
+
result = call_inpaint_api(image_path, mask_path, prompt)
|
| 45 |
+
if result:
|
| 46 |
+
print(f"Success! Result saved as: {result}")
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
main()
|
local_test_result.png
ADDED
|
test_app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from fastapi.testclient import TestClient
|
| 2 |
from app import app
|
| 3 |
-
import pytest
|
| 4 |
|
| 5 |
client = TestClient(app)
|
| 6 |
|
|
|
|
| 1 |
from fastapi.testclient import TestClient
|
| 2 |
from app import app
|
|
|
|
| 3 |
|
| 4 |
client = TestClient(app)
|
| 5 |
|
test_local.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionInpaintPipeline
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
def test_local():
|
| 6 |
+
# Load model
|
| 7 |
+
model_id = "Uminosachi/realisticVisionV51_v51VAE-inpainting"
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 10 |
+
|
| 11 |
+
print(f"Using device: {device}")
|
| 12 |
+
|
| 13 |
+
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 14 |
+
model_id,
|
| 15 |
+
torch_dtype=dtype,
|
| 16 |
+
safety_checker=None
|
| 17 |
+
).to(device)
|
| 18 |
+
|
| 19 |
+
# Load test images
|
| 20 |
+
image_path = r"C:\Users\M. Y\Downloads\t2.png"
|
| 21 |
+
mask_path = "generated_mask_1.png"
|
| 22 |
+
|
| 23 |
+
image = Image.open(image_path)
|
| 24 |
+
mask_image = Image.open(mask_path)
|
| 25 |
+
|
| 26 |
+
# Resize to multiple of 8
|
| 27 |
+
width, height = (dim - dim % 8 for dim in image.size)
|
| 28 |
+
image = image.resize((width, height))
|
| 29 |
+
mask_image = mask_image.resize((width, height))
|
| 30 |
+
mask_image = mask_image.convert("L")
|
| 31 |
+
|
| 32 |
+
# Test inference
|
| 33 |
+
result = pipe(
|
| 34 |
+
prompt="add some flowers and a fountain",
|
| 35 |
+
image=image,
|
| 36 |
+
mask_image=mask_image,
|
| 37 |
+
num_inference_steps=20,
|
| 38 |
+
guidance_scale=7.5,
|
| 39 |
+
).images[0]
|
| 40 |
+
|
| 41 |
+
result.save("local_test_result.png")
|
| 42 |
+
print("Test complete! Check local_test_result.png")
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
test_local()
|