rohitr01 commited on
Commit
002bf33
·
verified ·
1 Parent(s): d4c4d41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -11
app.py CHANGED
@@ -1,16 +1,132 @@
 
 
 
 
 
 
1
  import torch
2
- from diffusers import StableDiffusionPipeline
3
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Load model (use float32 for CPU)
6
- pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32)
7
- pipe.to("cpu")
8
 
9
- # Image generation function
10
- def generate_image(prompt):
11
- image = pipe(prompt).images[0]
12
- return image
13
 
14
- # Gradio Interface
15
- demo = gr.Interface(fn=generate_image, inputs="text", outputs="image")
16
- demo.launch()
 
1
+ # Ensure necessary packages are installed
2
+ !pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 # Install specific version of torchvision+
3
+ !pip install git+https://github.com/huggingface/diffusers.git # Install latest diffusers
4
+ !pip install torch gradio basicsr realesrgan Pillow diffusers transformers accelerate safetensors # Install dependencies
5
+
6
+ # Import necessary libraries
7
  import torch
 
8
  import gradio as gr
9
+ import webbrowser
10
+ from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
11
+ from PIL import Image
12
+
13
+ # Check if GPU is available
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+ if device == "cpu":
16
+ print("⚠️ Warning: Running on CPU, performance may be slow.")
17
+
18
+ # Load Text-to-Image model
19
+ print("🔄 Loading Stable Diffusion txt2img model...")
20
+ pipe_txt2img = StableDiffusionPipeline.from_pretrained(
21
+ "runwayml/stable-diffusion-v1-5",
22
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
23
+ ).to(device)
24
+ print("✅ Text-to-Image model loaded!")
25
+
26
+ # Load Image-to-Image model
27
+ print("🔄 Loading Stable Diffusion img2img model...")
28
+ pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
29
+ "runwayml/stable-diffusion-v1-5",
30
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
31
+ ).to(device)
32
+ print("✅ Image-to-Image model loaded!")
33
+
34
+ # Function to generate images from text
35
+ def generate_txt2img(prompt, steps=50, guidance=7.5, width=512, height=512, seed=-1, save_format="png"):
36
+ generator = torch.manual_seed(seed) if seed != -1 else None
37
+ image = pipe_txt2img(
38
+ prompt, num_inference_steps=steps, guidance_scale=guidance, width=width, height=height,
39
+ generator=generator
40
+ ).images[0]
41
+
42
+ output_path = f"generated_image.{save_format}" # Save image in the requested format
43
+ image.save(output_path, format=save_format.upper()) # Save in the selected format
44
+ print(f"Image saved to {output_path}")
45
+ return output_path
46
+
47
+ # Function to transform images using img2img
48
+ def generate_img2img(prompt, image, strength=0.5, steps=50, guidance=7.5, width=512, height=512, seed=-1, save_format="png"):
49
+ generator = torch.manual_seed(seed) if seed != -1 else None
50
+ image = pipe_img2img(
51
+ prompt, image=image, strength=strength, num_inference_steps=steps, guidance_scale=guidance,
52
+ width=width, height=height, generator=generator
53
+ ).images[0]
54
+
55
+ output_path = f"modified_image.{save_format}" # Save image in the requested format
56
+ image.save(output_path, format=save_format.upper()) # Save in the selected format
57
+ print(f"Image saved to {output_path}")
58
+ return output_path
59
+
60
+ # Define Gradio UI
61
+ def create_ui():
62
+ with gr.Blocks(title="DiffuGen: AI Image Generation") as demo:
63
+ gr.Markdown("# 🌟 DiffuGen - AI Image Generator")
64
+
65
+ # Text-to-Image Tab
66
+ with gr.Tab("📷 Text to Image"):
67
+ with gr.Row():
68
+ prompt = gr.Textbox(label="Enter a text prompt")
69
+
70
+ with gr.Row():
71
+ steps = gr.Slider(10, 100, value=50, step=10, label="Steps")
72
+ guidance = gr.Slider(1, 15, value=7.5, label="Guidance Scale")
73
+
74
+ with gr.Row():
75
+ width = gr.Slider(256, 1024, value=512, step=64, label="Width")
76
+ height = gr.Slider(256, 1024, value=512, step=64, label="Height")
77
+ seed = gr.Number(value=-1, label="Seed (-1 for random)")
78
+
79
+ with gr.Row():
80
+ save_format = gr.Dropdown(
81
+ choices=["png", "jpg"], value="png", label="Select Image Format"
82
+ )
83
+
84
+ generate_btn = gr.Button("🚀 Generate Image")
85
+ output_image = gr.Image(label="Generated Image", type="pil")
86
+
87
+ generate_btn.click(
88
+ generate_txt2img,
89
+ inputs=[prompt, steps, guidance, width, height, seed, save_format],
90
+ outputs=output_image
91
+ )
92
+
93
+ # Image-to-Image Tab
94
+ with gr.Tab("🖼️ Image to Image"):
95
+ with gr.Row():
96
+ prompt_img2img = gr.Textbox(label="Enter a prompt")
97
+
98
+ with gr.Row():
99
+ input_img = gr.Image(label="Upload Image", type="pil")
100
+
101
+ with gr.Row():
102
+ strength = gr.Slider(0.1, 1.0, value=0.5, label="Denoising Strength")
103
+ steps_img2img = gr.Slider(10, 100, value=50, label="Steps")
104
+ guidance_img2img = gr.Slider(1, 15, value=7.5, label="Guidance Scale")
105
+
106
+ with gr.Row():
107
+ width_img2img = gr.Slider(256, 1024, value=512, step=64, label="Width")
108
+ height_img2img = gr.Slider(256, 1024, value=512, step=64, label="Height")
109
+ seed_img2img = gr.Number(value=-1, label="Seed (-1 for random)")
110
+
111
+ with gr.Row():
112
+ save_format_img2img = gr.Dropdown(
113
+ choices=["png", "jpg"], value="png", label="Select Image Format"
114
+ )
115
+
116
+ generate_img_btn = gr.Button("🔄 Transform Image")
117
+ output_img2img = gr.Image(label="Modified Image", type="pil")
118
+
119
+ generate_img_btn.click(
120
+ generate_img2img,
121
+ inputs=[prompt_img2img, input_img, strength, steps_img2img, guidance_img2img, width_img2img, height_img2img, seed_img2img, save_format_img2img],
122
+ outputs=output_img2img
123
+ )
124
 
125
+ return demo
 
 
126
 
127
+ # Launch Gradio WebUI
128
+ web_ui = create_ui()
129
+ url = web_ui.launch(share=True)
 
130
 
131
+ # Automatically open the WebUI in a new browser tab
132
+ webbrowser.open(url)