kashifaleem commited on
Commit
b2564a3
·
verified ·
1 Parent(s): 08af80c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline
4
+ import os
5
+
6
+ # Set the model ID
7
+ MODEL_ID = "runwayml/stable-diffusion-v1-5"
8
+
9
+ # Detect hardware
10
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
11
+ # Use float16 for faster, lighter inference if on GPU
12
+ TORCH_DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
13
+
14
+ # Load the pipeline
15
+ print(f"Loading model on {DEVICE}...")
16
+ pipe = StableDiffusionPipeline.from_pretrained(
17
+ MODEL_ID,
18
+ torch_dtype=TORCH_DTYPE,
19
+ use_safetensors=True
20
+ )
21
+ pipe = pipe.to(DEVICE)
22
+
23
+ def generate_image(prompt, negative_prompt):
24
+ if not prompt:
25
+ return None
26
+
27
+ try:
28
+ # Generate the image
29
+ image = pipe(
30
+ prompt=prompt,
31
+ negative_prompt=negative_prompt,
32
+ num_inference_steps=30,
33
+ guidance_scale=7.5
34
+ ).images[0]
35
+ return image
36
+ except Exception as e:
37
+ print(f"Error: {e}")
38
+ return None
39
+
40
+ # Build the UI
41
+ with gr.Blocks(theme=gr.themes.Base()) as demo:
42
+ gr.Markdown("# ✨ AI Image Generator")
43
+ gr.Markdown("Enter a prompt and click generate to create an image.")
44
+
45
+ with gr.Row():
46
+ with gr.Column():
47
+ prompt = gr.Textbox(label="Prompt", placeholder="A majestic lion in the jungle...")
48
+ neg_prompt = gr.Textbox(label="Negative Prompt", placeholder="blurry, low quality, distorted")
49
+ btn = gr.Button("Generate", variant="primary")
50
+
51
+ with gr.Column():
52
+ output = gr.Image(label="Result")
53
+
54
+ btn.click(fn=generate_image, inputs=[prompt, neg_prompt], outputs=output)
55
+
56
+ if __name__ == "__main__":
57
+ demo.launch()