Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import StableDiffusionPipeline
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
print("Loading the Stable Diffusion model (this may take a minute)...")
|
6 |
+
pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
7 |
+
pipeline.to("cuda")
|
8 |
+
print("Model loaded successfully!")
|
9 |
+
|
10 |
+
def generate_image(prompt, guidance_scale):
|
11 |
+
"""
|
12 |
+
Generate an image from a text prompt using Stable Diffusion.
|
13 |
+
|
14 |
+
Args:
|
15 |
+
prompt (str): The text prompt for generating the image.
|
16 |
+
guidance_scale (float): Controls how closely the image matches the prompt.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
PIL.Image.Image: The generated image.
|
20 |
+
"""
|
21 |
+
if not prompt.strip():
|
22 |
+
return "Error: Prompt cannot be empty!"
|
23 |
+
try:
|
24 |
+
image = pipeline(prompt, guidance_scale=guidance_scale).images[0]
|
25 |
+
return image
|
26 |
+
except Exception as e:
|
27 |
+
return f"Error: {str(e)}"
|
28 |
+
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("# 🖼 Text-to-Image Generator by Abhinav ")
|
31 |
+
gr.Markdown("Enter a text description below to generate an image.")
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
prompt_input = gr.Textbox(label="Enter Image Prompt", placeholder="e.g., A beautiful place in Kerala", lines=2)
|
35 |
+
guidance_slider = gr.Slider(label="Guidance Scale", minimum=5.0, maximum=15.0, value=7.5, step=0.5)
|
36 |
+
|
37 |
+
generate_button = gr.Button("Generate Image")
|
38 |
+
output_image = gr.Image(label="Generated Image")
|
39 |
+
|
40 |
+
generate_button.click(
|
41 |
+
fn=generate_image,
|
42 |
+
inputs=[prompt_input, guidance_slider],
|
43 |
+
outputs=output_image
|
44 |
+
)
|
45 |
+
demo.launch()
|