import gradio as gr
import requests
import os

# API ссылка
url = "https://stablediffusionapi.com/api/v4/dreambooth"

# API ключ
api_key = os.getenv("KEY")

model_list = ["AbsoluteReality 1.8.1", "Elldreth's Vivid Mix", "Anything V5", "Openjourney V4", "Analog Diffusion", "Lyriel 1.6", "Realistic Vision 5.0", "Dreamshaper 8", "epiCRealism v5", "CyberRealistic 3.3", "ToonYou 6", "Deliberate 3"]

# Функция для отправки запроса
def render(prompt, model, negative_prompt, width, height, guidance, seed, upscale):
    data = {
        "key": api_key,
        "model_id": model,
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "width": width,
        "height": height,
        "samples": "1",
        "num_inference_steps": "40",
        "safety_checker": "no",
        "enhance_prompt": "yes",
        "seed": seed,
        "guidance_scale": guidance,
        "multi_lingual": "no",
        "panorama": "no",
        "self_attention": "no",
        "upscale": upscale,
        "embeddings": "embeddings_model_id",
        "lora": "lora_model_id",
        "webhook": None,
        "track_id": None,
    }
    response = requests.post(url, json=data)
    if response.status_code == 200:
        return response.content
    else:
        return None

# Создаем компоненты для ввода данных
prompt_textbox = gr.Textbox(lines=3, placeholder="Введите описание изображения", label="Описание изображения:")
model_radio = gr.Radio(choices=model_list, label="Модель:", value="Anything V5", type="value")
negative_prompt_textbox = gr.Textbox(lines=3, placeholder="Введите Negative Prompt", label="Negative Prompt:")
width_slider = gr.Slider(minimum=256, maximum=2048, value=512, label="Ширина:")
height_slider = gr.Slider(minimum=256, maximum=2048, value=512, label="Высота:")
guidance_slider = gr.Slider(minimum=1, maximum=20, value=7.5, label="CFG Scale:")
seed_slider = gr.Slider(minimum=-1, maximum=1000000, value=-1, label="Seed:")
upscale_dropdown = gr.Dropdown(choices=["yes", "no"], label="Upscale", value="no")

# Создаем интерфейс
iface = gr.Interface(
    fn=render,
    inputs=[
        prompt_textbox,
        model_radio,
        negative_prompt_textbox,
        width_slider,
        height_slider,
        guidance_slider,
        seed_slider,
        upscale_dropdown,
    ],
    outputs=gr.Image(),
    title="Вкладка 1"
)

# Запускаем MultiPage
iface.launch()