t2i-custom / app.py
Hev832's picture
Update app.py
045d91e verified
raw
history blame
1.65 kB
import gradio as gr
from diffusers import DiffusionPipeline
# Initialize the pipeline variable globally
pipeline = None
# Load the pipeline and LoRA weights
def load_cust(modelsyu, progress=gr.Progress()):
progress(10.4, desc="loading your lora models...")
global pipeline
pipeline = DiffusionPipeline.from_pretrained(modelsyu)
# Ensure the pipeline uses the CPU
pipeline.to("cpu")
return "Models loaded successfully"
def generate_image(prompt, negative_prompt):
global pipeline
# Generate the image with the provided prompts
if pipeline is None:
return "Pipeline not loaded. Please load the models first."
# Make sure the pipeline is set to use the CPU
pipeline.to("cpu")
image = pipeline(prompt, negative_prompt=negative_prompt).images[0]
return image
# Define the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Text to Image Generation Custom models Demo")
prompt = gr.Textbox(label="Prompt", placeholder="Enter your text prompt here")
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter your negative prompt here")
submit_button = gr.Button("Generate Image")
with gr.Accordion('Load your custom models first'):
basem = gr.Textbox(label="Your Lora model")
exports = gr.Button("Load your models")
outputid = gr.Textbox(label="Output", interactive=False)
exports.click(load_cust, inputs=[basem], outputs=[outputid])
output_image = gr.Image(label="Generated Image")
submit_button.click(generate_image, inputs=[prompt, negative_prompt], outputs=output_image)
# Launch the demo
demo.launch()