svjack's picture
Update app.py
b1b0acb verified
raw
history blame
3.84 kB
'''
sudo apt-get update && sudo apt-get install git-lfs ffmpeg cbm
# Clone this repository
git clone https://huggingface.co/spaces/svjack/illustrious-xl-v1.0-demo
# Go into the repository
cd illustrious-xl-v1.0-demo
### Install dependencies ###
conda create --name py310 python=3.10
conda activate py310
# Install ipykernel and add the environment to Jupyter
pip install ipykernel
python -m ipykernel install --user --name py310 --display-name "py310"
pip install -r requirements.txt
python app.py
'''
# Import necessary libraries
import gradio as gr
import diffusers
import torch
# Automatically detect if CUDA is available and set the device accordingly
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load the model from the specified file outside the function to ensure better performance
model = diffusers.StableDiffusionXLPipeline.from_single_file(
"https://huggingface.co/Liberata/illustrious-xl-v1.0/blob/main/Illustrious-XL-v1.0.safetensors",
torch_dtype=torch.float16
).to(device) # Move the model to the detected device
# Define the function to generate an image from text using the pre-loaded model
def generate_image(prompt, guidance_scale, num_inference_steps):
# 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:
# 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>
""")
# Create a two-column layout
with gr.Row():
with gr.Column():
# 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")
# Add a resolution input
#resolution_input = gr.Textbox(value="1536x1536", label="Resolution")
with gr.Column():
# 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")
# 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)
# Add examples
examples = [
["1boy, Digital anime-style ALBEDO has light gray, messy hair, blue eyes. Shy look, in white & blue hoodie, at cozy café table with food. Shelf background"],
["1boy ,Digital anime-style CHONGYUN has shiny silver hair, green eyes. Cheerful expression, in bright blue T-shirt, at luxurious restaurant with Belgian chocolates. Elegant crystal chandelier background."],
["A fox drinking tea under a cherry blossom tree, anime style, 4K resolution"],
["Night view of a futuristic city, cyberpunk style, neon lights"],
["Medieval knight battling a dragon, epic scene, oil painting texture"]
]
gr.Examples(examples, [prompt_input, guidance_scale_slider, num_inference_steps_slider])
# Launch the Gradio app with share=True
if __name__ == "__main__":
demo.launch(share=True, show_error=True)