File size: 1,838 Bytes
8df578c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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()