Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from diffusers import AutoPipelineForText2Image, DDIMScheduler
|
| 5 |
+
from transformers import CLIPVisionModelWithProjection
|
| 6 |
+
import numpy as np
|
| 7 |
+
import spaces # Ensure this is available in your environment
|
| 8 |
+
|
| 9 |
+
# Initialize a zero tensor for demonstration purposes
|
| 10 |
+
zero = torch.Tensor([0]).cuda()
|
| 11 |
+
print(zero.device) # Should output 'cuda:0' if a GPU is available
|
| 12 |
+
|
| 13 |
+
@spaces.GPU # Decorate the function to run on GPU
|
| 14 |
+
def transform_image(face_image):
|
| 15 |
+
print(zero.device) # Check the device inside the function, should be 'cuda:0'
|
| 16 |
+
|
| 17 |
+
generator = torch.Generator(device="cuda").manual_seed(0) # Use GPU device if available
|
| 18 |
+
|
| 19 |
+
# Process the input face image
|
| 20 |
+
if isinstance(face_image, Image.Image):
|
| 21 |
+
processed_face_image = face_image
|
| 22 |
+
elif isinstance(face_image, np.ndarray):
|
| 23 |
+
processed_face_image = Image.fromarray(face_image)
|
| 24 |
+
else:
|
| 25 |
+
raise ValueError("Unsupported image format")
|
| 26 |
+
|
| 27 |
+
# Load the style image from the local path
|
| 28 |
+
style_image_path = "/content/soyjak2.jpeg"
|
| 29 |
+
style_image = Image.open(style_image_path)
|
| 30 |
+
|
| 31 |
+
# Perform the transformation using the GPU
|
| 32 |
+
image = pipeline(
|
| 33 |
+
prompt="soyjak",
|
| 34 |
+
ip_adapter_image=[style_image, processed_face_image],
|
| 35 |
+
negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
|
| 36 |
+
num_inference_steps=30,
|
| 37 |
+
generator=generator,
|
| 38 |
+
).images[0]
|
| 39 |
+
|
| 40 |
+
return image
|
| 41 |
+
|
| 42 |
+
# Load models and configure pipeline with GPU support
|
| 43 |
+
pipeline = AutoPipelineForText2Image.from_pretrained(
|
| 44 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 45 |
+
torch_dtype=torch.float16, # Consider using torch.float32 for GPU computations
|
| 46 |
+
device="cuda", # Use GPU device if available
|
| 47 |
+
).to("cuda") # Ensure the model is moved to GPU
|
| 48 |
+
|
| 49 |
+
# Additional pipeline configurations
|
| 50 |
+
pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config).to("cuda")
|
| 51 |
+
pipeline.enable_model_cpu_offload(False) # Consider not offloading to CPU when using GPU
|
| 52 |
+
|
| 53 |
+
# Gradio interface setup
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
fn=transform_image,
|
| 56 |
+
inputs=gr.Image(label="Upload your face image"),
|
| 57 |
+
outputs=gr.Image(label="Your Soyjak"),
|
| 58 |
+
title="InstaSoyjak - turn anyone into a Soyjak",
|
| 59 |
+
description="All you need to do is upload an image. Please use responsibly. Please follow me on Twitter if you like this space: https://twitter.com/angrypenguinPNG. Idea from Yacine, please give him a follow: https://twitter.com/yacineMTB.",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
demo.launch()
|