Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,58 @@ import torch
|
|
| 3 |
from diffusers import StableDiffusionPipeline, UNet2DConditionModel
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
|
| 7 |
model_id = "UX4567/Stable-Diffusion-v1-3.0"
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
| 10 |
print("Model load ho raha hai...")
|
| 11 |
-
|
| 12 |
trained_unet = UNet2DConditionModel.from_pretrained(
|
| 13 |
-
model_id,
|
| 14 |
torch_dtype=torch.float16
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# 2. Initialize pipeline with runwayml base and your custom UNet
|
| 18 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 19 |
"runwayml/stable-diffusion-v1-5",
|
| 20 |
unet=trained_unet,
|
| 21 |
-
torch_dtype=torch.float16
|
|
|
|
| 22 |
).to(device)
|
| 23 |
|
| 24 |
-
# 3. Generation function with GPU decorator
|
| 25 |
@spaces.GPU
|
| 26 |
def generate_image(prompt):
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return image
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
demo.launch()
|
| 41 |
-
|
|
|
|
| 3 |
from diffusers import StableDiffusionPipeline, UNet2DConditionModel
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
| 6 |
model_id = "UX4567/Stable-Diffusion-v1-3.0"
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
print("Model load ho raha hai...")
|
| 10 |
+
|
| 11 |
trained_unet = UNet2DConditionModel.from_pretrained(
|
| 12 |
+
model_id,
|
| 13 |
torch_dtype=torch.float16
|
| 14 |
)
|
| 15 |
|
|
|
|
| 16 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 17 |
"runwayml/stable-diffusion-v1-5",
|
| 18 |
unet=trained_unet,
|
| 19 |
+
torch_dtype=torch.float16,
|
| 20 |
+
safety_checker=None # thoda fast hoga
|
| 21 |
).to(device)
|
| 22 |
|
|
|
|
| 23 |
@spaces.GPU
|
| 24 |
def generate_image(prompt):
|
| 25 |
+
if not prompt.strip():
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
# Quality booster
|
| 29 |
+
enhanced_prompt = f"{prompt}, ultra sharp, highly detailed, cinematic lighting, 8k"
|
| 30 |
+
negative_prompt = "blurry, low quality, low-res, distorted, ugly, bad anatomy, watermark, text, deformed"
|
| 31 |
+
|
| 32 |
+
image = pipe(
|
| 33 |
+
prompt=enhanced_prompt,
|
| 34 |
+
negative_prompt=negative_prompt,
|
| 35 |
+
num_inference_steps=35,
|
| 36 |
+
guidance_scale=8.5, # 7.5 se 8.5 - thoda zyada sharp
|
| 37 |
+
width=512,
|
| 38 |
+
height=768 # tere cyberpunk wale ke liye vertical better lagega
|
| 39 |
+
).images[0]
|
| 40 |
return image
|
| 41 |
|
| 42 |
+
# Better UI - tere screenshot jaisa
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# Calico Image Generator\nApne fine-tuned Stable Diffusion model se creative images generate karein!")
|
| 45 |
+
|
| 46 |
+
with gr.Group():
|
| 47 |
+
prompt_box = gr.Textbox(label="Enter your prompt", value="a futuristic cyberpunk city at night", lines=2)
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
clear_btn = gr.Button("Clear")
|
| 51 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 52 |
+
|
| 53 |
+
output_img = gr.Image(label="Generated Image")
|
| 54 |
+
|
| 55 |
+
gr.Markdown("Share via Link se share kar sakta hai tu!")
|
| 56 |
+
|
| 57 |
+
submit_btn.click(fn=generate_image, inputs=prompt_box, outputs=output_img)
|
| 58 |
+
clear_btn.click(lambda: ("", None), inputs=None, outputs=[prompt_box, output_img])
|
| 59 |
|
| 60 |
+
demo.launch()
|
|
|
|
|
|