Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,47 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from diffusers import DiffusionPipeline
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import DiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 6 |
+
|
| 7 |
+
# Pre-load the models but do not load them yet
|
| 8 |
+
models = {
|
| 9 |
+
"ArtifyAI v1.1": "ImageInception/ArtifyAI-v1.1",
|
| 10 |
+
"ArtifyAI v1.0": "ImageInception/ArtifyAI-v1.0"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
# Function to load the selected model
|
| 14 |
+
def load_model(model_name):
|
| 15 |
+
return DiffusionPipeline.from_pretrained(models[model_name], torch_dtype=torch.float16 if device == "cuda" else torch.float32).to(device)
|
| 16 |
+
|
| 17 |
+
# Initially load the first model
|
| 18 |
+
pipe = load_model("ArtifyAI v1.1")
|
| 19 |
+
|
| 20 |
+
def generate_image(prompt, selected_model):
|
| 21 |
+
global pipe
|
| 22 |
+
|
| 23 |
+
# Load the selected model if it's not already loaded
|
| 24 |
+
if selected_model in models:
|
| 25 |
+
pipe = load_model(selected_model)
|
| 26 |
+
|
| 27 |
+
if device == "cuda":
|
| 28 |
+
with torch.cuda.amp.autocast():
|
| 29 |
+
image = pipe(prompt).images[0]
|
| 30 |
+
else:
|
| 31 |
+
image = pipe(prompt).images[0]
|
| 32 |
+
return image
|
| 33 |
+
|
| 34 |
+
# Gradio interface
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=generate_image,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Textbox(label="Enter your prompt"),
|
| 39 |
+
gr.Dropdown(choices=list(models.keys()), label="Select Model", value="ArtifyAI v1.1")
|
| 40 |
+
],
|
| 41 |
+
outputs=gr.Image(type="pil"),
|
| 42 |
+
title="ArtifyAI",
|
| 43 |
+
description="Generate images using the selected model."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|