bdc-divya commited on
Commit
d5b07c7
·
1 Parent(s): 225c89b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ from diffusers import StableDiffusionPipeline # Import StableDiffusionPipeline for image generation
3
+ import torch # Import PyTorch for deep learning operations
4
+ import gradio as gr # Import Gradio for creating a web interface
5
+
6
+ # Define configuration parameters
7
+ class CFG:
8
+ image_gen_steps = 35 # Number of steps for image generation
9
+ image_gen_model_id = "stabilityai/stable-diffusion-2" # ID of the StableDiffusion model
10
+ image_gen_size = (400, 400) # Size of the generated image
11
+ image_gen_guidance_scale = 9 # Guidance scale for image generation
12
+
13
+ # Load the StableDiffusion model
14
+ image_gen_model = StableDiffusionPipeline.from_pretrained(
15
+ CFG.image_gen_model_id,
16
+ revision="fp16",
17
+ guidance_scale=9
18
+ )
19
+
20
+ # Define a function for image generation
21
+ def generate_image(prompt):
22
+ # Generate an image from a text prompt using the loaded model
23
+ image = image_gen_model(
24
+ prompt,
25
+ num_inference_steps=CFG.image_gen_steps,
26
+ guidance_scale=CFG.image_gen_guidance_scale
27
+ ).images[0]
28
+
29
+ # Resize the generated image to the specified size
30
+ image = image.resize(CFG.image_gen_size)
31
+ return image # Return the generated image as the result
32
+
33
+ # Define a Gradio interface
34
+ iface = gr.Interface(
35
+ fn=generate_image, # Use the generate_image function for processing input
36
+ inputs="text", # Accept text input from the user
37
+ outputs="image", # Display the generated image as output
38
+ title="StableDiffusion Image Generation", # Set the title of the web interface
39
+ description="Generate images from text prompts using StableDiffusion model.", # Provide a description
40
+ live=False # Set to False if you don't want real-time updates (for beginner-friendly interaction)
41
+ )
42
+
43
+ # Start the Gradio interface
44
+ iface.launch(debug=True)