Fooocus_app / app.py
uruguayai's picture
Update app.py
0fa0e0e verified
raw
history blame
2.66 kB
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline
from PIL import Image
import numpy as np
import cv2
# Device setup
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load Stable Diffusion model
model_name = "CompVis/stable-diffusion-v1-4"
def load_image_model():
return StableDiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float16).to(device)
image_model = load_image_model()
def generate_image(prompt):
with torch.no_grad():
image = image_model(prompt).images[0]
return image
# Face swap function placeholder (replace with actual implementation)
def swap_faces(image1, image2):
# Placeholder implementation, you should replace this with your actual face swapping code
return image1
def face_swap(image1, image2):
image1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2BGR)
image2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2BGR)
swapped_image = swap_faces(image1, image2)
return cv2.cvtColor(swapped_image, cv2.COLOR_BGR2RGB)
# Upscaling function
def upscale_image(image, scale_factor=2):
image = Image.fromarray(np.array(image))
width, height = image.size
new_size = (int(width * scale_factor), int(height * scale_factor))
upscaled_image = image.resize(new_size, Image.LANCZOS)
return upscaled_image
# Gradio interface function
def process_image(prompt, image1=None, image2=None, scale_factor=2):
# Generate image from prompt
generated_image = generate_image(prompt)
# Perform faceswap if two images are provided
swapped_image = None
if image1 and image2:
swapped_image = face_swap(image1, image2)
# Perform upscaling if scale factor is specified
upscaled_image = upscale_image(generated_image, scale_factor)
return {
"Generated Image": generated_image,
"Swapped Image": swapped_image,
"Upscaled Image": upscaled_image
}
# Gradio interface setup
iface = gr.Interface(
fn=process_image,
inputs=[
gr.Textbox(label="Enter your prompt"),
gr.Image(label="Image 1 (for faceswap)", type="pil", optional=True),
gr.Image(label="Image 2 (for faceswap)", type="pil", optional=True),
gr.Slider(label="Upscale Factor", minimum=1, maximum=4, step=1, default=2)
],
outputs=[
gr.Image(label="Generated Image"),
gr.Image(label="Swapped Image"),
gr.Image(label="Upscaled Image")
],
title="Fooocus Image Processing",
description="Generate images from prompts, swap faces, and upscale images."
)
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860)