Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,106 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
img_to_text = gr.Interface.load("spaces/pharma/CLIP-Interrogator")
|
| 3 |
-
|
| 4 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
|
| 5 |
img_to_text = gr.Interface.load("spaces/pharma/CLIP-Interrogator")
|
| 6 |
+
|
| 7 |
+
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
|
| 8 |
+
|
| 9 |
+
def get_images(prompt):
|
| 10 |
+
gallery_dir = stable_diffusion(prompt, fn_index=2)
|
| 11 |
+
return [os.path.join(gallery_dir, image) for image in os.listdir(gallery_dir)]
|
| 12 |
+
|
| 13 |
+
def get_prompts(uploaded_image):
|
| 14 |
+
return img_to_text(uploaded_image)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
with gr.Column():
|
| 19 |
+
gr.Markdown(
|
| 20 |
+
"""
|
| 21 |
+
## Stable Diffusion Perception ππ
|
| 22 |
+
Input an image and see how the model perceives it! π
|
| 23 |
+
"""
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
gr.Markdown(
|
| 27 |
+
"""
|
| 28 |
+
Upload your image below π
|
| 29 |
+
"""
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
with gr.Column():
|
| 33 |
+
input_img = gr.Image(
|
| 34 |
+
type="filepath",
|
| 35 |
+
)
|
| 36 |
+
with gr.Row():
|
| 37 |
+
translate_img = gr.Button("Upload Image!", elem_id = "img2text")
|
| 38 |
+
see_prompts = gr.Button("Check how your image prompts your model!", elem_id="check_btn_1")
|
| 39 |
+
#translate_img_directly = gr.Button("Translate your image now.", elem_id="translate_img")
|
| 40 |
+
|
| 41 |
+
with gr.Accordion(label="Stable Diffusion Settings", elem_id="sd_settings", visible=False):
|
| 42 |
+
with gr.Row():
|
| 43 |
+
guidance_scale = gr.Slider(2, 15, value = 7, label = 'Guidance Scale')
|
| 44 |
+
nb_iterations = gr.Slider(10, 50, value = 25, step = 1, label = 'Steps')
|
| 45 |
+
seed = gr.Slider(label = "Seed", minimum = 0, maximum = 2147483647, step = 1, randomize = True)
|
| 46 |
+
|
| 47 |
+
gr.Markdown(
|
| 48 |
+
"""
|
| 49 |
+
## Check what the model thinks of the image π
|
| 50 |
+
"""
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
with gr.Column():
|
| 54 |
+
img2text_output = gr.Textbox(
|
| 55 |
+
label="Convert your image to text!",
|
| 56 |
+
lines=4,
|
| 57 |
+
elem_id="translated"
|
| 58 |
+
)
|
| 59 |
+
with gr.Row():
|
| 60 |
+
clear_btn = gr.Button(value="Clear")
|
| 61 |
+
diffuse_btn = gr.Button(value="Diffuse it!", elem_id="diffuse_btn")
|
| 62 |
+
|
| 63 |
+
clear_btn.click(fn=lambda value: gr.update(value=""), inputs=clear_btn, outputs=img2text_output)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
gr.Markdown("""
|
| 68 |
+
## 3. Diffuse it! π§βπ¨
|
| 69 |
+
"""
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
sd_output = gr.Gallery().style(grid=2, height="auto")
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
see_prompts.click(img_to_text,
|
| 76 |
+
inputs = input_img,
|
| 77 |
+
outputs = [
|
| 78 |
+
img2text_output
|
| 79 |
+
])
|
| 80 |
+
|
| 81 |
+
def translate_directly(img):
|
| 82 |
+
images = get_images(get_prompts(img))
|
| 83 |
+
return images
|
| 84 |
+
|
| 85 |
+
"""
|
| 86 |
+
translate_img_directly.click(translate_directly,
|
| 87 |
+
inputs = [
|
| 88 |
+
input_img
|
| 89 |
+
],
|
| 90 |
+
outputs = [
|
| 91 |
+
img2text_output,
|
| 92 |
+
sd_output
|
| 93 |
+
])
|
| 94 |
+
|
| 95 |
+
"""
|
| 96 |
+
diffuse_btn.click(get_images,
|
| 97 |
+
inputs = [
|
| 98 |
+
img2text_output
|
| 99 |
+
],
|
| 100 |
+
outputs = sd_output
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
demo.launch()
|