Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,13 @@
|
|
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
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Define directories for uploads and outputs
|
8 |
UPLOAD_FOLDER = 'uploads_gradio'
|
@@ -53,6 +58,8 @@ def animate_image(file_path):
|
|
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',
|
@@ -63,13 +70,16 @@ def animate_image(file_path):
|
|
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'])
|
@@ -77,30 +87,37 @@ 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 |
-
|
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'])
|
@@ -108,6 +125,7 @@ def index():
|
|
108 |
"""
|
109 |
Root endpoint to provide basic information.
|
110 |
"""
|
|
|
111 |
return jsonify({
|
112 |
"message": "Animated Drawings API",
|
113 |
"endpoints": {
|
@@ -120,9 +138,11 @@ 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)
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
+
import logging
|
4 |
from flask import Flask, request, send_file, jsonify
|
5 |
from werkzeug.utils import secure_filename
|
6 |
+
from flask_cors import CORS
|
7 |
+
|
8 |
+
# Configure logging
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
logger = logging.getLogger(__name__)
|
11 |
|
12 |
# Define directories for uploads and outputs
|
13 |
UPLOAD_FOLDER = 'uploads_gradio'
|
|
|
58 |
raise ValueError("Unsupported file type. Please upload an image file (png, jpg, jpeg, bmp).")
|
59 |
|
60 |
try:
|
61 |
+
logger.info(f"Starting animation for {input_path}")
|
62 |
+
|
63 |
# Run the image_to_animation.py script with required arguments
|
64 |
subprocess.run([
|
65 |
'python', 'examples/image_to_animation.py',
|
|
|
70 |
gif_path = os.path.join(char_anno_dir, "video.gif")
|
71 |
|
72 |
if os.path.exists(gif_path):
|
73 |
+
logger.info(f"Animation successful: {gif_path}")
|
74 |
return gif_path
|
75 |
else:
|
76 |
raise FileNotFoundError("Animation failed to generate. Please ensure the input image contains clear humanoid drawings.")
|
77 |
|
78 |
except subprocess.CalledProcessError as e:
|
79 |
+
logger.error(f"Error during processing: {e}")
|
80 |
raise RuntimeError(f"Error during processing: {e}")
|
81 |
except Exception as e:
|
82 |
+
logger.error(f"Unexpected error: {e}")
|
83 |
raise RuntimeError(f"Unexpected error: {e}")
|
84 |
|
85 |
@app.route('/animate', methods=['POST'])
|
|
|
87 |
"""
|
88 |
Endpoint to receive an image and return the animated GIF.
|
89 |
"""
|
90 |
+
logger.info("Received request to /animate")
|
91 |
if 'file' not in request.files:
|
92 |
+
logger.warning("No file part in the request.")
|
93 |
return jsonify({"error": "No file part in the request."}), 400
|
94 |
|
95 |
file = request.files['file']
|
96 |
|
97 |
if file.filename == '':
|
98 |
+
logger.warning("No file selected for uploading.")
|
99 |
return jsonify({"error": "No file selected for uploading."}), 400
|
100 |
|
101 |
if file and allowed_file(file.filename):
|
102 |
filename = secure_filename(file.filename)
|
103 |
input_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
104 |
file.save(input_path)
|
105 |
+
logger.info(f"File saved to {input_path}")
|
106 |
|
107 |
try:
|
108 |
gif_path = animate_image(input_path)
|
109 |
+
logger.info(f"Sending GIF: {gif_path}")
|
110 |
return send_file(
|
111 |
gif_path,
|
112 |
mimetype='image/gif',
|
113 |
as_attachment=True,
|
114 |
+
download_name=f"{os.path.splitext(filename)[0]}.gif"
|
115 |
)
|
116 |
except Exception as e:
|
117 |
+
logger.error(f"Error in /animate: {e}")
|
118 |
return jsonify({"error": str(e)}), 500
|
119 |
else:
|
120 |
+
logger.warning("Allowed file types are png, jpg, jpeg, bmp.")
|
121 |
return jsonify({"error": "Allowed file types are png, jpg, jpeg, bmp."}), 400
|
122 |
|
123 |
@app.route('/', methods=['GET'])
|
|
|
125 |
"""
|
126 |
Root endpoint to provide basic information.
|
127 |
"""
|
128 |
+
logger.info("Received request to /")
|
129 |
return jsonify({
|
130 |
"message": "Animated Drawings API",
|
131 |
"endpoints": {
|
|
|
138 |
"""
|
139 |
Health check endpoint.
|
140 |
"""
|
141 |
+
logger.info("Received request to /health")
|
142 |
return jsonify({"status": "healthy"}), 200
|
143 |
|
144 |
if __name__ == '__main__':
|
145 |
# Use the PORT environment variable provided by Hugging Face Spaces or default to 7860
|
146 |
port = int(os.getenv("PORT", "7860"))
|
147 |
+
logger.info(f"Starting Flask app on port {port}")
|
148 |
app.run(host='0.0.0.0', port=port)
|