codewithdark commited on
Commit
ab9f5d8
·
verified ·
1 Parent(s): aa3db10

Update utility/image_generator.py

Browse files
Files changed (1) hide show
  1. utility/image_generator.py +39 -38
utility/image_generator.py CHANGED
@@ -1,39 +1,40 @@
1
- from diffusers import DiffusionPipeline
2
- import torch
3
- import re
4
- from PIL import Image
5
- import io
6
- from dotenv import load_dotenv
7
- import os
8
-
9
- load_dotenv()
10
-
11
- # Ensure GPU is used if available
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
13
- from diffusers import DiffusionPipeline
14
-
15
- pipeline = DiffusionPipeline.from_pretrained("Shakker-Labs/AWPortrait-FL")
16
-
17
- def generate_image_prompts(script):
18
- # Split the script into sentences
19
- sentences = re.split(r'(?<=[.!?]) +', script)
20
-
21
- # Generate prompts for each sentence
22
- prompts = []
23
- for sentence in sentences:
24
- if sentence.strip(): # Ensure the sentence is not empty
25
- prompts.append(sentence.strip())
26
-
27
- return prompts
28
-
29
- def generate_images(prompts):
30
- image_files = []
31
- for idx, prompt in enumerate(prompts):
32
- print(f"Generating image for prompt: {prompt}")
33
- # Ensure the prompt is processed on the correct device
34
- image = pipeline(prompt).images[0]
35
- filename = f"generated_image_{idx}.png"
36
- image.save(filename)
37
- image_files.append(filename)
38
-
 
39
  return image_files
 
1
+ from diffusers import DiffusionPipeline
2
+ import torch
3
+ import re
4
+ from PIL import Image
5
+ import io
6
+ from dotenv import load_dotenv
7
+ import os
8
+
9
+ load_dotenv()
10
+
11
+ # Ensure GPU is used if available
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+
14
+
15
+ pipeline = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev")
16
+ pipeline = pipeline.to(device)
17
+
18
+ def generate_image_prompts(script):
19
+ # Split the script into sentences
20
+ sentences = re.split(r'(?<=[.!?]) +', script)
21
+
22
+ # Generate prompts for each sentence
23
+ prompts = []
24
+ for sentence in sentences:
25
+ if sentence.strip(): # Ensure the sentence is not empty
26
+ prompts.append(sentence.strip())
27
+
28
+ return prompts
29
+
30
+ def generate_images(prompts):
31
+ image_files = []
32
+ for idx, prompt in enumerate(prompts):
33
+ print(f"Generating image for prompt: {prompt}")
34
+ # Ensure the prompt is processed on the correct device
35
+ image = pipeline(prompt).images[0]
36
+ filename = f"generated_image_{idx}.png"
37
+ image.save(filename)
38
+ image_files.append(filename)
39
+
40
  return image_files