-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest_handling.py
85 lines (56 loc) · 3.01 KB
/
request_handling.py
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
import requests
import os
import base64
import streamlit as st
from get_frames import *
api = 'http://127.0.0.1:7860'
# SD_MODEL = '768-v-ema.safetensors'
# negp = '''two bodies, two heads, doll, extra nipples, bad anatomy, blurry, fuzzy, extra arms, extra fingers, poorly drawn hands, disfigured, tiling, deformed, mutated, out of frame, cloned face, ugly, disfigured, bad proportion, out of frame, b&w, painting, drawing, watermark, logo, text, signature, icon, monochrome, blurry, ugly, cartoon, 3d, bad_prompt, long neck, totem pole, multiple heads, multiple jaws, disfigured, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, username, artist name, ancient, character, frame, child, asian, cartoon, animation, grayscale 3d, disfigured, bad art, deformed, poorly drawn, extra limbs, strange colours, boring, sketch, lackluster, repetitive, cropped, naked, nude, disfigured, double heads, duplicated, text, oversaturated'''
def process(path_to_video: str,
subject_matter: list,
keywords: str,
images: list):
# st.lottie(lottie_loading, height=50)
# TODO: вызываю Толю, разбиваем видео на кадры
frame_paths = get_frames(path_to_video, './temp')
ret_list = []
for path in frame_paths:
gen_path = img2img(api, keywords, 15, path)
ret_list.append(gen_path)
st.image(gen_path)
return ret_list
# TODO: вызываю Настю, удаляем плохие слова
def img2img(api, text, steps, image_path):
# opt = requests.get(url=f'{api}/sdapi/v1/options')
# opt_json = opt.json()
# # TODO: тут менять модель
# opt_json['sd_model_checkpoint'] = SD_MODEL
# requests.post(url=f'{api}/sdapi/v1/options', json=opt_json)
# путь до скрипта
api_url = f"{api}/sdapi/v1/img2img"
# открыли файл
with open(image_path, 'rb') as file:
image_data = file.read()
# закодировали для сетки
encoded_image = base64.b64encode(image_data).decode('utf-8')
# это то что отправляем нейросетке
payload = {
'prompt' : text,
"init_images": [encoded_image],
"steps": steps,
}
# ответ
sd_response = requests.post(api_url, json=payload)
name = f'{os.path.basename(image_path)}_temp'
if sd_response.status_code == 200:
sd_response = sd_response.json()
encoded_result = sd_response["images"][0]
result_data = base64.b64decode(encoded_result)
output_path = f"results/{name}.jpg"
with open(output_path, 'wb') as file:
file.write(result_data)
return output_path
else:
print("Ошибка при выполнении запроса:", sd_response.text)
return None
# print(process('/home/stdf/Desktop/12.mp4', [], 'guy with a sport car', []))