Spaces:
Runtime error
Runtime error
Update utility/image_generator.py
Browse files- 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 |
-
|
| 14 |
-
|
| 15 |
-
pipeline = DiffusionPipeline.from_pretrained("
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
prompts
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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
|