Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
import subprocess
|
|
|
|
|
|
|
4 |
|
5 |
# Define directories for uploads and outputs
|
6 |
UPLOAD_FOLDER = 'uploads_gradio'
|
@@ -10,13 +12,28 @@ OUTPUT_FOLDER = 'outputs_gradio'
|
|
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 |
-
|
16 |
-
|
17 |
Args:
|
18 |
file_path (str): Path to the uploaded file.
|
19 |
-
|
20 |
Returns:
|
21 |
str: Path to the generated GIF.
|
22 |
"""
|
@@ -26,45 +43,86 @@ def animate_image(file_path):
|
|
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 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
if __name__ ==
|
68 |
-
# Use the PORT environment variable provided by Hugging Face Spaces
|
69 |
port = int(os.getenv("PORT", "7860"))
|
70 |
-
|
|
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
+
from flask import Flask, request, send_file, jsonify
|
4 |
+
from werkzeug.utils import secure_filename
|
5 |
+
from flask_cors import CORS # Import CORS for handling cross-origin requests
|
6 |
|
7 |
# Define directories for uploads and outputs
|
8 |
UPLOAD_FOLDER = 'uploads_gradio'
|
|
|
12 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
13 |
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
14 |
|
15 |
+
# Initialize Flask app
|
16 |
+
app = Flask(__name__)
|
17 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
18 |
+
|
19 |
+
# Enable CORS for all routes (optional, adjust origins as needed)
|
20 |
+
CORS(app)
|
21 |
+
|
22 |
+
# Allowed file extensions
|
23 |
+
ALLOWED_EXTENSIONS = {'.png', '.jpg', '.jpeg', '.bmp'}
|
24 |
+
|
25 |
+
def allowed_file(filename):
|
26 |
+
"""Check if the file has an allowed extension."""
|
27 |
+
_, ext = os.path.splitext(filename)
|
28 |
+
return ext.lower() in ALLOWED_EXTENSIONS
|
29 |
+
|
30 |
def animate_image(file_path):
|
31 |
"""
|
32 |
+
Process the uploaded image and generate an animated GIF.
|
33 |
+
|
34 |
Args:
|
35 |
file_path (str): Path to the uploaded file.
|
36 |
+
|
37 |
Returns:
|
38 |
str: Path to the generated GIF.
|
39 |
"""
|
|
|
43 |
input_path = file_path
|
44 |
filename = os.path.basename(input_path)
|
45 |
base, ext = os.path.splitext(filename)
|
46 |
+
|
47 |
# Define the annotation directory for this specific image
|
48 |
char_anno_dir = os.path.join(OUTPUT_FOLDER, f"{base}_out")
|
49 |
os.makedirs(char_anno_dir, exist_ok=True)
|
50 |
+
|
51 |
+
# Validate file extension
|
52 |
+
if ext.lower() not in ALLOWED_EXTENSIONS:
|
53 |
+
raise ValueError("Unsupported file type. Please upload an image file (png, jpg, jpeg, bmp).")
|
54 |
+
|
55 |
try:
|
|
|
|
|
|
|
|
|
|
|
56 |
# Run the image_to_animation.py script with required arguments
|
57 |
subprocess.run([
|
58 |
'python', 'examples/image_to_animation.py',
|
59 |
input_path, char_anno_dir
|
60 |
], check=True)
|
61 |
+
|
62 |
# Path to the generated GIF
|
63 |
gif_path = os.path.join(char_anno_dir, "video.gif")
|
64 |
+
|
65 |
if os.path.exists(gif_path):
|
66 |
return gif_path
|
67 |
else:
|
68 |
raise FileNotFoundError("Animation failed to generate. Please ensure the input image contains clear humanoid drawings.")
|
69 |
+
|
70 |
except subprocess.CalledProcessError as e:
|
71 |
raise RuntimeError(f"Error during processing: {e}")
|
72 |
except Exception as e:
|
73 |
raise RuntimeError(f"Unexpected error: {e}")
|
74 |
|
75 |
+
@app.route('/animate', methods=['POST'])
|
76 |
+
def animate():
|
77 |
+
"""
|
78 |
+
Endpoint to receive an image and return the animated GIF.
|
79 |
+
"""
|
80 |
+
if 'file' not in request.files:
|
81 |
+
return jsonify({"error": "No file part in the request."}), 400
|
82 |
+
|
83 |
+
file = request.files['file']
|
84 |
+
|
85 |
+
if file.filename == '':
|
86 |
+
return jsonify({"error": "No file selected for uploading."}), 400
|
87 |
+
|
88 |
+
if file and allowed_file(file.filename):
|
89 |
+
filename = secure_filename(file.filename)
|
90 |
+
input_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
91 |
+
file.save(input_path)
|
92 |
+
|
93 |
+
try:
|
94 |
+
gif_path = animate_image(input_path)
|
95 |
+
return send_file(
|
96 |
+
gif_path,
|
97 |
+
mimetype='image/gif',
|
98 |
+
as_attachment=True,
|
99 |
+
attachment_filename=f"{os.path.splitext(filename)[0]}.gif"
|
100 |
+
)
|
101 |
+
except Exception as e:
|
102 |
+
return jsonify({"error": str(e)}), 500
|
103 |
+
else:
|
104 |
+
return jsonify({"error": "Allowed file types are png, jpg, jpeg, bmp."}), 400
|
105 |
+
|
106 |
+
@app.route('/', methods=['GET'])
|
107 |
+
def index():
|
108 |
+
"""
|
109 |
+
Root endpoint to provide basic information.
|
110 |
+
"""
|
111 |
+
return jsonify({
|
112 |
+
"message": "Animated Drawings API",
|
113 |
+
"endpoints": {
|
114 |
+
"/animate": "POST an image to receive an animated GIF."
|
115 |
+
}
|
116 |
+
})
|
117 |
+
|
118 |
+
@app.route('/health', methods=['GET'])
|
119 |
+
def health():
|
120 |
+
"""
|
121 |
+
Health check endpoint.
|
122 |
+
"""
|
123 |
+
return jsonify({"status": "healthy"}), 200
|
124 |
|
125 |
+
if __name__ == '__main__':
|
126 |
+
# Use the PORT environment variable provided by Hugging Face Spaces or default to 7860
|
127 |
port = int(os.getenv("PORT", "7860"))
|
128 |
+
app.run(host='0.0.0.0', port=port)
|