File size: 2,426 Bytes
f2c0d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
import os

title="Prompt Converter"

description="""
<p style="text-align:center;">
Stable Diffusion 2 uses OpenCLIP ViT-H model trained on LAION dataset so it knows different things than the OpenAI ViT-L we're all used to prompting.
<br />This demo converts a v1.x stable diffusion prompt to a stable diffusion 2.x prompt, 
<br />by generating an image through <a href="https://huggingface.co/runwayml/stable-diffusion-v1-5" target="_blank">RunwayML Stable Diffusion 1.5</a>, then Interrogate the resulting image through <a href="https://huggingface.co/spaces/fffiloni/CLIP-Interrogator-2" target="_blank">CLIP Interrogator 2</a> to give you a Stable Diffusion 2 equivalent prompt.
</p>
"""

stable_diffusion = gr.load(name="spaces/runwayml/stable-diffusion-v1-5")
clip_interrogator_2 = gr.load(name="spaces/fffiloni/CLIP-Interrogator-2")

def get_images(prompt):
    gallery_dir = stable_diffusion(prompt, fn_index=2)    
    img_results = [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)]  
    return img_results[0]

def get_new_prompt(img, mode):
    interrogate = clip_interrogator_2(img, mode, 12, api_name="clipi2")
    return interrogate

def infer(prompt, mode):
    img = get_images(prompt) 
    result = get_new_prompt(img, mode)    
    return result[0]
    
prompt_input = gr.Textbox(lines=4, label="Input v1.x Stable Diffusion prompt")
mode_input = gr.Radio(['best', 'classic', 'fast'], label='mode', value='fast')
prompt_output = gr.Textbox(lines=4, label="Converted v2.x Stable Diffusion prompt")

examples=[
    ["girl with steampunk weapons and uniform, serious, finely detailed, made by wlop, boichi, ilya kuvshinov, full body portrait, illustration, grass, sunny, sky, anime, side view, perfect anime face, detailed face, zoomed out, smooth","fast"],
    ["a yellow cockatiel riding on the rings of saturn wearing a propeller hat, fantasy, intricate, elegant, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration, art by artgerm and greg rutkowski and alphonse mucha ","classic"],
    ["painting, view from inside edward hopper's painting nighthawks, of a group of werebears robbing a bank, foggy ","best"]
]

demo=gr.Interface(fn=infer, inputs=[prompt_input,mode_input], outputs=[prompt_output],title=title,description=description,examples=examples)
demo.queue(max_size=10,concurrency_count=20)
demo.launch(enable_queue=True)