Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,70 @@
|
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
-
from flask import Flask, request, send_file, jsonify
|
4 |
-
import uuid
|
5 |
-
|
6 |
-
app = Flask(__name__)
|
7 |
|
8 |
# Define directories for uploads and outputs
|
9 |
UPLOAD_FOLDER = 'uploads_gradio'
|
10 |
OUTPUT_FOLDER = 'outputs_gradio'
|
11 |
|
|
|
12 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
13 |
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
image_file.save(input_path)
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
os.makedirs(char_anno_dir, exist_ok=True)
|
32 |
-
|
33 |
try:
|
34 |
-
#
|
|
|
|
|
|
|
|
|
|
|
35 |
subprocess.run([
|
36 |
'python', 'examples/image_to_animation.py',
|
37 |
input_path, char_anno_dir
|
38 |
], check=True)
|
39 |
-
|
40 |
# Path to the generated GIF
|
41 |
gif_path = os.path.join(char_anno_dir, "video.gif")
|
42 |
-
|
43 |
-
if
|
44 |
-
return
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
return send_file(gif_path, mimetype="image/gif")
|
49 |
-
|
50 |
except subprocess.CalledProcessError as e:
|
51 |
-
|
52 |
except Exception as e:
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
-
#
|
57 |
-
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import os
|
3 |
import subprocess
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Define directories for uploads and outputs
|
6 |
UPLOAD_FOLDER = 'uploads_gradio'
|
7 |
OUTPUT_FOLDER = 'outputs_gradio'
|
8 |
|
9 |
+
# Create directories if they don't exist
|
10 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
11 |
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
12 |
|
13 |
+
def animate_image(file_path):
|
14 |
+
"""
|
15 |
+
Function to process the uploaded image and generate an animated GIF.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
file_path (str): Path to the uploaded file.
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
str: Path to the generated GIF.
|
22 |
+
"""
|
23 |
+
if not file_path:
|
24 |
+
raise ValueError("No file uploaded.")
|
|
|
25 |
|
26 |
+
input_path = file_path
|
27 |
+
filename = os.path.basename(input_path)
|
28 |
+
base, ext = os.path.splitext(filename)
|
29 |
+
|
30 |
+
# Define the annotation directory for this specific image
|
31 |
+
char_anno_dir = os.path.join(OUTPUT_FOLDER, f"{base}_out")
|
32 |
os.makedirs(char_anno_dir, exist_ok=True)
|
33 |
+
|
34 |
try:
|
35 |
+
# Validate file extension
|
36 |
+
allowed_extensions = ['.png', '.jpg', '.jpeg', '.bmp']
|
37 |
+
if ext.lower() not in allowed_extensions:
|
38 |
+
raise ValueError("Unsupported file type. Please upload an image file (png, jpg, jpeg, bmp).")
|
39 |
+
|
40 |
+
# Run the image_to_animation.py script with required arguments
|
41 |
subprocess.run([
|
42 |
'python', 'examples/image_to_animation.py',
|
43 |
input_path, char_anno_dir
|
44 |
], check=True)
|
45 |
+
|
46 |
# Path to the generated GIF
|
47 |
gif_path = os.path.join(char_anno_dir, "video.gif")
|
48 |
+
|
49 |
+
if os.path.exists(gif_path):
|
50 |
+
return gif_path
|
51 |
+
else:
|
52 |
+
raise FileNotFoundError("Animation failed to generate. Please ensure the input image contains clear humanoid drawings.")
|
53 |
+
|
|
|
|
|
54 |
except subprocess.CalledProcessError as e:
|
55 |
+
raise RuntimeError(f"Error during processing: {e}")
|
56 |
except Exception as e:
|
57 |
+
raise RuntimeError(f"Unexpected error: {e}")
|
58 |
+
|
59 |
+
iface = gr.Interface(
|
60 |
+
fn=animate_image,
|
61 |
+
inputs=gr.Image(label="Upload Drawing", type="filepath", sources=["upload", "webcam"]),
|
62 |
+
outputs=gr.Image(label="Animated GIF"),
|
63 |
+
title="Animated Drawings",
|
64 |
+
description="Upload your drawing or take a photo, and get an animated GIF."
|
65 |
+
)
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
+
# Use the PORT environment variable provided by Hugging Face Spaces
|
69 |
+
port = int(os.getenv("PORT", "7860"))
|
70 |
+
iface.launch(server_name="0.0.0.0", server_port=port)
|