Text-Image / app.py
saikub's picture
Create app.py
8df578c
raw
history blame
1.84 kB
import gradio as gr
from diffusers import DiffusionPipeline
# Load the pre-trained model
pipeline = DiffusionPipeline.from_pretrained("stablediffusionapi/juggernaut-xl-v5")
# Load the LORA weights
pipeline.load_lora_weights("openskyml/dalle-3-xl")
# Define a function to generate images
def generate_image(text):
# Encode the text using the LORA model
input_ids = pipeline.encode_plus(
text,
add_special_tokens=True,
max_length=512,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt'
)
# Generate an image using the encoded input
image = pipeline.generate(
input_ids,
attention_mask=input_ids.attention_mask,
max_length=512,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt'
)
# Decode the image
image = pipeline.decode(image, skip_special_tokens=True)
return image
# Create a Gradio interface
interface = gr.Interface(
title='Text-to-Image App',
description='Generate images from text using a pre-trained diffusion model',
input_widget=gr.TextInput(
label='Enter text',
placeholder='Type some text here'
),
output_widget=gr.ImageOutput(
label='Generated image'
),
submit_button=gr.Button(
label='Generate image'
)
)
# Define a callback function to handle user input
def handle_input(text):
# Generate an image using the generate_image function
image = generate_image(text)
# Display the generated image
interface.output_widget.set_image(image)
return image
# Set up the Gradio interface
interface.attach_handlers(
input_widget=handle_input,
submit_button=handle_input
)
# Run the Gradio interface
interface.run()