import gradio as gr
import torch
import gc
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import os

os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:32"

model_id = "stabilityai/stable-diffusion-2-1"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")


def text2image(prompt):
    image = pipe(prompt).images[0]

    return image


demo = gr.Interface(fn=text2image,
                    inputs='text',
                    outputs='image',
                    title = "text2image",
                    examples = ['a photo of an astronaut riding a horse on mars'])


if __name__ == "__main__":
    demo.queue(concurrency_count=3).launch(server_name = "0.0.0.0", server_port = 7013)
