Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,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 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
|
27 |
-
|
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 |
-
#
|
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
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
54 |
except subprocess.CalledProcessError as e:
|
55 |
-
|
56 |
except Exception as e:
|
57 |
-
|
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 |
-
#
|
69 |
-
|
70 |
-
iface.launch(server_name="0.0.0.0", server_port=port)
|
|
|
|
|
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 |
+
@app.route('/animate', methods=['POST'])
|
16 |
+
def animate():
|
17 |
+
if 'image' not in request.files:
|
18 |
+
return jsonify({"error": "No image file provided."}), 400
|
19 |
+
|
20 |
+
image_file = request.files['image']
|
21 |
+
if image_file.filename == '':
|
22 |
+
return jsonify({"error": "Empty filename."}), 400
|
23 |
+
|
24 |
+
# Save uploaded image
|
25 |
+
unique_id = str(uuid.uuid4())
|
26 |
+
input_path = os.path.join(UPLOAD_FOLDER, unique_id + os.path.splitext(image_file.filename)[1])
|
27 |
+
image_file.save(input_path)
|
28 |
|
29 |
+
# Prepare output directory
|
30 |
+
char_anno_dir = os.path.join(OUTPUT_FOLDER, f"{unique_id}_out")
|
|
|
|
|
|
|
|
|
31 |
os.makedirs(char_anno_dir, exist_ok=True)
|
32 |
+
|
33 |
try:
|
34 |
+
# Run the image_to_animation.py script
|
|
|
|
|
|
|
|
|
|
|
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 not os.path.exists(gif_path):
|
44 |
+
return jsonify({"error": "Animation not generated. Ensure input image is a clear humanoid drawing."}), 500
|
45 |
+
|
46 |
+
# Return the GIF as a response
|
47 |
+
# After returning, we can clean up if desired
|
48 |
+
return send_file(gif_path, mimetype="image/gif")
|
49 |
+
|
50 |
except subprocess.CalledProcessError as e:
|
51 |
+
return jsonify({"error": "Error during processing", "details": str(e)}), 500
|
52 |
except Exception as e:
|
53 |
+
return jsonify({"error": "Unexpected error", "details": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
+
# Run the Flask app
|
57 |
+
app.run(host="0.0.0.0", port=7860, debug=False)
|
|