-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_backend.py
More file actions
69 lines (53 loc) · 1.83 KB
/
image_backend.py
File metadata and controls
69 lines (53 loc) · 1.83 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import subprocess
import json
class ImageBackend:
"""
Converts prompts into actual images using external tools.
Supports:
- Ollama vision models (if available)
- Stable Diffusion CLI (optional)
- fallback: structured output
"""
def __init__(self, mode="ollama"):
self.mode = mode
# =====================================================
# GENERATE IMAGE
# =====================================================
def generate(self, prompt, output_path="frame.png"):
if self.mode == "ollama":
return self._ollama_generate(prompt, output_path)
# fallback placeholder
return self._mock_generate(prompt, output_path)
# =====================================================
# OLLAMA PIPELINE
# =====================================================
def _ollama_generate(self, prompt, output_path):
"""
NOTE:
Ollama itself does not generate images,
so this produces a structured prompt for:
- stable diffusion backend
- or external pipeline
"""
result = subprocess.run(
["ollama", "run", "codegemma:7b", prompt],
capture_output=True,
text=True
)
with open(output_path + ".txt", "w") as f:
f.write(result.stdout)
return {
"status": "prompt_generated",
"path": output_path + ".txt",
"text": result.stdout
}
# =====================================================
# MOCK RENDERER (DEBUG MODE)
# =====================================================
def _mock_generate(self, prompt, output_path):
with open(output_path + ".txt", "w") as f:
f.write(prompt)
return {
"status": "mock",
"path": output_path + ".txt"
}