MKAtaturkv2 / app.py
samet
Update app.py
30179cd
raw
history blame contribute delete
No virus
9.24 kB
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
import gradio as gr
import torch
from transformers import pipeline
from PIL import Image
model_id = 'Extraphy/mustafa-kemal-ataturkv2'
prefix = 'Atatürk'
scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
scheduler=scheduler)
#pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
#model_id,
#torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
# scheduler=scheduler)
if torch.cuda.is_available():
pipe = pipe.to("cuda")
#pipe_i2i = pipe_i2i.to("cuda")
def error_str(error, title="Error"):
return f"""#### {title}
{error}""" if error else ""
def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", auto_prefix=False):
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
prompt = f"{prefix} {prompt}" if auto_prefix else prompt
try:
return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
except Exception as e:
return None, error_str(e)
def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
result = pipe(
prompt,
negative_prompt = neg_prompt,
num_inference_steps = int(steps),
guidance_scale = guidance,
width = width,
height = height,
generator = generator)
return result.images[0]
css = """
.button, input, optgroup, select, textarea {
font-family: 'Source Sans Pro';
font-size: 80%;
font-weight: inherit;
line-height: inherit;
color: orangered;
margin: 0;
padding: 0;
font-weight: 600;
}
.gr-form {
display: contents;
flex-direction: column;
align-items: center;
justify-content: center;
width: 50%;
line-height: 0.9rem;
}
.gr-label {
font-size: 1.2rem;
margin-bottom: 0.5rem;
}
.gr-input, .gr-select {
width: 100%;
font-size: 0.8rem;
border: 1px solid #f78900;
border-radius: 0.15rem;
}
.gr-input[type="number"] {
width: fit-content;
}
.gr-button {
font-size: 1.2rem;
padding: 1.5rem 0.2rem;
border: none;
border-radius: 0.25rem;
background-image: linear-gradient(0deg, rgb(15, 54, 96) 0.00%,rgb(252, 133, 123) 99.00%);
background: linear-gradient(0deg, rgb(185, 54, 96) 0.00%,rgb(252, 133, 123) 99.00%);
color: #f3f3f3;
cursor: copy;
}
.gr-button:hover {
background-color: Red;
}
}
.main-div div {
display: inline-flex;
align-items: center;
gap: .8rem;
font-size: 1.75rem;
}
.main-div div h1 {
font-weight: 1000;
margin-bottom: 7px;
}
.main-div p {
margin-bottom: 10px;
font-size: 94%;
}
a {
text-decoration: underline;
}
.tabs {
margin-top: 0;
margin-bottom: 0;
}
#gallery {
min-height: 20rem;
}
.container {
width: 70%;
}
.hcontainer {
width: 80%;
display: flex;
overflow: auto;
min-height: 185px;
align-items: center;
flex-direction: column;
justify-content: flex-start;
}
.hcontainer1 {
top: 20px;
right: 0px;
width: 100%;
height: 90px;
display: flex;
position: absolute;
align-items: flex-start;
border-radius: var(--dl-radius-radius-radius8);
justify-content: flex-start;
}
.hcontainer2 {
top: 20px;
right: 0px;
width: 100%;
height: 100%;
display: flex;
position: absolute;
align-items: flex-start;
border-radius: var(--dl-radius-radius-radius8);
justify-content: flex-start;
background-image: linear-gradient(0deg, rgb(185, 54, 96) 0.00%,rgb(252, 133, 123) 99.00%);
}
.home-image {
top: 0px;
right: var(--dl-space-space-halfunit);
width: 50%;
bottom: 0px;
margin: auto;
position: absolute;
object-fit: cover;
}
.home-image1 {
top: 17px;
left: 23px;
width: 348px;
height: 52px;
margin: auto;
position: absolute;
align-self: flex-start;
object-fit: cover;
}
:root {
--dl-color-gray-500: #595959;
--dl-color-gray-700: #999999;
--dl-color-gray-900: #D9D9D9;
--dl-size-size-large: 144px;
--dl-size-size-small: 48px;
--dl-color-danger-300: #A22020;
--dl-color-danger-500: #BF2626;
--dl-color-danger-700: #E14747;
--dl-color-gray-black: #000000;
--dl-color-gray-white: #FFFFFF;
--dl-size-size-medium: 96px;
--dl-size-size-xlarge: 192px;
--dl-size-size-xsmall: 16px;
--dl-space-space-unit: 16px;
--dl-color-primary-100: #003EB3;
--dl-color-primary-300: #0074F0;
--dl-color-primary-500: #14A9FF;
--dl-color-primary-700: #85DCFF;
--dl-color-success-300: #199033;
--dl-color-success-500: #32A94C;
--dl-color-success-700: #4CC366;
--dl-size-size-xxlarge: 288px;
--dl-radius-radius-round: 50%;
--dl-space-space-halfunit: 8px;
--dl-space-space-sixunits: 96px;
--dl-space-space-twounits: 32px;
--dl-radius-radius-radius2: 2px;
--dl-radius-radius-radius4: 4px;
--dl-radius-radius-radius8: 8px;
--dl-space-space-fiveunits: 80px;
--dl-space-space-fourunits: 64px;
--dl-space-space-threeunits: 48px;
--dl-space-space-oneandhalfunits: 24px;
}
"""
with gr.Blocks(css=css) as demo:
gr.HTML(
f"""
<div class="hcontainer">
<div class="hcontainer1">
<div class="hcontainer2">
<img
alt="image"
src="https://i.imgur.com/mLhGqm1.png"
class="home-image"
/>
<img
alt="image"
src="https://i.imgur.com/Eq0OK8z.png"
class="home-image1"
/>
</div>
</div>
</div>
"""
)
with gr.Row():
with gr.Column(scale=60):
with gr.Group():
with gr.Row():
prompt = gr.Textbox(label="Prompt", value="Portrait of Atatürk, fantasy, intricate, elegant, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration, artgerm and greg rutkowski and alphonse mucha",
examples=[
["What a beautiful morning for a walk!"],
["It was the best of times, it was the worst of times."],
],
show_label=False, max_lines=3,placeholder=f"{prefix} [your prompt]").style(container=True)
generate = gr.Button(value="ÜRET").style(rounded=(False, True, True, False))
image_out = gr.Image(height=512)
error_output = gr.Markdown()
with gr.Column(scale=40):
with gr.Tab("Seçenekler"):
with gr.Group():
neg_prompt = gr.Textbox(label="Negatif Girdi", placeholder="Çıkarılacak Girdiler", value="lowres, text, error, cropped, low quality, duplicate, mutilated, out of frame, extra fingers, mutated hands, mutation, deformed, blurry, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, missing arms, missing legs, fused fingers")
#auto_prefix = gr.Checkbox(label="Prefix styling tokens automatically (Atatürk)", value=prefix, visible=prefix)
with gr.Row():
guidance = gr.Slider(label="Sanat Ölçeği", value=7.5, maximum=15)
steps = gr.Slider(label="İşlem Adımı", value=25, minimum=2, maximum=75, step=1)
with gr.Row():
width = gr.Slider(label="Genişlik", value=512, minimum=64, maximum=1024, step=8)
height = gr.Slider(label="Yükseklik", value=512, minimum=64, maximum=1024, step=8)
seed = gr.Slider(0, 2147483647, label='Köken (0 = Rastgele)', value=0, step=1)
#with gr.Tab("Image to image"):
with gr.Group():
gr.Textbox(
label="Bilgiler",
lines=3,
value="Mustafa Kemal Atatürk'ün bilinen ve genç halini üretebileceğiniz bir yapay zeka modeli. Bilinen hali için Atatürk, genç hali için ise GençAtatürk yazabilirsiniz.",
)
#image2 = gr.Image(label="Image", height=256, tool="editor", type="pil")
#strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
#auto_prefix.change(lambda x: gr.update(placeholder=f"{prefix} [your prompt]" if x else "[Your prompt]"), inputs=auto_prefix, outputs=prompt, queue=False)
inputs = [prompt, guidance, steps, width, height, seed, neg_prompt]
outputs = [image_out, error_output]
prompt.submit(inference, inputs=inputs, outputs=outputs)
generate.click(inference, inputs=inputs, outputs=outputs)
gr.HTML("""
<div style="border-top: 1px solid #303030;" align="right">
<br> <p style="text-align: right;font-size: 160%;">Daha fazla bilgi için <a href="https://twitter.com/skbulous" target="_blank">SKB</a></p><br>
<p style="text-align: right;" ><a href="https://merzigo.com/"><img src="https://i.imgur.com/Gsifypg.png" align="right"></a>
<br><br><br>
<p style="text-align: right;">This space was created using <a href="https://huggingface.co/spaces/anzorq/sd-space-creator">SD Space Creator</a>.</p>
</div>
""")
demo.queue(concurrency_count=1)
demo.launch()