| import gradio as gr |
| import requests |
| import time |
| import json |
| import base64 |
| import os |
| from io import BytesIO |
| import html |
| import re |
| import cv2 |
| import torch |
| from basicsr.archs.srvgg_arch import SRVGGNetCompact |
| from gfpgan.utils import GFPGANer |
| from realesrgan.utils import RealESRGANer |
|
|
| os.system("pip freeze") |
| |
| if not os.path.exists('realesr-general-x4v3.pth'): |
| os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .") |
| if not os.path.exists('GFPGANv1.2.pth'): |
| os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .") |
| if not os.path.exists('GFPGANv1.3.pth'): |
| os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .") |
| if not os.path.exists('GFPGANv1.4.pth'): |
| os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .") |
| if not os.path.exists('RestoreFormer.pth'): |
| os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .") |
| if not os.path.exists('CodeFormer.pth'): |
| os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/CodeFormer.pth -P .") |
|
|
| |
| model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu') |
| model_path = 'realesr-general-x4v3.pth' |
| half = True if torch.cuda.is_available() else False |
| upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half) |
|
|
| os.makedirs('output', exist_ok=True) |
|
|
|
|
|
|
| class Prodia: |
| def __init__(self, api_key, base=None): |
| self.base = base or "https://api.prodia.com/v1" |
| self.headers = { |
| "X-Prodia-Key": api_key |
| } |
| |
| def generate(self, params): |
| response = self._post(f"{self.base}/sd/generate", params) |
| return response.json() |
| |
| def transform(self, params): |
| response = self._post(f"{self.base}/sd/transform", params) |
| return response.json() |
| |
| def controlnet(self, params): |
| response = self._post(f"{self.base}/sd/controlnet", params) |
| return response.json() |
| |
| def get_job(self, job_id): |
| response = self._get(f"{self.base}/job/{job_id}") |
| return response.json() |
|
|
| def wait(self, job): |
| job_result = job |
|
|
| while job_result['status'] not in ['succeeded', 'failed']: |
| time.sleep(0.25) |
| job_result = self.get_job(job['job']) |
|
|
| return job_result |
|
|
| def list_models(self): |
| response = self._get(f"{self.base}/sd/models") |
| return response.json() |
|
|
| def list_samplers(self): |
| response = self._get(f"{self.base}/sd/samplers") |
| return response.json() |
|
|
| def _post(self, url, params): |
| headers = { |
| **self.headers, |
| "Content-Type": "application/json" |
| } |
| response = requests.post(url, headers=headers, data=json.dumps(params)) |
|
|
| if response.status_code != 200: |
| raise Exception(f"Bad Prodia Response: {response.status_code}") |
|
|
| return response |
|
|
| def _get(self, url): |
| response = requests.get(url, headers=self.headers) |
|
|
| if response.status_code != 200: |
| raise Exception(f"Bad Prodia Response: {response.status_code}") |
|
|
| return response |
|
|
|
|
| def image_to_base64(image): |
| |
| buffered = BytesIO() |
| image.save(buffered, format="PNG") |
| |
| |
| img_str = base64.b64encode(buffered.getvalue()) |
|
|
| return img_str.decode('utf-8') |
|
|
|
|
| def remove_id_and_ext(text): |
| text = re.sub(r'\[.*\]$', '', text) |
| extension = text[-12:].strip() |
| if extension == "safetensors": |
| text = text[:-13] |
| elif extension == "ckpt": |
| text = text[:-4] |
| return text |
|
|
|
|
| def get_data(text): |
| results = {} |
| patterns = { |
| 'prompt': r'(.*)', |
| 'negative_prompt': r'Negative prompt: (.*)', |
| 'steps': r'Steps: (\d+),', |
| 'seed': r'Seed: (\d+),', |
| 'sampler': r'Sampler:\s*([^\s,]+(?:\s+[^\s,]+)*)', |
| 'model': r'Model:\s*([^\s,]+)', |
| 'cfg_scale': r'CFG scale:\s*([\d\.]+)', |
| 'size': r'Size:\s*([0-9]+x[0-9]+)' |
| } |
| for key in ['prompt', 'negative_prompt', 'steps', 'seed', 'sampler', 'model', 'cfg_scale', 'size']: |
| match = re.search(patterns[key], text) |
| if match: |
| results[key] = match.group(1) |
| else: |
| results[key] = None |
| if results['size'] is not None: |
| w, h = results['size'].split("x") |
| results['w'] = w |
| results['h'] = h |
| else: |
| results['w'] = None |
| results['h'] = None |
| return results |
|
|
|
|
| def send_to_txt2img(image): |
|
|
| result = {tabs: gr.update(selected="t2i")} |
|
|
| try: |
| text = image.info['parameters'] |
| data = get_data(text) |
| result[prompt] = gr.update(value=data['prompt']) |
| result[negative_prompt] = gr.update(value=data['negative_prompt']) if data['negative_prompt'] is not None else gr.update() |
| result[steps] = gr.update(value=int(data['steps'])) if data['steps'] is not None else gr.update() |
| result[seed] = gr.update(value=int(data['seed'])) if data['seed'] is not None else gr.update() |
| result[cfg_scale] = gr.update(value=float(data['cfg_scale'])) if data['cfg_scale'] is not None else gr.update() |
| result[width] = gr.update(value=int(data['w'])) if data['w'] is not None else gr.update() |
| result[height] = gr.update(value=int(data['h'])) if data['h'] is not None else gr.update() |
| result[sampler] = gr.update(value=data['sampler']) if data['sampler'] is not None else gr.update() |
| if model in model_names: |
| result[model] = gr.update(value=model_names[model]) |
| else: |
| result[model] = gr.update() |
| return result |
|
|
| except Exception as e: |
| print(e) |
|
|
| return result |
|
|
|
|
| prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY")) |
| model_list = prodia_client.list_models() |
| model_names = {} |
|
|
| for model_name in model_list: |
| name_without_ext = remove_id_and_ext(model_name) |
| model_names[name_without_ext] = model_name |
|
|
|
|
| def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed): |
| result = prodia_client.generate({ |
| "prompt": prompt, |
| "negative_prompt": negative_prompt, |
| "model": model, |
| "steps": steps, |
| "sampler": sampler, |
| "cfg_scale": cfg_scale, |
| "width": width, |
| "height": height, |
| "seed": seed |
| }) |
|
|
| job = prodia_client.wait(result) |
|
|
| return job["imageUrl"] |
|
|
|
|
| def img2img(img, version, scale, weight): |
| weight /= 100 |
| print(img, version, scale, weight) |
| try: |
| extension = os.path.splitext(os.path.basename(str(img)))[1] |
| img = cv2.imread(img, cv2.IMREAD_UNCHANGED) |
| if len(img.shape) == 3 and img.shape[2] == 4: |
| img_mode = 'RGBA' |
| elif len(img.shape) == 2: |
| img_mode = None |
| img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) |
| else: |
| img_mode = None |
|
|
| if version == 'v1.2': |
| face_enhancer = GFPGANer( |
| model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler) |
| elif version == 'v1.3': |
| face_enhancer = GFPGANer( |
| model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler) |
| elif version == 'v1.4': |
| face_enhancer = GFPGANer( |
| model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler) |
| elif version == 'RestoreFormer': |
| face_enhancer = GFPGANer( |
| model_path='RestoreFormer.pth', upscale=2, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler) |
| elif version == 'CodeFormer': |
| face_enhancer = GFPGANer( |
| model_path='CodeFormer.pth', upscale=2, arch='CodeFormer', channel_multiplier=2, bg_upsampler=upsampler) |
|
|
| try: |
| _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight) |
| except RuntimeError as error: |
| print('Error', error) |
|
|
| try: |
| interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4 |
| h, w = img.shape[0:2] |
| output = cv2.resize(output, (int(w * scale), int(h * scale)), interpolation=interpolation) |
| except Exception as error: |
| print('wrong scale input.', error) |
| if img_mode == 'RGBA': |
| extension = 'png' |
| else: |
| extension = 'jpg' |
| save_path = f'output/out.{extension}' |
| cv2.imwrite(save_path, output) |
|
|
| output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB) |
| return output |
| except Exception as error: |
| print('global exception', error) |
| return None |
|
|
| css = """ |
| footer {visibility: hidden !important;} |
| """ |
| |
| with gr.Blocks(css=css) as demo: |
| |
| with gr.Tabs() as tabs: |
| with gr.Row(): |
| with gr.Column(scale=3): |
| with gr.Tab("Базовые настройки"): |
| with gr.Row(): |
| prompt = gr.Textbox(placeholder="Введите описание изображения...", show_label=True, label="Описание изображения", lines=3) |
| |
| with gr.Accordion(label="Модель нейросети:", open=False): |
| model = gr.Radio(interactive=True, value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=False, choices=prodia_client.list_models()) |
| |
| with gr.Tab("Расширенные настройки"): |
| with gr.Row(): |
| with gr.Row(): |
| negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="[deformed | disfigured], poorly drawn, [bad : wrong] anatomy, [extra | missing | floating | disconnected] limb, (mutated hands and fingers), blurry") |
| with gr.Column(scale=1): |
| sampler = gr.Dropdown(value="DPM++ 2M Karras", show_label=True, label="Sampling Method", choices=prodia_client.list_samplers()) |
| with gr.Column(scale=1): |
| steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=30, value=25, step=1) |
| with gr.Column(scale=1): |
| width = gr.Slider(label="Ширина", minimum=15, maximum=1024, value=512, step=8) |
| height = gr.Slider(label="Длина", minimum=15, maximum=1024, value=512, step=8) |
| |
| cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1) |
| seed = gr.Slider(label="Seed", minimum=-1, maximum=10000000, value=-1) |
| with gr.Tab("Настройки апскейлинга"): |
| with gr.Row(): |
| version = gr.Radio(choices=['v1.2', 'v1.3', 'v1.4', 'RestoreFormer', 'CodeFormer'], value='v1.4', label='Версия'), |
| scale = gr.Number(label="Коэффициент масштабирования", value=2), |
| weight = gr.Slider(0, 100, label='Weight, только для CodeFormer. 0 для лучшего качества, 100 для лучшей идентичности', value=50) |
|
|
|
|
| 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='Сгенерированное изображение:') |
|
|
| with gr.Column(): |
| text_button_up = gr.Button("Улучшить качество", variant='secondary', elem_id="upscb") |
| with gr.Column(scale=2): |
| image_output_up = gr.Image(show_label=True, label='Увеличенное изображение:') |
|
|
| text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output) |
| text_button_up.click(img2img, inputs=[image_output, version, scale, weight], outputs=image_output_up) |
| |
| demo.queue(concurrency_count=64, max_size=80, api_open=False).launch(max_threads=256) |
|
|