Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

raea gen: a loaded file changed in place is a new input

shinyobjectze25152416447
diff --git a/io/gen-core/src/flow.rs b/io/gen-core/src/flow.rs
index 58ac3ac2..6cc5760a 100644
--- a/io/gen-core/src/flow.rs
+++ b/io/gen-core/src/flow.rs
@@ -185,13 +185,26 @@ impl Flow {
/// Each step's key: a hash of its op, model and inputs, with each reference replaced by that step's key, so a
/// step's key changes when anything it's made from does. Cached outputs are found by it.
pub fn keys(&self) -> Result<BTreeMap<String, String>, String> {
+ self.keys_in(None)
+ }
+
+ /// The keys, with a `media.load` of a local file keyed by the file's contents as well as its path (read from
+ /// `base` when the path is relative): a picture changed in place is a new input, and every step made from it is
+ /// made again (a face's front made again at the same path kept its old bald edits, sheet3d chars/gb-sapper 11-17).
+ pub fn keys_in(&self, base: Option<&std::path::Path>) -> Result<BTreeMap<String, String>, String> {
let mut keys = BTreeMap::new();
for id in self.order()? {
let s = &self.steps[&id];
// a mesh's `size` is fitted after the model is made (raea gen check), so it doesn't make another model
let inputs = J::Object(s.inputs.iter().filter(|(k, _)| !(s.op == "mesh.generate" && k.as_str() == "size")).map(|(k, v)| (k.clone(), v.clone())).collect());
let resolved = substitute(&inputs, &|r| keys.get(r).map(|k: &String| format!("key:{k}")));
- let text = format!("{}\u{0}{}\u{0}{}", s.op, s.model.as_deref().unwrap_or(""), canonical(&resolved));
+ let mut text = format!("{}\u{0}{}\u{0}{}", s.op, s.model.as_deref().unwrap_or(""), canonical(&resolved));
+ if let (Some(b), "media.load", Some(p)) = (base, s.op.as_str(), s.inputs.get("path").and_then(|p| p.as_str())) {
+ let f = if std::path::Path::new(p).is_absolute() { std::path::PathBuf::from(p) } else { b.join(p) };
+ if let Ok(bytes) = std::fs::read(&f) {
+ text.push_str(&format!("\u{0}file:{:x}", Sha256::digest(&bytes)));
+ }
+ }
keys.insert(id, format!("{:x}", Sha256::digest(text.as_bytes()))[..24].to_string());
}
Ok(keys)
diff --git a/io/gen/src/run.rs b/io/gen/src/run.rs
index fbb8ce71..e4bd9ba3 100644
--- a/io/gen/src/run.rs
+++ b/io/gen/src/run.rs
@@ -73,7 +73,7 @@ impl<'a> Run<'a> {
pub fn plan(&self, flow: &Flow) -> Result<J, String> {
let made = Made::from_settings().ok();
let cached = self.index();
- let keys = flow.keys()?;
+ let keys = flow.keys_in(Some(&self.base))?;
let (mut rows, mut total) = (Vec::new(), 0u64);
// a text step's words aren't written yet: a stand-in prices the prompts that take them
let words = |r: &str| flow.steps.get(r).filter(|t| t.op == "text.generate").map(|_| format!("(the words of step {r})"));
@@ -138,7 +138,7 @@ impl<'a> Run<'a> {
/// The flow made: each output step's files.
pub fn run(&self, flow: &Flow) -> Result<J, String> {
std::fs::create_dir_all(&self.dir).map_err(|e| format!("{}: {e}", self.dir.display()))?;
- let keys = flow.keys()?;
+ let keys = flow.keys_in(Some(&self.base))?;
let cached = self.index();
let mut done: BTreeMap<String, Made1> = BTreeMap::new();
let (mut spent, mut rows) = (0u64, Vec::new());
diff --git a/io/gen/src/tests.rs b/io/gen/src/tests.rs
index 6d5efaec..c3d27223 100644
--- a/io/gen/src/tests.rs
+++ b/io/gen/src/tests.rs
@@ -134,6 +134,25 @@ fn keys_change_with_what_a_step_reads() {
assert_eq!(Flow::parse(&f.to_json()).unwrap(), f);
}
+#[test]
+fn a_loaded_file_changed_in_place_is_a_new_input() {
+ let dir = std::env::temp_dir().join(format!("raea-gen-keys-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ std::fs::write(dir.join("front.jpeg"), b"one face").unwrap();
+ let mut f = Flow::default();
+ f.steps.insert("ref".into(), Step::new("media.load", json!({"path": "front.jpeg"})));
+ f.steps.insert("bald".into(), Step::new("image.edit", json!({"image": "@ref", "prompt": "shaved"})));
+ f.out = vec!["bald".into()];
+ let k1 = f.keys_in(Some(&dir)).unwrap();
+ std::fs::write(dir.join("front.jpeg"), b"another face").unwrap();
+ let k2 = f.keys_in(Some(&dir)).unwrap();
+ assert_ne!(k1["ref"], k2["ref"]);
+ assert_ne!(k1["bald"], k2["bald"]);
+ // without a base (or with the file gone) the key is the path's, as before
+ assert_eq!(f.keys().unwrap(), f.keys_in(None).unwrap());
+ std::fs::remove_dir_all(&dir).ok();
+}
+
#[test]
fn made_bodies_fit_the_model() {
let model = json!({"id": "kling_3_pro_i2v", "durations": [3, 4, 5, 10], "mediaRoles": {"start_image": "x", "end_image": "y"}, "allowedParams": ["negative_prompt"], "inputMap": {"audio": {}}});