Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,71 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import subprocess
|
4 |
-
import requests
|
5 |
-
import time
|
6 |
|
|
|
7 |
UPLOAD_FOLDER = 'uploads_gradio'
|
8 |
OUTPUT_FOLDER = 'outputs_gradio'
|
9 |
|
|
|
10 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
11 |
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
12 |
|
13 |
-
# Start TorchServe in the background
|
14 |
-
# Adjust arguments to match your configuration
|
15 |
-
ts_process = subprocess.Popen([
|
16 |
-
"torchserve", "--start",
|
17 |
-
"--model-store", "/home/torchserve/model-store",
|
18 |
-
"--ts-config", "/home/torchserve/config.properties",
|
19 |
-
"--no-config-snapshots"
|
20 |
-
])
|
21 |
-
|
22 |
-
# Wait for TorchServe to become healthy
|
23 |
-
while True:
|
24 |
-
try:
|
25 |
-
r = requests.get("http://127.0.0.1:8080/ping")
|
26 |
-
if r.status_code == 200:
|
27 |
-
break
|
28 |
-
except:
|
29 |
-
pass
|
30 |
-
time.sleep(1)
|
31 |
-
|
32 |
def animate_image(file_path):
|
33 |
-
|
34 |
-
|
35 |
-
# Process the response and then run your animation logic.
|
36 |
-
# ...
|
37 |
-
# Or if you run your original script, just as before:
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
if not file_path:
|
40 |
raise ValueError("No file uploaded.")
|
|
|
|
|
41 |
input_path = file_path
|
42 |
filename = os.path.basename(input_path)
|
43 |
base, ext = os.path.splitext(filename)
|
44 |
-
|
45 |
-
|
46 |
-
raise ValueError("Unsupported file type.")
|
47 |
-
|
48 |
char_anno_dir = os.path.join(OUTPUT_FOLDER, f"{base}_out")
|
49 |
os.makedirs(char_anno_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
gif_path = os.path.join(char_anno_dir, "video.gif")
|
54 |
-
if os.path.exists(gif_path):
|
55 |
-
return gif_path
|
56 |
-
else:
|
57 |
-
raise FileNotFoundError("Animation failed to generate.")
|
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 or take a photo
|
65 |
)
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
-
|
69 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
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 |
+
# 'file_path' is a string path to the uploaded file
|
27 |
input_path = file_path
|
28 |
filename = os.path.basename(input_path)
|
29 |
base, ext = os.path.splitext(filename)
|
30 |
+
|
31 |
+
# Define the annotation directory for this specific image
|
|
|
|
|
32 |
char_anno_dir = os.path.join(OUTPUT_FOLDER, f"{base}_out")
|
33 |
os.makedirs(char_anno_dir, exist_ok=True)
|
34 |
+
|
35 |
+
try:
|
36 |
+
# Validate file extension
|
37 |
+
allowed_extensions = ['.png', '.jpg', '.jpeg', '.bmp']
|
38 |
+
if ext.lower() not in allowed_extensions:
|
39 |
+
raise ValueError("Unsupported file type. Please upload an image file (png, jpg, jpeg, bmp).")
|
40 |
+
|
41 |
+
# Run the image_to_animation.py script with required arguments
|
42 |
+
subprocess.run([
|
43 |
+
'python', 'examples/image_to_animation.py',
|
44 |
+
input_path, char_anno_dir
|
45 |
+
# Optionally, add motion_cfg_fn and retarget_cfg_fn here if needed
|
46 |
+
], check=True)
|
47 |
+
|
48 |
+
# Path to the generated GIF
|
49 |
+
gif_path = os.path.join(char_anno_dir, "video.gif")
|
50 |
+
|
51 |
+
if os.path.exists(gif_path):
|
52 |
+
return gif_path
|
53 |
+
else:
|
54 |
+
raise FileNotFoundError("Animation failed to generate. Please ensure the input image contains clear humanoid drawings.")
|
55 |
+
|
56 |
+
except subprocess.CalledProcessError as e:
|
57 |
+
raise RuntimeError(f"Error during processing: {e}")
|
58 |
+
except Exception as e:
|
59 |
+
raise RuntimeError(f"Unexpected error: {e}")
|
60 |
|
61 |
+
# Define the Gradio interface using the updated API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
iface = gr.Interface(
|
63 |
fn=animate_image,
|
64 |
inputs=gr.Image(label="Upload Drawing", type="filepath", sources=["upload", "webcam"]),
|
65 |
outputs=gr.Image(label="Animated GIF"),
|
66 |
title="Animated Drawings",
|
67 |
+
description="Upload your drawing or take a photo, and get an animated GIF."
|
68 |
)
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
+
iface.launch()
|
|