nanee-animation / app.py
ygauravyy's picture
Update app.py
055a595 verified
raw
history blame
1.89 kB
import os
import subprocess
from flask import Flask, request, send_file, jsonify
import uuid
app = Flask(__name__)
# Define directories for uploads and outputs
UPLOAD_FOLDER = 'uploads_gradio'
OUTPUT_FOLDER = 'outputs_gradio'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
@app.route('/animate', methods=['POST'])
def animate():
if 'image' not in request.files:
return jsonify({"error": "No image file provided."}), 400
image_file = request.files['image']
if image_file.filename == '':
return jsonify({"error": "Empty filename."}), 400
# Save uploaded image
unique_id = str(uuid.uuid4())
input_path = os.path.join(UPLOAD_FOLDER, unique_id + os.path.splitext(image_file.filename)[1])
image_file.save(input_path)
# Prepare output directory
char_anno_dir = os.path.join(OUTPUT_FOLDER, f"{unique_id}_out")
os.makedirs(char_anno_dir, exist_ok=True)
try:
# Run the image_to_animation.py script
subprocess.run([
'python', 'examples/image_to_animation.py',
input_path, char_anno_dir
], check=True)
# Path to the generated GIF
gif_path = os.path.join(char_anno_dir, "video.gif")
if not os.path.exists(gif_path):
return jsonify({"error": "Animation not generated. Ensure input image is a clear humanoid drawing."}), 500
# Return the GIF as a response
# After returning, we can clean up if desired
return send_file(gif_path, mimetype="image/gif")
except subprocess.CalledProcessError as e:
return jsonify({"error": "Error during processing", "details": str(e)}), 500
except Exception as e:
return jsonify({"error": "Unexpected error", "details": str(e)}), 500
if __name__ == "__main__":
# Run the Flask app
app.run(host="0.0.0.0", port=7860, debug=False)