lalashechka commited on
Commit
f41d114
1 Parent(s): 49672b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -0
app.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import time
4
+ import json
5
+ from contextlib import closing
6
+ from websocket import create_connection
7
+ from deep_translator import GoogleTranslator
8
+ from langdetect import detect
9
+ import os
10
+ from PIL import Image
11
+ import io
12
+ import base64
13
+ import os
14
+ import random
15
+ import tempfile
16
+
17
+
18
+ def flip_text(prompt, motion):
19
+ language = detect(prompt)
20
+
21
+ url_video_g = os.getenv("url_video_g")
22
+ url_video_c = os.getenv("url_video_c")
23
+
24
+ if language == 'ru':
25
+ prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
26
+ print(prompt)
27
+
28
+ if motion == "Приближение →←":
29
+ motion = 'zoom in'
30
+ if motion == "Отдаление ←→":
31
+ motion = 'zoom out'
32
+ if motion == "Вверх ↑":
33
+ motion = 'up'
34
+ if motion == "Вниз ↓":
35
+ motion = 'down'
36
+ if motion == "Влево ←":
37
+ motion = 'left'
38
+ if motion == "Вправо →":
39
+ motion = 'right'
40
+ if motion == "По часовой стрелке ⟳":
41
+ motion = 'rotate cw'
42
+ if motion == "Против часовой стрелки ⟲":
43
+ motion = 'rotate ccw'
44
+
45
+ data = {"prompt": f"{prompt}","image": "null", "denoise": 0.75,"motion": motion}
46
+ r = requests.post(f"{url_video_g}", json=data)
47
+ while True:
48
+ data2 = {"task_id": f"{r.json()['task_id']}"}
49
+ r2 = requests.post(f"{url_video_c}", json=data2)
50
+ time.sleep(3)
51
+ try:
52
+ if r2.json()['status'] == "QUEUED":
53
+ continue
54
+ if r2.json()['status'] == "PROCESSING":
55
+ continue
56
+ except:
57
+ try:
58
+ n_im2 = f"{time.time()}"
59
+ with tempfile.NamedTemporaryFile(prefix=f'aaafff{n_im2}', suffix='.mp4', delete=False) as file:
60
+ for chunk in r2.iter_content(chunk_size=1024):
61
+ if chunk:
62
+ file.write(chunk)
63
+ return file.name
64
+ except Exception as e:
65
+ print(e)
66
+ break
67
+
68
+
69
+ def flip_text2(encoded_string, prompt, motion):
70
+ try:
71
+ language = detect(prompt)
72
+ if language == 'ru':
73
+ prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
74
+ print(prompt)
75
+ except:
76
+ pass
77
+
78
+ if motion == "Приближение →←":
79
+ motion = 'zoom in'
80
+ if motion == "Отдаление ←→":
81
+ motion = 'zoom out'
82
+ if motion == "Вверх ↑":
83
+ motion = 'up'
84
+ if motion == "Вниз ↓":
85
+ motion = 'down'
86
+ if motion == "Влево ←":
87
+ motion = 'left'
88
+ if motion == "Вправо →":
89
+ motion = 'right'
90
+ if motion == "По часовой стрелке ⟳":
91
+ motion = 'rotate cw'
92
+ if motion == "Против часовой стрелки ⟲":
93
+ motion = 'rotate ccw'
94
+
95
+ with open(encoded_string, "rb") as image_file:
96
+ encoded_string2 = base64.b64encode(image_file.read())
97
+ encoded_string2 = str(encoded_string2).replace("b'", '')
98
+
99
+ data = {"prompt": f"{prompt}","image": f"{encoded_string2}","denoise":0.75,"motion": motion}
100
+ r = requests.post(f"{url_video_g}", json=data)
101
+ while True:
102
+ data2 = {"task_id": f"{r.json()['task_id']}"}
103
+ r2 = requests.post(f"{url_video_c}", json=data2)
104
+ time.sleep(3)
105
+ try:
106
+ if r2.json()['status'] == "QUEUED":
107
+ continue
108
+ if r2.json()['status'] == "PROCESSING":
109
+ continue
110
+ except:
111
+ try:
112
+ n_im2 = f"{time.time()}"
113
+ with tempfile.NamedTemporaryFile(prefix=f'aaafff{n_im2}', suffix='.mp4', delete=False) as file:
114
+ for chunk in r2.iter_content(chunk_size=1024):
115
+ if chunk:
116
+ file.write(chunk)
117
+ return file.name
118
+ except Exception as e:
119
+ print(e)
120
+ break
121
+
122
+
123
+ css = """
124
+ #generate {
125
+ width: 100%;
126
+ background: #e253dd !important;
127
+ border: none;
128
+ border-radius: 50px;
129
+ outline: none !important;
130
+ color: white;
131
+ }
132
+ #generate:hover {
133
+ background: #de6bda !important;
134
+ outline: none !important;
135
+ color: #fff;
136
+ }
137
+ footer {visibility: hidden !important;}
138
+ """
139
+
140
+ with gr.Blocks(css=css) as demo:
141
+
142
+ with gr.Tab("Сгенерировать видео"):
143
+ with gr.Column():
144
+ prompt = gr.Textbox(placeholder="Введите описание видео...", show_label=True, label='Описание:', lines=3)
145
+ motion1 = gr.Dropdown(value="Приближение →←", interactive=True, show_label=True, label="Движение камеры:", choices=[
146
+ "Приближение →←", "Отдаление ←→", "Вверх ↑", "Вниз ↓", "Влево ←", "Вправо →", "По часовой стрелке ⟳", "Против час��вой стрелки ⟲"])
147
+ with gr.Column():
148
+ text_button = gr.Button("Сгенерировать видео", variant='primary', elem_id="generate")
149
+ with gr.Column():
150
+ video_output = gr.Video(show_label=True, label='Результат:', type="file")
151
+ text_button.click(flip_text, inputs=[prompt, motion1], outputs=video_output)
152
+
153
+ with gr.Tab("Анимировать изображение"):
154
+ with gr.Column():
155
+ prompt2 = gr.Image(show_label=True, interactive=True, type='filepath', label='Исходное изображение:')
156
+ prompt12 = gr.Textbox(placeholder="Введите описание видео...", show_label=True, label='Описание видео (опционально):', lines=3)
157
+ motion2 = gr.Dropdown(value="Приближение →←", interactive=True, show_label=True, label="Движение камеры:", choices=[
158
+ "Приближение →←", "Отдаление ←→", "Вверх ↑", "Вниз ↓", "Влево ←", "Вправо →", "По часовой стрелке ⟳", "Против часовой стрелки ⟲"])
159
+ with gr.Column():
160
+ text_button2 = gr.Button("Анимировать изображение", variant='primary', elem_id="generate")
161
+ with gr.Column():
162
+ video_output2 = gr.Video(show_label=True, label='Результат:', type="file")
163
+ text_button2.click(flip_text2, inputs=[prompt2, prompt12, motion2], outputs=video_output2)
164
+
165
+ demo.queue(concurrency_count=12)
166
+ demo.launch()