File size: 1,893 Bytes
d49f7bc
 
055a595
 
 
 
d49f7bc
e5e07c1
d49f7bc
 
 
 
 
 
055a595
 
 
 
 
 
 
 
 
 
 
 
 
e5e07c1
055a595
 
d49f7bc
055a595
e5e07c1
055a595
e5e07c1
 
 
 
055a595
e5e07c1
 
055a595
 
 
 
 
 
 
 
e5e07c1
055a595
e5e07c1
055a595
d49f7bc
 
055a595
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)