File size: 1,311 Bytes
1983f1b
461ffd0
1983f1b
461ffd0
 
1983f1b
 
 
 
 
461ffd0
 
 
 
 
1983f1b
461ffd0
 
 
 
 
 
 
 
 
 
 
 
1983f1b
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import DalleBartTokenizer, DalleBartForConditionalGeneration
from PIL import Image
import numpy as np

# Load the tokenizer and model
tokenizer = DalleBartTokenizer.from_pretrained("dalle-mini/dalle-mini")
model = DalleBartForConditionalGeneration.from_pretrained("dalle-mini/dalle-mini")

# Ensure the model is in evaluation mode and use GPU if available
model.eval()
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

def generate_image(prompt):
    try:
        inputs = tokenizer(prompt, return_tensors="pt").to(device)
        outputs = model.generate(**inputs)
        
        # Convert the output tensor to an image
        # Adjust this line according to the model output format
        image = outputs[0]  # Assuming this is an image tensor
        image = Image.fromarray(np.array(image))  # Convert tensor to numpy array then to image

        return image
    except Exception as e:
        return str(e)

# Define Gradio interface
iface = gr.Interface(fn=generate_image,
                     inputs="text",
                     outputs="image",
                     title="DALL-E Mini Image Generator",
                     description="Enter a prompt to generate an image.")

if __name__ == "__main__":
    iface.launch()