Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import base64
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
from time import sleep
|
7 |
+
|
8 |
+
def file_to_base64(filepath):
|
9 |
+
with open(filepath, "rb") as file:
|
10 |
+
file_data = file.read()
|
11 |
+
base64_encoded_data = base64.b64encode(file_data)
|
12 |
+
base64_message = base64_encoded_data.decode('utf-8')
|
13 |
+
return base64_message
|
14 |
+
|
15 |
+
def submit_job(audio_file, preset, beat_sensitivity, fps):
|
16 |
+
api_key = os.getenv('DEFORUM_API_KEY')
|
17 |
+
if not api_key:
|
18 |
+
return "Error: API key is not set in environment variables", None
|
19 |
+
|
20 |
+
server = "https://deforum.studio"
|
21 |
+
api_url = f'{server}/api/public/v1/audiovis1'
|
22 |
+
headers = {
|
23 |
+
"Content-Type": "application/json",
|
24 |
+
"Authorization": f"Bearer {api_key}"
|
25 |
+
}
|
26 |
+
|
27 |
+
audio_base64 = file_to_base64(audio_file.name)
|
28 |
+
|
29 |
+
payload = {
|
30 |
+
"audioData": audio_base64,
|
31 |
+
"presetName": preset,
|
32 |
+
"beatSensitivity": beat_sensitivity,
|
33 |
+
"fps": fps
|
34 |
+
}
|
35 |
+
|
36 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
37 |
+
if response.status_code != 201:
|
38 |
+
return f"Error submitting job: {response.status_code} - {response.text}", None
|
39 |
+
|
40 |
+
data = response.json()
|
41 |
+
tracking_url = f"{server}{data['links']['audiovis1']}"
|
42 |
+
|
43 |
+
while True:
|
44 |
+
response = requests.get(tracking_url, headers=headers)
|
45 |
+
if response.status_code != 200:
|
46 |
+
return f"Error getting job status: {response.text}", None
|
47 |
+
|
48 |
+
tracking_data = response.json()
|
49 |
+
if tracking_data['status'] in ['canceled', 'failed', 'succeeded']:
|
50 |
+
break
|
51 |
+
sleep(10)
|
52 |
+
|
53 |
+
if tracking_data['status'] != 'succeeded':
|
54 |
+
return f"Job ended with status: {tracking_data['status']}", None
|
55 |
+
else:
|
56 |
+
return tracking_data['links']['outputUrls'][0]
|
57 |
+
|
58 |
+
description1 = """
|
59 |
+
# Audio Visualizer Playground
|
60 |
+
A way for you to easily create audio-synced animation masks for AnimateDiff or Deforum Animations.
|
61 |
+
"""
|
62 |
+
description2 = """
|
63 |
+
### Please provide feedback for us about which masks you like, or would like to see in our animation channel in our Discord: [discord.gg/deforum](https://discord.gg/deforum)
|
64 |
+
"""
|
65 |
+
|
66 |
+
custom_css = """
|
67 |
+
@import url('//fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap');
|
68 |
+
@import url('//fonts.googleapis.com/css?family=Aldrich&display=swap');
|
69 |
+
@import-normalize;
|
70 |
+
|
71 |
+
html, body {
|
72 |
+
height: 100%;
|
73 |
+
margin: 0;
|
74 |
+
font-family: 'Open Sans', sans-serif;
|
75 |
+
background-color: #121212;
|
76 |
+
color: #e0e0e0;
|
77 |
+
text-align: center;
|
78 |
+
}
|
79 |
+
|
80 |
+
.gradio-container {
|
81 |
+
max-width: 900px;
|
82 |
+
margin: 0 auto;
|
83 |
+
padding: 20px;
|
84 |
+
border-radius: 10px;
|
85 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
|
86 |
+
background-color: #1e1e1e;
|
87 |
+
}
|
88 |
+
|
89 |
+
h1, h2, h3, h4, h5, h6 {
|
90 |
+
font-family: 'Aldrich', sans-serif;
|
91 |
+
color: #ffffff;
|
92 |
+
text-align: center;
|
93 |
+
}
|
94 |
+
|
95 |
+
label {
|
96 |
+
font-weight: bold;
|
97 |
+
color: #bbbbbb;
|
98 |
+
text-align: center;
|
99 |
+
}
|
100 |
+
|
101 |
+
.gr-button {
|
102 |
+
background-color: #ff5722 !important;
|
103 |
+
color: #fff !important;
|
104 |
+
border: none !important;
|
105 |
+
padding: 10px 20px !important;
|
106 |
+
border-radius: 5px !important;
|
107 |
+
cursor: pointer !important;
|
108 |
+
transition: background-color 0.3s ease !important;
|
109 |
+
}
|
110 |
+
|
111 |
+
.gr-button:hover {
|
112 |
+
background-color: #e64a19 !important;
|
113 |
+
}
|
114 |
+
|
115 |
+
.gr-box, .gr-box-labeled, .gr-file, input[type=text], textarea, .gr-input, .gr-textbox input {
|
116 |
+
background-color: #444444 !important;
|
117 |
+
color: #e0e0e0 !important;
|
118 |
+
border: 1px solid #333333 !important;
|
119 |
+
border-radius: 5px !important;
|
120 |
+
padding: 10px !important;
|
121 |
+
}
|
122 |
+
|
123 |
+
.gr-file {
|
124 |
+
display: flex;
|
125 |
+
align-items: center;
|
126 |
+
justify-content: center;
|
127 |
+
height: 60px; /* Adjusted height */
|
128 |
+
border: 2px dashed #ff5722 !important;
|
129 |
+
transition: background-color 0.3s ease !important;
|
130 |
+
}
|
131 |
+
|
132 |
+
.gr-file:hover {
|
133 |
+
background-color: #555555 !important;
|
134 |
+
}
|
135 |
+
|
136 |
+
video, .gr-video {
|
137 |
+
border-radius: 5px !important;
|
138 |
+
}
|
139 |
+
|
140 |
+
.markdown-text {
|
141 |
+
color: #e0e0e0 !important;
|
142 |
+
}
|
143 |
+
"""
|
144 |
+
|
145 |
+
def main(audio_file, preset, beat_sensitivity, fps):
|
146 |
+
video_url = submit_job(audio_file, preset, beat_sensitivity, fps)
|
147 |
+
return video_url
|
148 |
+
|
149 |
+
with gr.Blocks(css=custom_css) as demo:
|
150 |
+
gr.Markdown(description1, elem_id="markdown-text")
|
151 |
+
gr.Markdown(description2, elem_id="markdown-text")
|
152 |
+
audio_file = gr.File(label="Upload MP3 File", file_types=['audio'])
|
153 |
+
preset = gr.Dropdown(label="Select Visualization Preset", choices=[
|
154 |
+
"pulsing-circles", "pulsing-hexagons", "pulsing-squares", "pulsing-triangles",
|
155 |
+
"rewbs-01", "rewbs-02", "rotating-shapes"
|
156 |
+
])
|
157 |
+
beat_sensitivity = gr.Slider(label="Beat Sensitivity", minimum=1, maximum=5, step=0.1, value=3)
|
158 |
+
fps = gr.Slider(label="FPS", minimum=12, maximum=24, step=1)
|
159 |
+
submit_button = gr.Button("Submit")
|
160 |
+
output_video = gr.Video(label="Output MP4")
|
161 |
+
|
162 |
+
def update_output(video_url):
|
163 |
+
output_video.update(value=video_url, visible=True)
|
164 |
+
|
165 |
+
submit_button.click(main, inputs=[audio_file, preset, beat_sensitivity, fps], outputs=[output_video])
|
166 |
+
|
167 |
+
if __name__ == "__main__":
|
168 |
+
demo.launch()
|