Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import DiffusionPipeline
|
| 3 |
+
|
| 4 |
+
# Load the pre-trained model
|
| 5 |
+
pipeline = DiffusionPipeline.from_pretrained("stablediffusionapi/juggernaut-xl-v5")
|
| 6 |
+
|
| 7 |
+
# Load the LORA weights
|
| 8 |
+
pipeline.load_lora_weights("openskyml/dalle-3-xl")
|
| 9 |
+
|
| 10 |
+
# Define a function to generate images
|
| 11 |
+
def generate_image(text):
|
| 12 |
+
# Encode the text using the LORA model
|
| 13 |
+
input_ids = pipeline.encode_plus(
|
| 14 |
+
text,
|
| 15 |
+
add_special_tokens=True,
|
| 16 |
+
max_length=512,
|
| 17 |
+
padding='max_length',
|
| 18 |
+
truncation=True,
|
| 19 |
+
return_attention_mask=True,
|
| 20 |
+
return_tensors='pt'
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Generate an image using the encoded input
|
| 24 |
+
image = pipeline.generate(
|
| 25 |
+
input_ids,
|
| 26 |
+
attention_mask=input_ids.attention_mask,
|
| 27 |
+
max_length=512,
|
| 28 |
+
padding='max_length',
|
| 29 |
+
truncation=True,
|
| 30 |
+
return_attention_mask=True,
|
| 31 |
+
return_tensors='pt'
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Decode the image
|
| 35 |
+
image = pipeline.decode(image, skip_special_tokens=True)
|
| 36 |
+
|
| 37 |
+
return image
|
| 38 |
+
|
| 39 |
+
# Create a Gradio interface
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
+
title='Text-to-Image App',
|
| 42 |
+
description='Generate images from text using a pre-trained diffusion model',
|
| 43 |
+
input_widget=gr.TextInput(
|
| 44 |
+
label='Enter text',
|
| 45 |
+
placeholder='Type some text here'
|
| 46 |
+
),
|
| 47 |
+
output_widget=gr.ImageOutput(
|
| 48 |
+
label='Generated image'
|
| 49 |
+
),
|
| 50 |
+
submit_button=gr.Button(
|
| 51 |
+
label='Generate image'
|
| 52 |
+
)
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Define a callback function to handle user input
|
| 56 |
+
def handle_input(text):
|
| 57 |
+
# Generate an image using the generate_image function
|
| 58 |
+
image = generate_image(text)
|
| 59 |
+
|
| 60 |
+
# Display the generated image
|
| 61 |
+
interface.output_widget.set_image(image)
|
| 62 |
+
|
| 63 |
+
return image
|
| 64 |
+
|
| 65 |
+
# Set up the Gradio interface
|
| 66 |
+
interface.attach_handlers(
|
| 67 |
+
input_widget=handle_input,
|
| 68 |
+
submit_button=handle_input
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Run the Gradio interface
|
| 72 |
+
interface.run()
|