Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -16,9 +16,12 @@ def load_image_model():
|
|
16 |
image_model = load_image_model()
|
17 |
|
18 |
def generate_image(prompt):
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
22 |
|
23 |
# Face swap function placeholder (replace with actual implementation)
|
24 |
def swap_faces(image1, image2):
|
@@ -26,37 +29,50 @@ def swap_faces(image1, image2):
|
|
26 |
return image1
|
27 |
|
28 |
def face_swap(image1, image2):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
# Upscaling function
|
37 |
def upscale_image(image, scale_factor=2):
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
43 |
|
44 |
# Gradio interface function
|
45 |
def process_image(prompt, image1=None, image2=None, scale_factor=2):
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Gradio interface setup
|
62 |
iface = gr.Interface(
|
|
|
16 |
image_model = load_image_model()
|
17 |
|
18 |
def generate_image(prompt):
|
19 |
+
try:
|
20 |
+
with torch.no_grad():
|
21 |
+
image = image_model(prompt).images[0]
|
22 |
+
return image
|
23 |
+
except Exception as e:
|
24 |
+
return f"Error generating image: {str(e)}"
|
25 |
|
26 |
# Face swap function placeholder (replace with actual implementation)
|
27 |
def swap_faces(image1, image2):
|
|
|
29 |
return image1
|
30 |
|
31 |
def face_swap(image1, image2):
|
32 |
+
try:
|
33 |
+
if image1 is None or image2 is None:
|
34 |
+
return None
|
35 |
+
image1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2BGR)
|
36 |
+
image2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2BGR)
|
37 |
+
swapped_image = swap_faces(image1, image2)
|
38 |
+
return cv2.cvtColor(swapped_image, cv2.COLOR_BGR2RGB)
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error during face swap: {str(e)}"
|
41 |
|
42 |
# Upscaling function
|
43 |
def upscale_image(image, scale_factor=2):
|
44 |
+
try:
|
45 |
+
image = Image.fromarray(np.array(image))
|
46 |
+
width, height = image.size
|
47 |
+
new_size = (int(width * scale_factor), int(height * scale_factor))
|
48 |
+
upscaled_image = image.resize(new_size, Image.LANCZOS)
|
49 |
+
return upscaled_image
|
50 |
+
except Exception as e:
|
51 |
+
return f"Error during upscaling: {str(e)}"
|
52 |
|
53 |
# Gradio interface function
|
54 |
def process_image(prompt, image1=None, image2=None, scale_factor=2):
|
55 |
+
try:
|
56 |
+
# Generate image from prompt
|
57 |
+
generated_image = generate_image(prompt)
|
58 |
+
|
59 |
+
# Perform faceswap if two images are provided
|
60 |
+
swapped_image = face_swap(image1, image2)
|
61 |
+
|
62 |
+
# Perform upscaling if scale factor is specified
|
63 |
+
upscaled_image = upscale_image(generated_image, scale_factor)
|
64 |
|
65 |
+
return {
|
66 |
+
"Generated Image": generated_image,
|
67 |
+
"Swapped Image": swapped_image,
|
68 |
+
"Upscaled Image": upscaled_image
|
69 |
+
}
|
70 |
+
except Exception as e:
|
71 |
+
return {
|
72 |
+
"Generated Image": f"Error in process_image function: {str(e)}",
|
73 |
+
"Swapped Image": None,
|
74 |
+
"Upscaled Image": None
|
75 |
+
}
|
76 |
|
77 |
# Gradio interface setup
|
78 |
iface = gr.Interface(
|