Update app.py
Browse files
app.py
CHANGED
|
@@ -12,11 +12,19 @@ from google.cloud import storage
|
|
| 12 |
from google.oauth2 import service_account
|
| 13 |
import json
|
| 14 |
import shutil
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
API_URL = f"http://{API_HOST}:{API_PORT}/faceswap"
|
| 19 |
|
|
|
|
|
|
|
| 20 |
def setup_gcs_auth():
|
| 21 |
"""Set up GCS authentication using service account from environment variable"""
|
| 22 |
service_account_json = os.environ.get("SERVICE_ACCOUNT")
|
|
@@ -26,7 +34,7 @@ def setup_gcs_auth():
|
|
| 26 |
credentials = service_account.Credentials.from_service_account_info(service_account_info)
|
| 27 |
return credentials
|
| 28 |
except Exception as e:
|
| 29 |
-
|
| 30 |
return None
|
| 31 |
return None
|
| 32 |
|
|
@@ -54,7 +62,7 @@ def download_from_gcs(gcs_url):
|
|
| 54 |
urllib.request.urlretrieve(gcs_url, temp_file.name)
|
| 55 |
return temp_file.name
|
| 56 |
except Exception as e:
|
| 57 |
-
|
| 58 |
return None
|
| 59 |
|
| 60 |
def faceswap_process(gif_file, face_file):
|
|
@@ -66,36 +74,37 @@ def faceswap_process(gif_file, face_file):
|
|
| 66 |
temp_face = tempfile.NamedTemporaryFile(suffix='.jpg', delete=False)
|
| 67 |
|
| 68 |
try:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
with open(temp_gif.name, 'wb') as f:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
f.write(open(gif_file, 'rb').read())
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
with open(temp_face.name, 'wb') as f:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
f.write(open(face_file, 'rb').read())
|
| 99 |
|
| 100 |
files = {
|
| 101 |
'gif_file': ('input.gif', open(temp_gif.name, 'rb'), 'image/gif'),
|
|
@@ -142,6 +151,7 @@ def faceswap_process(gif_file, face_file):
|
|
| 142 |
except:
|
| 143 |
pass
|
| 144 |
|
|
|
|
| 145 |
return None, f"❌ Error: {str(e)}"
|
| 146 |
|
| 147 |
# Custom CSS
|
|
@@ -197,8 +207,13 @@ with gr.Blocks(title="Easel x GifSwap", css=custom_css) as demo:
|
|
| 197 |
|
| 198 |
with gr.Row():
|
| 199 |
with gr.Column():
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
submit_btn = gr.Button("Swap Face", variant="primary")
|
| 203 |
|
| 204 |
with gr.Column(elem_classes="output-container"):
|
|
@@ -281,4 +296,4 @@ with gr.Blocks(title="Easel x GifSwap", css=custom_css) as demo:
|
|
| 281 |
|
| 282 |
# Launch the app
|
| 283 |
if __name__ == "__main__":
|
| 284 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 12 |
from google.oauth2 import service_account
|
| 13 |
import json
|
| 14 |
import shutil
|
| 15 |
+
import logging
|
| 16 |
|
| 17 |
+
# Set up logging
|
| 18 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
# Get API endpoint from environment variables with fallback
|
| 22 |
+
API_HOST = os.environ.get("FACESWAP_API_HOST", "162.243.118.138")
|
| 23 |
+
API_PORT = os.environ.get("FACESWAP_API_PORT", "8080")
|
| 24 |
API_URL = f"http://{API_HOST}:{API_PORT}/faceswap"
|
| 25 |
|
| 26 |
+
logger.info(f"Using API endpoint: {API_URL}")
|
| 27 |
+
|
| 28 |
def setup_gcs_auth():
|
| 29 |
"""Set up GCS authentication using service account from environment variable"""
|
| 30 |
service_account_json = os.environ.get("SERVICE_ACCOUNT")
|
|
|
|
| 34 |
credentials = service_account.Credentials.from_service_account_info(service_account_info)
|
| 35 |
return credentials
|
| 36 |
except Exception as e:
|
| 37 |
+
logger.error(f"Error setting up GCS auth: {e}")
|
| 38 |
return None
|
| 39 |
return None
|
| 40 |
|
|
|
|
| 62 |
urllib.request.urlretrieve(gcs_url, temp_file.name)
|
| 63 |
return temp_file.name
|
| 64 |
except Exception as e:
|
| 65 |
+
logger.error(f"Error downloading from GCS: {e}")
|
| 66 |
return None
|
| 67 |
|
| 68 |
def faceswap_process(gif_file, face_file):
|
|
|
|
| 74 |
temp_face = tempfile.NamedTemporaryFile(suffix='.jpg', delete=False)
|
| 75 |
|
| 76 |
try:
|
| 77 |
+
# Handle gif_file - could be a PIL Image (from gr.Image) or a filepath
|
| 78 |
+
if isinstance(gif_file, str):
|
| 79 |
+
# It's a filepath
|
| 80 |
+
shutil.copy(gif_file, temp_gif.name)
|
| 81 |
+
elif hasattr(gif_file, 'save'):
|
| 82 |
+
# It's a PIL Image
|
| 83 |
+
gif_file.save(temp_gif.name, 'GIF')
|
| 84 |
+
elif hasattr(gif_file, 'read'):
|
| 85 |
+
# It's a file-like object
|
| 86 |
with open(temp_gif.name, 'wb') as f:
|
| 87 |
+
f.write(gif_file.read())
|
| 88 |
+
else:
|
| 89 |
+
# Try as numpy array
|
| 90 |
+
from PIL import Image
|
| 91 |
+
Image.fromarray(gif_file).save(temp_gif.name, 'GIF')
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# Handle face_file - could be a PIL Image (from gr.Image) or a filepath
|
| 94 |
+
if isinstance(face_file, str):
|
| 95 |
+
# It's a filepath
|
| 96 |
+
shutil.copy(face_file, temp_face.name)
|
| 97 |
+
elif hasattr(face_file, 'save'):
|
| 98 |
+
# It's a PIL Image
|
| 99 |
+
face_file.save(temp_face.name, 'JPEG')
|
| 100 |
+
elif hasattr(face_file, 'read'):
|
| 101 |
+
# It's a file-like object
|
| 102 |
with open(temp_face.name, 'wb') as f:
|
| 103 |
+
f.write(face_file.read())
|
| 104 |
+
else:
|
| 105 |
+
# Try as numpy array
|
| 106 |
+
from PIL import Image
|
| 107 |
+
Image.fromarray(face_file).save(temp_face.name, 'JPEG')
|
|
|
|
| 108 |
|
| 109 |
files = {
|
| 110 |
'gif_file': ('input.gif', open(temp_gif.name, 'rb'), 'image/gif'),
|
|
|
|
| 151 |
except:
|
| 152 |
pass
|
| 153 |
|
| 154 |
+
logger.exception("Error in faceswap_process")
|
| 155 |
return None, f"❌ Error: {str(e)}"
|
| 156 |
|
| 157 |
# Custom CSS
|
|
|
|
| 207 |
|
| 208 |
with gr.Row():
|
| 209 |
with gr.Column():
|
| 210 |
+
# Use gr.Image instead of gr.File to show thumbnails
|
| 211 |
+
gif_input = gr.Image(label="Upload GIF", type="filepath",
|
| 212 |
+
image_mode="RGB", sources=["upload"],
|
| 213 |
+
elem_id="gif_input")
|
| 214 |
+
face_input = gr.Image(label="Upload Face Image", type="filepath",
|
| 215 |
+
image_mode="RGB", sources=["upload"],
|
| 216 |
+
elem_id="face_input")
|
| 217 |
submit_btn = gr.Button("Swap Face", variant="primary")
|
| 218 |
|
| 219 |
with gr.Column(elem_classes="output-container"):
|
|
|
|
| 296 |
|
| 297 |
# Launch the app
|
| 298 |
if __name__ == "__main__":
|
| 299 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|