Spaces:
Runtime error
Runtime error
File size: 7,620 Bytes
f41d114 9c88c69 bbc3e9e 3b4ef39 f41d114 9c88c69 bbc3e9e 8132e49 f41d114 9c88c69 f41d114 9c88c69 f41d114 |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
import gradio as gr
import requests
import time
import json
from contextlib import closing
from websocket import create_connection
from deep_translator import GoogleTranslator
from langdetect import detect
import os
from PIL import Image
import io
import base64
import os
import random
import tempfile
def animate_img(encoded_string):
r = requests.post("https://stable-video-diffusion.com/api/upload", files={"file": open(encoded_string, 'rb')})
print(r.text)
hash_ = r.json()['hash']
time.sleep(10)
while True:
r2 = requests.get(f"https://stable-video-diffusion.com/result?hash={hash_}")
source_string = r2.text
if "Generation has been in progress for" in source_string:
time.sleep(10)
continue
if "Generation has been in progress for" not in source_string:
pattern = r'https://storage.stable-video-diffusion.com/([a-f0-9]{32})\.mp4'
matches = re.findall(pattern, source_string)
sd_video = []
for match in matches:
sd_video.append(f"https://storage.stable-video-diffusion.com/{match}.mp4")
print(sd_video[0])
return sd_video[0]
def flip_text(prompt, motion):
try:
language = detect(prompt)
if language == 'ru':
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
print(prompt)
except:
prompt = 'video'
url_video_g = os.getenv("url_video_g")
url_video_c = os.getenv("url_video_c")
if motion == "Приближение →←":
motion = 'zoom in'
if motion == "Отдаление ←→":
motion = 'zoom out'
if motion == "Вверх ↑":
motion = 'up'
if motion == "Вниз ↓":
motion = 'down'
if motion == "Влево ←":
motion = 'left'
if motion == "Вправо →":
motion = 'right'
if motion == "По часовой стрелке ⟳":
motion = 'rotate cw'
if motion == "Против часовой стрелки ⟲":
motion = 'rotate ccw'
data = {"prompt": f"{prompt}","image": "null", "denoise": 0.75,"motion": motion}
r = requests.post(f"{url_video_g}", json=data)
while True:
data2 = {"task_id": f"{r.json()['task_id']}"}
r2 = requests.post(f"{url_video_c}", json=data2)
time.sleep(3)
try:
if r2.json()['status'] == "QUEUED":
continue
if r2.json()['status'] == "PROCESSING":
continue
except:
try:
n_im2 = f"{time.time()}"
with tempfile.NamedTemporaryFile(prefix=f'aaafff{n_im2}', suffix='.mp4', delete=False) as file:
for chunk in r2.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
return file.name
except Exception as e:
print(e)
break
def flip_text2(encoded_string, prompt, motion):
url_video_g = os.getenv("url_video_g")
url_video_c = os.getenv("url_video_c")
try:
language = detect(prompt)
if language == 'ru':
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
print(prompt)
except:
pass
if motion == "Приближение →←":
motion = 'zoom in'
if motion == "Отдаление ←→":
motion = 'zoom out'
if motion == "Вверх ↑":
motion = 'up'
if motion == "Вниз ↓":
motion = 'down'
if motion == "Влево ←":
motion = 'left'
if motion == "Вправо →":
motion = 'right'
if motion == "По часовой стрелке ⟳":
motion = 'rotate cw'
if motion == "Против часовой стрелки ⟲":
motion = 'rotate ccw'
with open(encoded_string, "rb") as image_file:
encoded_string2 = base64.b64encode(image_file.read())
encoded_string2 = str(encoded_string2).replace("b'", '')
data = {"prompt": f"{prompt}","image": f"{encoded_string2}","denoise":0.75,"motion": motion}
r = requests.post(f"{url_video_g}", json=data)
while True:
data2 = {"task_id": f"{r.json()['task_id']}"}
r2 = requests.post(f"{url_video_c}", json=data2)
time.sleep(3)
try:
if r2.json()['status'] == "QUEUED":
continue
if r2.json()['status'] == "PROCESSING":
continue
except:
try:
n_im2 = f"{time.time()}"
with tempfile.NamedTemporaryFile(prefix=f'aaafff{n_im2}', suffix='.mp4', delete=False) as file:
for chunk in r2.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
return file.name
except Exception as e:
print(e)
break
css = """
#generate {
width: 100%;
background: #e253dd !important;
border: none;
border-radius: 50px;
outline: none !important;
color: white;
}
#generate:hover {
background: #de6bda !important;
outline: none !important;
color: #fff;
}
footer {visibility: hidden !important;}
"""
with gr.Blocks(css=css) as demo:
with gr.Tab("Сгенерировать видео"):
with gr.Column():
prompt = gr.Textbox(placeholder="Введите описание видео...", show_label=True, label='Описание:', lines=3)
motion1 = gr.Dropdown(value="Приближение →←", interactive=True, show_label=True, label="Движение камеры:", choices=[
"Приближение →←", "Отдаление ←→", "Вверх ↑", "Вниз ↓", "Влево ←", "Вправо →", "По часовой стрелке ⟳", "Против часовой стрелки ⟲"])
with gr.Column():
text_button = gr.Button("Сгенерировать видео", variant='primary', elem_id="generate")
with gr.Column():
video_output = gr.Video(show_label=True, label='Результат:', type="file")
text_button.click(flip_text, inputs=[prompt, motion1], outputs=video_output)
with gr.Tab("Анимировать изображение"):
with gr.Column():
prompt2 = gr.Image(show_label=True, interactive=True, type='filepath', label='Исходное изображение:')
prompt12 = gr.Textbox(placeholder="Введите описание видео...", show_label=True, label='Описание видео (опционально):', lines=3)
motion2 = gr.Dropdown(value="Приближение →←", interactive=True, show_label=True, label="Движение камеры:", choices=[
"Приближение →←", "Отдаление ←→", "Вверх ↑", "Вниз ↓", "Влево ←", "Вправо →", "По часовой стрелке ⟳", "Против часовой стрелки ⟲"])
with gr.Column():
text_button2 = gr.Button("Анимировать изображение", variant='primary', elem_id="generate")
with gr.Column():
video_output2 = gr.Video(show_label=True, label='Результат:', type="file")
text_button2.click(animate_img, inputs=[prompt2], outputs=video_output2)
demo.queue(concurrency_count=12)
demo.launch() |