Spaces:
Running
Running
lalashechka
commited on
Commit
·
22d4eb1
1
Parent(s):
294b326
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
11 |
+
|
12 |
+
def flip_text(prompt, negative_prompt, task, steps, sampler, cfg_scale, seed):
|
13 |
+
result = {"prompt": prompt,"negative_prompt": negative_prompt,"task": task,"steps": steps,"sampler": sampler,"cfg_scale": cfg_scale,"seed": seed}
|
14 |
+
print(result)
|
15 |
+
|
16 |
+
language = detect(prompt)
|
17 |
+
|
18 |
+
if language == 'ru':
|
19 |
+
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
20 |
+
print(prompt)
|
21 |
+
|
22 |
+
cfg = int(cfg_scale)
|
23 |
+
steps = int(steps)
|
24 |
+
seed = int(seed)
|
25 |
+
|
26 |
+
width = 1024
|
27 |
+
height = 1024
|
28 |
+
url_sd1 = os.getenv("url_sd1")
|
29 |
+
url_sd2 = os.getenv("url_sd2")
|
30 |
+
|
31 |
+
print(task)
|
32 |
+
|
33 |
+
if task == 'Stable Diffusion XL 1.0':
|
34 |
+
model = 'sd_xl_base_1.0'
|
35 |
+
if task == 'Crystal Clear XL':
|
36 |
+
model = '[3d] crystalClearXL_ccxl_97637'
|
37 |
+
if task == 'Juggernaut XL':
|
38 |
+
model = '[photorealistic] juggernautXL_version2_113240'
|
39 |
+
if task == 'DreamShaper XL':
|
40 |
+
model = '[base model] dreamshaperXL09Alpha_alpha2Xl10_91562'
|
41 |
+
if task == 'SDXL Niji':
|
42 |
+
model = '[midjourney] sdxlNijiV51_sdxlNijiV51_112807'
|
43 |
+
if task == 'Cinemax SDXL':
|
44 |
+
model = '[movie] cinemaxAlphaSDXLCinema_alpha1_107473'
|
45 |
+
if task == 'NightVision XL':
|
46 |
+
model = '[photorealistic] nightvisionXLPhotorealisticPortrait_beta0702Bakedvae_113098'
|
47 |
+
|
48 |
+
negative = negative_prompt
|
49 |
+
|
50 |
+
try:
|
51 |
+
with closing(create_connection(f"{url_sd1}")) as conn:
|
52 |
+
conn.send('{"fn_index":231,"session_hash":""}')
|
53 |
+
conn.send(f'{{"data":["task()","{prompt}","{negative}",[],{steps},"{sampler}",false,false,1,1,{cfg},{seed},-1,0,0,0,false,{width},{height},false,0.7,2,"Lanczos",0,0,0,"Use same sampler","","",[],"None",true,"{model}","Automatic",null,null,null,false,false,"positive","comma",0,false,false,"","Seed","",[],"Nothing","",[],"Nothing","",[],true,false,false,false,0,null,null,false,null,null,false,null,null,false,50,[],"","",""],"event_data":null,"fn_index":231,"session_hash":""}}')
|
54 |
+
print(conn.recv())
|
55 |
+
print(conn.recv())
|
56 |
+
print(conn.recv())
|
57 |
+
print(conn.recv())
|
58 |
+
photo = f"{url_sd2}" + str(json.loads(conn.recv())['output']['data'][0][0]["name"])
|
59 |
+
return photo
|
60 |
+
except:
|
61 |
+
return None
|
62 |
+
|
63 |
+
css = """
|
64 |
+
#generate {
|
65 |
+
width: 100%;
|
66 |
+
background: #e253dd !important;
|
67 |
+
border: none;
|
68 |
+
border-radius: 50px;
|
69 |
+
outline: none !important;
|
70 |
+
color: white;
|
71 |
+
}
|
72 |
+
#generate:hover {
|
73 |
+
background: #de6bda !important;
|
74 |
+
outline: none !important;
|
75 |
+
color: #fff;
|
76 |
+
}
|
77 |
+
footer {visibility: hidden !important;}
|
78 |
+
"""
|
79 |
+
|
80 |
+
with gr.Blocks(css=css) as demo:
|
81 |
+
|
82 |
+
with gr.Tab("Базовые настройки"):
|
83 |
+
with gr.Row():
|
84 |
+
prompt = gr.Textbox(placeholder="Введите описание изображения...", show_label=True, label='Описание изображения:', lines=3)
|
85 |
+
with gr.Row():
|
86 |
+
task = gr.Radio(interactive=True, value="Stable Diffusion XL 1.0", show_label=True, label="Модель нейросети:", choices=['Stable Diffusion XL 1.0', 'Crystal Clear XL',
|
87 |
+
'Juggernaut XL', 'DreamShaper XL',
|
88 |
+
'SDXL Niji', 'Cinemax SDXL', 'NightVision XL'])
|
89 |
+
with gr.Tab("Расширенные настройки"):
|
90 |
+
with gr.Row():
|
91 |
+
negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=True, label='Negative Prompt:', lines=3, value="[deformed | disfigured], poorly drawn, [bad : wrong] anatomy, [extra | missing | floating | disconnected] limb, (mutated hands and fingers), blurry")
|
92 |
+
with gr.Row():
|
93 |
+
sampler = gr.Dropdown(value="DPM++ SDE Karras", show_label=True, label="Sampling Method:", choices=[
|
94 |
+
"Euler", "Euler a", "Heun", "DPM++ 2M", "DPM++ SDE", "DPM++ 2M Karras", "DPM++ SDE Karras", "DDIM"])
|
95 |
+
with gr.Row():
|
96 |
+
steps = gr.Slider(show_label=True, label="Sampling Steps:", minimum=1, maximum=50, value=35, step=1)
|
97 |
+
with gr.Row():
|
98 |
+
cfg_scale = gr.Slider(show_label=True, label="CFG Scale:", minimum=1, maximum=20, value=7, step=1)
|
99 |
+
with gr.Row():
|
100 |
+
seed = gr.Number(show_label=True, label="Seed:", minimum=-1, maximum=1000000, value=-1, step=1)
|
101 |
+
with gr.Column():
|
102 |
+
text_button = gr.Button("Сгенерировать изображение", variant='primary', elem_id="generate")
|
103 |
+
with gr.Column(scale=2):
|
104 |
+
image_output = gr.Image(show_label=True, label='Результат:', elem_id='image_output')
|
105 |
+
|
106 |
+
text_button.click(flip_text, inputs=[prompt, negative_prompt, task, steps, sampler, cfg_scale, seed], outputs=image_output)
|
107 |
+
|
108 |
+
demo.queue(concurrency_count=12)
|
109 |
+
demo.launch()
|