File size: 1,040 Bytes
b3081c2
 
 
 
 
48ae64f
b3081c2
 
 
 
 
 
 
 
68ea509
e6a56ca
 
 
b3081c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import constants
from PIL import Image
from gradio_client import Client


class ImageGenerator:     

    def generate_image(self, prompt, path='test_image.png'):
        try:
            # Initialize the Gradio Client with Hugging Face token
            client = Client(constants.IMAGE_GENERATION_SPACE_NAME, hf_token=constants.HF_TOKEN)

            # Make the API request
            result = client.predict(
    		prompt=prompt,
    		width=720,
    		height=1280,
    		api_name="/generate_image")

            image = Image.open(result)
            image.save(path)

            # Return the result (which includes the URL or file path)
            return result

        except Exception as e:
            print(f"Error during image generation: {e}")
            return {"error": str(e)}


# Example usage
if __name__ == '__main__':
    image_generator = ImageGenerator()  # You can pass custom params here if needed
    result = image_generator.generate_image("A cat with flowers around it.",path='wow9.png')
    
    print(result)