Spaces:
Running
Running
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 | |
def flip_text(prompt, negative_prompt, task, steps, sampler, cfg_scale, seed): | |
result = {"prompt": prompt,"negative_prompt": negative_prompt,"task": task,"steps": steps,"sampler": sampler,"cfg_scale": cfg_scale,"seed": seed} | |
print(result) | |
language = detect(prompt) | |
if language == 'ru': | |
prompt = GoogleTranslator(source='ru', target='en').translate(prompt) | |
print(prompt) | |
cfg = int(cfg_scale) | |
steps = int(steps) | |
seed = int(seed) | |
width = 1024 | |
height = 1024 | |
url_sd1 = os.getenv("url_sd1") | |
url_sd2 = os.getenv("url_sd2") | |
url_sd3 = os.getenv("url_sd3") | |
print(task) | |
try: | |
print('n_1') | |
with closing(create_connection(f"{url_sd3}", timeout=60)) as conn: | |
conn.send('{"fn_index":3,"session_hash":""}') | |
conn.send(f'{{"data":["{prompt}, 4k photo","[deformed | disfigured], poorly drawn, [bad : wrong] anatomy, [extra | missing | floating | disconnected] limb, (mutated hands and fingers), blurry",7.5,"(No style)"],"event_data":null,"fn_index":3,"session_hash":""}}') | |
while True: | |
status = json.loads(conn.recv())['msg'] | |
print(status) | |
if status == 'estimation': | |
continue | |
if status == 'process_starts': | |
break | |
photo = json.loads(conn.recv())['output']['data'][0][0] | |
photo = photo.replace('data:image/jpeg;base64,', '').replace('data:image/png;base64,', '') | |
photo = Image.open(io.BytesIO(base64.decodebytes(bytes(photo, "utf-8")))) | |
return photo | |
except: | |
print("n_2") | |
if task == 'Stable Diffusion XL 1.0': | |
model = 'sd_xl_base_1.0' | |
if task == 'Crystal Clear XL': | |
model = '[3d] crystalClearXL_ccxl_97637' | |
if task == 'Juggernaut XL': | |
model = '[photorealistic] juggernautXL_version2_113240' | |
if task == 'DreamShaper XL': | |
model = '[base model] dreamshaperXL09Alpha_alpha2Xl10_91562' | |
if task == 'SDXL Niji': | |
model = '[midjourney] sdxlNijiV51_sdxlNijiV51_112807' | |
if task == 'Cinemax SDXL': | |
model = '[movie] cinemaxAlphaSDXLCinema_alpha1_107473' | |
if task == 'NightVision XL': | |
model = '[photorealistic] nightvisionXLPhotorealisticPortrait_beta0702Bakedvae_113098' | |
negative = negative_prompt | |
try: | |
with closing(create_connection(f"{url_sd1}")) as conn: | |
conn.send('{"fn_index":231,"session_hash":""}') | |
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":""}}') | |
print(conn.recv()) | |
print(conn.recv()) | |
print(conn.recv()) | |
print(conn.recv()) | |
photo = f"{url_sd2}" + str(json.loads(conn.recv())['output']['data'][0][0]["name"]) | |
return photo | |
except: | |
return None | |
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.Row(): | |
prompt = gr.Textbox(placeholder="Введите описание изображения...", show_label=True, label='Описание изображения:', lines=3) | |
with gr.Row(): | |
task = gr.Radio(interactive=True, value="Stable Diffusion XL 1.0", show_label=True, label="Модель нейросети:", choices=['Stable Diffusion XL 1.0', 'Crystal Clear XL', | |
'Juggernaut XL', 'DreamShaper XL', | |
'SDXL Niji', 'Cinemax SDXL', 'NightVision XL']) | |
with gr.Tab("Расширенные настройки"): | |
with gr.Row(): | |
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") | |
with gr.Row(): | |
sampler = gr.Dropdown(value="DPM++ SDE Karras", show_label=True, label="Sampling Method:", choices=[ | |
"Euler", "Euler a", "Heun", "DPM++ 2M", "DPM++ SDE", "DPM++ 2M Karras", "DPM++ SDE Karras", "DDIM"]) | |
with gr.Row(): | |
steps = gr.Slider(show_label=True, label="Sampling Steps:", minimum=1, maximum=50, value=35, step=1) | |
with gr.Row(): | |
cfg_scale = gr.Slider(show_label=True, label="CFG Scale:", minimum=1, maximum=20, value=7, step=1) | |
with gr.Row(): | |
seed = gr.Number(show_label=True, label="Seed:", minimum=-1, maximum=1000000, value=-1, step=1) | |
with gr.Column(): | |
text_button = gr.Button("Сгенерировать изображение", variant='primary', elem_id="generate") | |
with gr.Column(scale=2): | |
image_output = gr.Image(show_label=True, label='Результат:', elem_id='image_output') | |
text_button.click(flip_text, inputs=[prompt, negative_prompt, task, steps, sampler, cfg_scale, seed], outputs=image_output) | |
demo.queue(concurrency_count=12) | |
demo.launch() |