Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import DiffusionPipeline
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
get_caption = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base")
|
6 |
+
|
7 |
+
def captioner(input):
|
8 |
+
output = get_caption(input)
|
9 |
+
return output[0]['generated_text']
|
10 |
+
|
11 |
+
generate_pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
12 |
+
|
13 |
+
def generate(prompt):
|
14 |
+
return generate_pipeline(prompt).images[0]
|
15 |
+
|
16 |
+
def caption_and_generate(image):
|
17 |
+
caption = captioner(image)
|
18 |
+
image = generate(caption)
|
19 |
+
return [caption, image]
|
20 |
+
|
21 |
+
with gr.Blocks() as demo:
|
22 |
+
gr.Markdown("# Describe-and-Generate game 🖍️")
|
23 |
+
image_upload = gr.Image(label="Your first image",type="pil")
|
24 |
+
btn_all = gr.Button("Caption and generate")
|
25 |
+
caption = gr.Textbox(label="Generated caption")
|
26 |
+
image_output = gr.Image(label="Generated Image")
|
27 |
+
|
28 |
+
btn_all.click(fn=caption_and_generate, inputs=[image_upload], outputs=[caption, image_output])
|
29 |
+
|
30 |
+
demo.launch()
|