Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from diffusers import
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
# Function to automatically switch between GPU and CPU
|
| 6 |
-
def load_model(base_model_id, adapter_model_id):
|
| 7 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
-
info = f"Running on {'GPU (CUDA) 🔥' if device == 'cuda' else 'CPU 🥶'}"
|
| 9 |
-
|
| 10 |
-
try:
|
| 11 |
-
# Load the base model dynamically on the correct device
|
| 12 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
| 13 |
-
base_model_id,
|
| 14 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 15 |
-
).to(device)
|
| 16 |
-
|
| 17 |
-
# If an adapter model is provided, load and merge the adapter model
|
| 18 |
-
if adapter_model_id:
|
| 19 |
-
adapter_pipe = DiffusionPipeline.from_pretrained(adapter_model_id)
|
| 20 |
-
adapter_pipe.load_lora_weights(base_model_id)
|
| 21 |
-
pipe = pipe.to(device)
|
| 22 |
|
| 23 |
-
return pipe, info
|
| 24 |
-
except Exception as e:
|
| 25 |
-
return None, f"Error loading model: {str(e)}"
|
| 26 |
|
| 27 |
-
# Function
|
| 28 |
-
def generate_image(
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
# Generate image
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return image, info
|
| 38 |
-
except Exception as e:
|
| 39 |
-
return None, f"Error generating image: {str(e)}"
|
| 40 |
|
| 41 |
-
# Create
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
with gr.Row():
|
| 46 |
-
|
| 47 |
-
base_model_id = gr.Textbox(
|
| 48 |
-
label="Enter Base Model ID (e.g., CompVis/stable-diffusion-v1-4)",
|
| 49 |
-
placeholder="Base Model ID"
|
| 50 |
-
)
|
| 51 |
-
adapter_model_id = gr.Textbox(
|
| 52 |
-
label="Enter Adapter Model ID (optional, e.g., nevreal/vMurderDrones-Lora)",
|
| 53 |
-
placeholder="Adapter Model ID (optional)",
|
| 54 |
-
value=""
|
| 55 |
-
)
|
| 56 |
-
prompt = gr.Textbox(
|
| 57 |
-
label="Enter your prompt",
|
| 58 |
-
placeholder="Describe the image you want to generate"
|
| 59 |
-
)
|
| 60 |
-
generate_btn = gr.Button("Generate Image")
|
| 61 |
-
|
| 62 |
-
with gr.Column():
|
| 63 |
-
output_image = gr.Image(label="Generated Image")
|
| 64 |
-
device_info = gr.Markdown() # To display device info and any error messages
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
# Launch the
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import DiffusionPipeline
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Function to generate image based on input text
|
| 7 |
+
def generate_image(prompt):
|
| 8 |
+
# Load the pipeline
|
| 9 |
|
| 10 |
+
pipeline = DiffusionPipeline.from_pretrained("John6666/mala-anime-mix-nsfw-pony-xl-v3-sdxl")
|
| 11 |
+
pipeline.load_lora_weights("nevreal/vMurderDrones")
|
| 12 |
|
| 13 |
+
# Generate the image using the text prompt
|
| 14 |
+
image = pipeline(prompt).images[0]
|
| 15 |
+
return image
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Create Gradio interface
|
| 18 |
with gr.Blocks() as demo:
|
| 19 |
+
# Title
|
| 20 |
+
gr.Markdown("# Text-to-Image Generation WebUI")
|
| 21 |
+
|
| 22 |
+
# Input for text prompt
|
| 23 |
with gr.Row():
|
| 24 |
+
prompt = gr.Textbox(label="Enter your prompt here", placeholder="Type your text prompt...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Output image display
|
| 27 |
+
output_image = gr.Image(label="Generated Image")
|
| 28 |
+
|
| 29 |
+
# Button to trigger the image generation
|
| 30 |
+
generate_button = gr.Button("Generate Image")
|
| 31 |
+
|
| 32 |
+
# When the button is clicked, call the generate_image function
|
| 33 |
+
generate_button.click(fn=generate_image, inputs=prompt, outputs=output_image)
|
| 34 |
|
| 35 |
+
# Launch the interface
|
| 36 |
demo.launch()
|