File size: 973 Bytes
b9af579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from diffusers import StableDiffusionPipeline
from PIL import Image
from io import BytesIO
from utils import load_unet_model

class TextToImage:
    """
    Class to handle Text-to-Image generation using Stable Diffusion XL.
    """
    def __init__(self, device="cpu"):
        # Model and repository details
        model_id = "OFA-Sys/small-stable-diffusion-v0"
        self.device = device
        self.pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32).pipe.to(device)
        print("Text-to-Image model loaded successfully.")
    
    
    async def generate_image(self, prompt):
        """
        Generate an image from a text prompt.

        Args:
            prompt (str): The text prompt to generate the image.

        Returns:
            PIL.Image: The generated image.
        """
        with torch.no_grad():
            image = self.pipe(
                prompt
            ).images[0]
        return image