Spaces:
Sleeping
Sleeping
Create my.js
Browse files
my.js
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import React, { useState } from 'react';
|
2 |
+
import { Upload } from 'lucide-react';
|
3 |
+
|
4 |
+
const FaceSwapForm = () => {
|
5 |
+
const [sourceFile, setSourceFile] = useState(null);
|
6 |
+
const [targetFile, setTargetFile] = useState(null);
|
7 |
+
const [doEnhance, setDoEnhance] = useState(true);
|
8 |
+
const [sourcePreview, setSourcePreview] = useState('');
|
9 |
+
const [targetPreview, setTargetPreview] = useState('');
|
10 |
+
const [isLoading, setIsLoading] = useState(false);
|
11 |
+
|
12 |
+
const handleFileChange = (event, type) => {
|
13 |
+
const file = event.target.files[0];
|
14 |
+
if (file) {
|
15 |
+
const reader = new FileReader();
|
16 |
+
reader.onloadend = () => {
|
17 |
+
if (type === 'source') {
|
18 |
+
setSourceFile(file);
|
19 |
+
setSourcePreview(reader.result);
|
20 |
+
} else {
|
21 |
+
setTargetFile(file);
|
22 |
+
setTargetPreview(reader.result);
|
23 |
+
}
|
24 |
+
};
|
25 |
+
reader.readAsDataURL(file);
|
26 |
+
}
|
27 |
+
};
|
28 |
+
|
29 |
+
const handleSubmit = async (e) => {
|
30 |
+
e.preventDefault();
|
31 |
+
setIsLoading(true);
|
32 |
+
|
33 |
+
try {
|
34 |
+
// Here you would implement your actual API call
|
35 |
+
console.log('Processing:', { sourceFile, targetFile, doEnhance });
|
36 |
+
// Simulate API delay
|
37 |
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
38 |
+
} catch (error) {
|
39 |
+
console.error('Error:', error);
|
40 |
+
} finally {
|
41 |
+
setIsLoading(false);
|
42 |
+
}
|
43 |
+
};
|
44 |
+
|
45 |
+
return (
|
46 |
+
<div className="max-w-2xl mx-auto p-6 bg-white rounded-lg shadow-md">
|
47 |
+
<h2 className="text-2xl font-bold mb-6 text-center">Face Swap</h2>
|
48 |
+
|
49 |
+
<form onSubmit={handleSubmit} className="space-y-6">
|
50 |
+
<div className="grid grid-cols-2 gap-6">
|
51 |
+
{/* Source Image Upload */}
|
52 |
+
<div className="space-y-2">
|
53 |
+
<label className="block text-sm font-medium text-gray-700">
|
54 |
+
Source Image
|
55 |
+
</label>
|
56 |
+
<div className="relative">
|
57 |
+
{sourcePreview ? (
|
58 |
+
<div className="relative h-48 rounded-lg overflow-hidden">
|
59 |
+
<img
|
60 |
+
src={sourcePreview}
|
61 |
+
alt="Source preview"
|
62 |
+
className="w-full h-full object-cover"
|
63 |
+
/>
|
64 |
+
<button
|
65 |
+
type="button"
|
66 |
+
onClick={() => {
|
67 |
+
setSourceFile(null);
|
68 |
+
setSourcePreview('');
|
69 |
+
}}
|
70 |
+
className="absolute top-2 right-2 bg-red-500 text-white p-1 rounded-full"
|
71 |
+
>
|
72 |
+
×
|
73 |
+
</button>
|
74 |
+
</div>
|
75 |
+
) : (
|
76 |
+
<label className="flex flex-col items-center justify-center h-48 border-2 border-dashed border-gray-300 rounded-lg cursor-pointer hover:bg-gray-50">
|
77 |
+
<Upload className="w-8 h-8 text-gray-400" />
|
78 |
+
<span className="mt-2 text-sm text-gray-500">Upload source image</span>
|
79 |
+
<input
|
80 |
+
type="file"
|
81 |
+
className="hidden"
|
82 |
+
accept="image/*"
|
83 |
+
onChange={(e) => handleFileChange(e, 'source')}
|
84 |
+
/>
|
85 |
+
</label>
|
86 |
+
)}
|
87 |
+
</div>
|
88 |
+
</div>
|
89 |
+
|
90 |
+
{/* Target Image Upload */}
|
91 |
+
<div className="space-y-2">
|
92 |
+
<label className="block text-sm font-medium text-gray-700">
|
93 |
+
Target Image
|
94 |
+
</label>
|
95 |
+
<div className="relative">
|
96 |
+
{targetPreview ? (
|
97 |
+
<div className="relative h-48 rounded-lg overflow-hidden">
|
98 |
+
<img
|
99 |
+
src={targetPreview}
|
100 |
+
alt="Target preview"
|
101 |
+
className="w-full h-full object-cover"
|
102 |
+
/>
|
103 |
+
<button
|
104 |
+
type="button"
|
105 |
+
onClick={() => {
|
106 |
+
setTargetFile(null);
|
107 |
+
setTargetPreview('');
|
108 |
+
}}
|
109 |
+
className="absolute top-2 right-2 bg-red-500 text-white p-1 rounded-full"
|
110 |
+
>
|
111 |
+
×
|
112 |
+
</button>
|
113 |
+
</div>
|
114 |
+
) : (
|
115 |
+
<label className="flex flex-col items-center justify-center h-48 border-2 border-dashed border-gray-300 rounded-lg cursor-pointer hover:bg-gray-50">
|
116 |
+
<Upload className="w-8 h-8 text-gray-400" />
|
117 |
+
<span className="mt-2 text-sm text-gray-500">Upload target image</span>
|
118 |
+
<input
|
119 |
+
type="file"
|
120 |
+
className="hidden"
|
121 |
+
accept="image/*"
|
122 |
+
onChange={(e) => handleFileChange(e, 'target')}
|
123 |
+
/>
|
124 |
+
</label>
|
125 |
+
)}
|
126 |
+
</div>
|
127 |
+
</div>
|
128 |
+
</div>
|
129 |
+
|
130 |
+
{/* Enhancement Option */}
|
131 |
+
<div className="flex items-center">
|
132 |
+
<input
|
133 |
+
type="checkbox"
|
134 |
+
id="enhance"
|
135 |
+
checked={doEnhance}
|
136 |
+
onChange={(e) => setDoEnhance(e.target.checked)}
|
137 |
+
className="h-4 w-4 text-blue-600 rounded border-gray-300"
|
138 |
+
/>
|
139 |
+
<label htmlFor="enhance" className="ml-2 text-sm text-gray-700">
|
140 |
+
Enable face enhancement
|
141 |
+
</label>
|
142 |
+
</div>
|
143 |
+
|
144 |
+
{/* Submit Button */}
|
145 |
+
<button
|
146 |
+
type="submit"
|
147 |
+
disabled={!sourceFile || !targetFile || isLoading}
|
148 |
+
className={`w-full py-2 px-4 rounded-md text-white font-medium ${
|
149 |
+
!sourceFile || !targetFile || isLoading
|
150 |
+
? 'bg-gray-400 cursor-not-allowed'
|
151 |
+
: 'bg-blue-600 hover:bg-blue-700'
|
152 |
+
}`}
|
153 |
+
>
|
154 |
+
{isLoading ? 'Processing...' : 'Swap Faces'}
|
155 |
+
</button>
|
156 |
+
</form>
|
157 |
+
</div>
|
158 |
+
);
|
159 |
+
};
|
160 |
+
|
161 |
+
export default FaceSwapForm;
|