Skydata001 commited on
Commit
2b4ca23
·
verified ·
1 Parent(s): 3b5206d

Create image_generator.py

Browse files
Files changed (1) hide show
  1. agents/image_generator.py +47 -0
agents/image_generator.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Image Generator Agent
3
+ Generates keyframe images using FLUX.1-dev.
4
+ """
5
+ import os
6
+ import torch
7
+ from diffusers import FluxPipeline
8
+ from PIL import Image
9
+
10
+ class ImageGeneratorAgent:
11
+ def __init__(self, gpu_id: int = 5):
12
+ self.gpu_id = gpu_id
13
+ self.device = f"cuda:{gpu_id}"
14
+ self.pipe = None
15
+ self._load_model()
16
+
17
+ def _load_model(self):
18
+ try:
19
+ model_id = "black-forest-labs/FLUX.1-dev"
20
+ self.pipe = FluxPipeline.from_pretrained(
21
+ model_id,
22
+ torch_dtype=torch.float8_e4m3fn,
23
+ variant="fp8",
24
+ cache_dir="/workspace/.cache/huggingface"
25
+ ).to(self.device)
26
+ self.pipe.safety_checker = None
27
+ print(f"[IMAGE_GENERATOR] FLUX loaded on {self.device}")
28
+ except Exception as e:
29
+ print(f"[IMAGE_GENERATOR] Failed to load FLUX: {e}")
30
+
31
+ async def generate(self, prompt: str, job_id: str) -> str:
32
+ if not self.pipe:
33
+ raise RuntimeError("FLUX pipeline not loaded")
34
+
35
+ output_path = f"/workspace/outputs/{job_id}_keyframe.png"
36
+
37
+ image = self.pipe(
38
+ prompt=prompt,
39
+ height=1024,
40
+ width=1024,
41
+ num_inference_steps=30,
42
+ guidance_scale=3.5,
43
+ max_sequence_length=512
44
+ ).images[0]
45
+
46
+ image.save(output_path)
47
+ return output_path