Spaces:
Runtime error
Runtime error
File size: 5,353 Bytes
42643f5 3a8e465 42643f5 3a8e465 8036d90 3a8e465 42643f5 3a8e465 42643f5 3a8e465 42643f5 3a8e465 42643f5 3a8e465 42643f5 3a8e465 42643f5 3a8e465 42643f5 3a8e465 42643f5 3a8e465 42643f5 |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
import gradio as gr
import json
from moviepy.video.io.VideoFileClip import VideoFileClip
import threading
import time
global_lock = threading.Lock()
ip_records = {}
def get_video_duration(video_path):
clip = VideoFileClip(video_path)
duration = clip.duration
clip.close()
return duration
from utils import processing
with open("sorted.json", "r") as f:
style_dicts = json.load(f)
styles = list(style_dicts.keys())
style_images = {k: v['thumbnailUrl'] for k, v in style_dicts.items() if v is not None}
# results_map = {
# "test32.mp4ClayStyle19:16": "test32.mp4",
# "test32.mp43D P19:16": "test32.mp4",
# }
def get_client_info():
request = gr.Request()
clientHost = request
print("IP address:", clientHost)
return clientHost
def check_ip_limit(client_ip):
current_time = time.time()
if client_ip not in ip_records:
ip_records[client_ip] = [current_time]
return True
else:
times = ip_records[client_ip]
times = [t for t in times if current_time - t < 5 * 60]
if len(times) < 2:
times.append(current_time)
ip_records[client_ip] = times
return True
else:
return False
def fn(in_video, style, aspect_ratio, prompt):
raise Exception("Service Updating")
print(f"Received video: {in_video}, aspect_ratio: {aspect_ratio}, style: {style}")
if in_video is None:
raise Exception("input_video is None")
# input_str = "".join([str(item) for item in [in_video, style, aspect_ratio]])
# if input_str in results_map:
# return results_map[input_str]
if aspect_ratio not in ["16:9", "9:16", "1:1"]:
raise Exception('aspect_ratio not in ["16:9", "9:16", "1:1"]')
if aspect_ratio == "16:9":
width, height = 1280, 720
elif aspect_ratio == "9:16":
width, height = 720, 1280
elif aspect_ratio == "1:1":
width, height = 960, 960
style_id = style_dicts[style]['id']
if style not in styles:
raise Exception("style not in styles")
video_duration = get_video_duration(in_video)
if video_duration < 3:
raise Exception("video_duration < 3")
transfer_duration = 3
res = None
try:
global_lock.acquire()
res = processing(in_video, width, height, style_id, transfer_duration, prompt)
if not res:
raise Exception("Processing failed")
finally:
global_lock.release()
return res
def load_description(fp):
with open(fp, 'r', encoding='utf-8') as f:
content = f.read()
return content
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
# Transform your videos with different Anime styles and have fun!
gr.HTML(load_description("title.md"))
with gr.Tab("Video Processing"):
with gr.Row():
in_video = gr.Video(label="1. Input Video (Please provide a clear video clip)", interactive=True)
aspect_ratio = gr.Radio(["16:9", "9:16", "1:1"], label="3. Aspect Ratio(Width:Height)", value="16:9", interactive=True)
prompt = gr.Textbox(label="4. Prompt", value="a character with cute face")
out_video = gr.Video(label="Final Output Video")
with gr.Row():
style = gr.Radio(
choices=styles,
label="2. Style, for more styles, please join our [Discord](https://discord.gg/cN8geBsBmu)",
value="ClayStyle1",
interactive=True
)
preview = gr.Image(
value=style_images["ClayStyle1"],
label="Style Preview",
show_label=True,
interactive=False
)
def update_preview(choice):
return style_images[choice]
style.change(fn=update_preview, inputs=style, outputs=preview)
submit_button = gr.Button("Generate", interactive=True, variant="primary")
examples = gr.Examples(
[
["test32.mp4", "ClayStyle1", "9:16", "a girl with cute face is walking"],
["test32.mp4", "3D P1", "9:16", "a girl with cute face is walking" ],
["test32.mp4", "FlatanimeV4-3", "9:16", "a girl with cute face is walking" ],
["test32.mp4", "FlatanimeV4 Delicated", "9:16", "a girl with cute face is walking" ],
["test32.mp4", "illustrationV4-1", "9:16", "a girl with cute face is walking" ],
["test32.mp4", "Child Crayon Drawing", "9:16", "a girl with cute face is walking" ],
["test32.mp4", "Pixels", "9:16", "a girl with cute face is walking" ],
["test32.mp4", "Color Ink Painting", "9:16", "a girl with cute face is walking" ],
["test32.mp4", "cutefaceV4 style1", "9:16", "a girl with cute face is walking" ],
["test32.mp4", "flatanime-gibli", "9:16", "a girl with cute face is walking" ],
],
inputs=[in_video, style, aspect_ratio, prompt],
outputs=[out_video],
fn=fn,
)
submit_button.click(
fn,
inputs=[in_video, style, aspect_ratio, prompt],
outputs=[out_video],
)
gr.HTML(load_description("end.md"))
if __name__ == "__main__":
demo.launch(show_error=True, share=False) |