Ashish08 commited on
Commit
85dbbb2
1 Parent(s): 48b5537

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -5
app.py CHANGED
@@ -1,23 +1,59 @@
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")
 
 
1
  from diffusers import DiffusionPipeline
2
+
3
+ import gradio as gr
4
+
5
+ from PIL import Image
6
+
7
  from transformers import pipeline
8
 
9
+ # Initialize Caption Generation Model
10
  get_caption = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base")
11
 
12
+ # Initialize Image Generation Model
13
+ generate_pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
14
+
15
+ def captioner(input: Image.Image) -> str:
16
+ """
17
+ Generate a descriptive caption for the given input image using the BLIP-IMAGE-CAPTIONING-BASE model.
18
+
19
+ Args:
20
+ input (Image.Image): The input image for which to generate a caption.
21
+
22
+ Returns:
23
+ str: The generated caption describing the input image.
24
+ """
25
  output = get_caption(input)
26
  return output[0]['generated_text']
27
 
28
+ def generate(prompt: str) -> Image.Image:
29
+ """
30
+ Generate an image based on the given textual prompt using the Stable Diffusion model.
31
+
32
+ Args:
33
+ prompt (str): The textual description based on which to generate an image.
34
 
35
+ Returns:
36
+ Image.Image: The generated image corresponding to the given prompt.
37
+ """
38
  return generate_pipeline(prompt).images[0]
39
 
40
+ @spaces.GPU(duration=300)
41
+ def caption_and_generate(image: Image.Image) -> list:
42
+ """
43
+ Generate a caption for the given image and then generate a new image based on that caption.
44
+
45
+ Args:
46
+ image (Image.Image): The input image for which to generate a caption and subsequently a new image.
47
+
48
+ Returns:
49
+ list: A list containing the generated caption (str) and the generated image (Image.Image).
50
+ """
51
  caption = captioner(image)
52
  image = generate(caption)
53
  return [caption, image]
54
 
55
+ ####### GRADIO APP #######
56
+
57
  with gr.Blocks() as demo:
58
  gr.Markdown("# Describe-and-Generate game 🖍️")
59
  image_upload = gr.Image(label="Your first image",type="pil")