Falln87 commited on
Commit
f21f9ea
·
verified ·
1 Parent(s): 47e7bdd

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +55 -0
App.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import snapshot_download
3
+ from diffusers import StableDiffusion
4
+ from gradio_tools import StableDiffusionTool
5
+
6
+ # Download the model files
7
+ snapshot_download("model-repository/stable-diffusion", "main", "model", "1.5")
8
+
9
+ # Load the Stable Diffusion model
10
+ model = StableDiffusion.from_pretrained("stable-diffusion")
11
+
12
+ # Define a function to generate images and captions
13
+ def generate_image_and_caption(prompt, guidance_scale=7.5):
14
+ # Generate an image from the prompt
15
+ image = model.generate_from_prompt(
16
+ prompt, guidance_scale=guidance_scale, num_images=1, batch_size=1
17
+ )
18
+
19
+ # Improve the prompt and generate a caption
20
+ improved_prompt = "A photo of " + prompt
21
+ caption = model.caption_image(improved_prompt, image[0])
22
+
23
+ return image[0], caption
24
+
25
+ # Create a StableDiffusionTool instance
26
+ stable_diffusion_tool = StableDiffusionTool(model)
27
+
28
+ # Create the Gradio interface
29
+ interface = gr.Interface(
30
+ fn=generate_image_and_caption,
31
+ inputs=[
32
+ gr.inputs.Textbox(lines=5, label="Image Prompt"),
33
+ gr.inputs.Slider(
34
+ minimum=0, maximum=10, default=7.5, step=0.5, label="Guidance Scale"
35
+ ),
36
+ ],
37
+ outputs=["image", "text"],
38
+ allow_flagging=True,
39
+ flagging_options=["Inappropriate"],
40
+ title="Stable Diffusion Gradio App",
41
+ description="Generate images and captions from text prompts using Stable Diffusion.",
42
+ article="This Gradio app showcases the capabilities of Stable Diffusion, a state-of-the-art defusion model. You can generate images and their captions by providing text prompts and adjusting the guidance scale.",
43
+ examples=[
44
+ ["A red apple on a table", 7.5],
45
+ ["A sunset over a beach", 5.0],
46
+ ["A dog riding a skateboard", 8.0],
47
+ ],
48
+ show_input=True,
49
+ enable_queue=True,
50
+ queue_message="Generating image and caption. Please wait...",
51
+ tools=[stable_diffusion_tool],
52
+ )
53
+
54
+ # Launch the app
55
+ interface.launch()