Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,80 @@
|
|
1 |
import gradio as gr
|
2 |
-
from diffusers import StableDiffusionPipeline
|
3 |
import torch
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
MODEL_NAME = "CompVis/stable-diffusion-v1-4" # Puedes cambiarlo a tu modelo subido en HF
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
return
|
13 |
|
14 |
-
|
15 |
-
model = load_model()
|
16 |
|
17 |
def generate_image(prompt):
|
18 |
-
# Generar imagen usando el modelo cargado
|
19 |
-
if not prompt:
|
20 |
-
return None
|
21 |
with torch.no_grad():
|
22 |
-
image =
|
23 |
return image
|
24 |
|
25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
iface = gr.Interface(
|
27 |
-
fn=
|
28 |
-
inputs=
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
)
|
33 |
|
34 |
if __name__ == "__main__":
|
35 |
-
# Lanzar la interfaz
|
36 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
import cv2
|
7 |
|
8 |
+
# Device setup
|
|
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
+
# Load Stable Diffusion model
|
12 |
+
model_name = "CompVis/stable-diffusion-v1-4"
|
13 |
+
def load_image_model():
|
14 |
+
return StableDiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float16).to(device)
|
15 |
|
16 |
+
image_model = load_image_model()
|
|
|
17 |
|
18 |
def generate_image(prompt):
|
|
|
|
|
|
|
19 |
with torch.no_grad():
|
20 |
+
image = image_model(prompt).images[0]
|
21 |
return image
|
22 |
|
23 |
+
# Face swap function placeholder (replace with actual implementation)
|
24 |
+
def swap_faces(image1, image2):
|
25 |
+
# Placeholder implementation, you should replace this with your actual face swapping code
|
26 |
+
return image1
|
27 |
+
|
28 |
+
def face_swap(image1, image2):
|
29 |
+
image1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2BGR)
|
30 |
+
image2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2BGR)
|
31 |
+
swapped_image = swap_faces(image1, image2)
|
32 |
+
return cv2.cvtColor(swapped_image, cv2.COLOR_BGR2RGB)
|
33 |
+
|
34 |
+
# Upscaling function
|
35 |
+
def upscale_image(image, scale_factor=2):
|
36 |
+
image = Image.fromarray(np.array(image))
|
37 |
+
width, height = image.size
|
38 |
+
new_size = (int(width * scale_factor), int(height * scale_factor))
|
39 |
+
upscaled_image = image.resize(new_size, Image.LANCZOS)
|
40 |
+
return upscaled_image
|
41 |
+
|
42 |
+
# Gradio interface function
|
43 |
+
def process_image(prompt, image1=None, image2=None, scale_factor=2):
|
44 |
+
# Generate image from prompt
|
45 |
+
generated_image = generate_image(prompt)
|
46 |
+
|
47 |
+
# Perform faceswap if two images are provided
|
48 |
+
swapped_image = None
|
49 |
+
if image1 and image2:
|
50 |
+
swapped_image = face_swap(image1, image2)
|
51 |
+
|
52 |
+
# Perform upscaling if scale factor is specified
|
53 |
+
upscaled_image = upscale_image(generated_image, scale_factor)
|
54 |
+
|
55 |
+
return {
|
56 |
+
"Generated Image": generated_image,
|
57 |
+
"Swapped Image": swapped_image,
|
58 |
+
"Upscaled Image": upscaled_image
|
59 |
+
}
|
60 |
+
|
61 |
+
# Gradio interface setup
|
62 |
iface = gr.Interface(
|
63 |
+
fn=process_image,
|
64 |
+
inputs=[
|
65 |
+
gr.Textbox(label="Enter your prompt"),
|
66 |
+
gr.Image(label="Image 1 (for faceswap)", type="pil", optional=True),
|
67 |
+
gr.Image(label="Image 2 (for faceswap)", type="pil", optional=True),
|
68 |
+
gr.Slider(label="Upscale Factor", minimum=1, maximum=4, step=1, default=2)
|
69 |
+
],
|
70 |
+
outputs=[
|
71 |
+
gr.Image(label="Generated Image"),
|
72 |
+
gr.Image(label="Swapped Image"),
|
73 |
+
gr.Image(label="Upscaled Image")
|
74 |
+
],
|
75 |
+
title="Fooocus Image Processing",
|
76 |
+
description="Generate images from prompts, swap faces, and upscale images."
|
77 |
)
|
78 |
|
79 |
if __name__ == "__main__":
|
|
|
80 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|