svjack's picture
Update app.py
d185329 verified
raw
history blame
2.01 kB
# Import necessary libraries
import gradio as gr
import diffusers
import torch
# Define the function to generate an image from text using the Liberata/illustrious-xl-v1.0 model
def generate_image(prompt, guidance_scale, num_inference_steps):
# Load the model from the specified file
model = diffusers.StableDiffusionXLPipeline.from_pretrained(
"Liberata/illustrious-xl-v1.0",
torch_dtype=torch.float16
)
# Generate the image from the text prompt with the specified parameters
image = model(prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps).images[0]
return image
# Create a Gradio interface for the text-to-image generation
with gr.Blocks() as demo:
# Create a textbox for the user to input the text prompt
prompt_input = gr.Textbox(label="Text Prompt")
# Create sliders for the guidance scale and number of inference steps
guidance_scale_slider = gr.Slider(1, 20, value=7.5, step=0.1, label="Guidance Scale")
num_inference_steps_slider = gr.Slider(1, 100, value=50, step=1, label="Number of Inference Steps")
# Create an image output to display the generated image
image_output = gr.Image(label="Generated Image")
# Create a button to trigger the image generation
generate_button = gr.Button("Generate Image")
# Add some documentation using HTML
gr.HTML("""
<h2>Text-to-Image Generation with Liberata/illustrious-xl-v1.0</h2>
<p>This app allows you to generate images from text prompts using the Liberata/illustrious-xl-v1.0 model.</p>
<p>Simply enter a text prompt, adjust the parameters, and click the 'Generate Image' button to see the generated image.</p>
""")
# Define the event listener for the button click
generate_button.click(fn=generate_image, inputs=[prompt_input, guidance_scale_slider, num_inference_steps_slider], outputs=image_output)
# Launch the Gradio app with share=True
if __name__ == "__main__":
demo.launch(share=True, show_error=True)