ygauravyy commited on
Commit
055a595
1 Parent(s): e717093

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -47
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
- def animate_image(file_path):
14
- """
15
- Function to process the uploaded image and generate an animated GIF.
16
-
17
- Args:
18
- file_path (str): Path to the uploaded file.
19
-
20
- Returns:
21
- str: Path to the generated GIF.
22
- """
23
- if not file_path:
24
- raise ValueError("No file uploaded.")
 
25
 
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
- 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
- # Use the PORT environment variable provided by Hugging Face Spaces
69
- port = int(os.getenv("PORT", "7860"))
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)