Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,96 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
import random
|
| 4 |
import torch
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
|
| 7 |
-
#
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
pipe = pipe.to(device)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
MAX_IMAGE_SIZE = 1024
|
| 18 |
-
|
| 19 |
-
# Define the custom model inference function
|
| 20 |
def custom_infer(
|
| 21 |
-
prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps
|
| 22 |
):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
custom_infer,
|
| 60 |
-
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 61 |
-
outputs=
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# Preloaded Gradio model
|
| 68 |
-
def preloaded_model_ui():
|
| 69 |
-
with gr.Blocks() as preloaded_demo:
|
| 70 |
-
gr.Markdown("## Preloaded Model: ZB-Tech Text-to-Image")
|
| 71 |
-
preloaded_demo = gr.load("models/ZB-Tech/Text-to-Image")
|
| 72 |
-
|
| 73 |
-
return preloaded_demo
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# Combine both interfaces in tabs
|
| 77 |
-
with gr.Blocks() as demo:
|
| 78 |
-
with gr.Tab("Custom Model"):
|
| 79 |
-
custom_ui = custom_model_ui()
|
| 80 |
-
|
| 81 |
-
with gr.Tab("Preloaded Model"):
|
| 82 |
-
preloaded_ui = preloaded_model_ui()
|
| 83 |
-
|
| 84 |
if __name__ == "__main__":
|
| 85 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import random
|
| 3 |
import torch
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
|
| 6 |
+
# Device setup
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
model_repo_id_turbo = "stabilityai/sdxl-turbo" # Stability AI Model
|
| 9 |
+
pipe_turbo = DiffusionPipeline.from_pretrained(model_repo_id_turbo, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32).to(device)
|
| 10 |
|
| 11 |
+
# Placeholder for ZB-Tech model
|
| 12 |
+
def load_zb_model():
|
| 13 |
+
return gr.Interface.load("models/ZB-Tech/Text-to-Image")
|
|
|
|
| 14 |
|
| 15 |
+
# Inference function
|
|
|
|
|
|
|
|
|
|
| 16 |
def custom_infer(
|
| 17 |
+
model_choice, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps
|
| 18 |
):
|
| 19 |
+
# Load the selected model
|
| 20 |
+
if model_choice == "Faster image generation (suitable for CPUs)":
|
| 21 |
+
model = load_zb_model()
|
| 22 |
+
return model(prompt)
|
| 23 |
+
else:
|
| 24 |
+
default_negative_prompt = "no watermark, hezzy, blurry"
|
| 25 |
+
combined_negative_prompt = f"{default_negative_prompt}, {negative_prompt}" if negative_prompt else default_negative_prompt
|
| 26 |
+
|
| 27 |
+
if randomize_seed:
|
| 28 |
+
seed = random.randint(0, np.iinfo(np.int32).max)
|
| 29 |
+
|
| 30 |
+
generator = torch.Generator().manual_seed(seed)
|
| 31 |
+
image = pipe_turbo(
|
| 32 |
+
prompt=prompt,
|
| 33 |
+
negative_prompt=combined_negative_prompt,
|
| 34 |
+
guidance_scale=guidance_scale,
|
| 35 |
+
num_inference_steps=num_inference_steps,
|
| 36 |
+
width=width,
|
| 37 |
+
height=height,
|
| 38 |
+
generator=generator,
|
| 39 |
+
).images[0]
|
| 40 |
+
return image, seed
|
| 41 |
+
|
| 42 |
+
# CSS for centering UI
|
| 43 |
+
css = """
|
| 44 |
+
#col-container {
|
| 45 |
+
display: flex;
|
| 46 |
+
flex-direction: column;
|
| 47 |
+
align-items: center;
|
| 48 |
+
justify-content: center;
|
| 49 |
+
text-align: center;
|
| 50 |
+
margin: 0 auto;
|
| 51 |
+
}
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
# Gradio app
|
| 55 |
+
with gr.Blocks(css=css) as demo:
|
| 56 |
+
with gr.Column(elem_id="col-container"):
|
| 57 |
+
# App name and description
|
| 58 |
+
gr.Markdown(
|
| 59 |
+
"""
|
| 60 |
+
# AI-Powered Text-to-Image Generator
|
| 61 |
+
*Generate stunning images from text prompts using advanced AI models.*
|
| 62 |
+
"""
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
# Dropdown for model selection
|
| 66 |
+
model_choice = gr.Dropdown(
|
| 67 |
+
label="Select Model",
|
| 68 |
+
choices=[
|
| 69 |
+
"Faster image generation (suitable for CPUs)",
|
| 70 |
+
"More customizable option (slower, suitable for GPUs)"
|
| 71 |
+
],
|
| 72 |
+
value="Faster image generation (suitable for CPUs)",
|
| 73 |
+
)
|
| 74 |
|
| 75 |
+
# Input section
|
| 76 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
|
| 77 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 78 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter a negative prompt here...")
|
| 79 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, value=0)
|
| 80 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 81 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1024, step=32, value=512)
|
| 82 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1024, step=32, value=512)
|
| 83 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, value=7.5)
|
| 84 |
+
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=25)
|
| 85 |
+
|
| 86 |
+
# Output section
|
| 87 |
+
result = gr.Image(label="Generated Image", type="pil")
|
| 88 |
+
gr.Button("Generate").click(
|
| 89 |
custom_infer,
|
| 90 |
+
inputs=[model_choice, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 91 |
+
outputs=result
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# Launch app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
if __name__ == "__main__":
|
| 96 |
demo.launch()
|