ext_loras.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const LORA_REGEX = /<(?!e:|h:)[^,> ]*>?/g;
  2. const LORA_TRIGGER = () => CFG.useLoras && tagword.match(LORA_REGEX);
  3. class LoraParser extends BaseTagParser {
  4. parse() {
  5. // Show lora
  6. let tempResults = [];
  7. if (tagword !== "<" && tagword !== "<l:" && tagword !== "<lora:") {
  8. let searchTerm = tagword.replace("<lora:", "").replace("<l:", "").replace("<", "");
  9. let filterCondition = x => x.toLowerCase().includes(searchTerm) || x.toLowerCase().replaceAll(" ", "_").includes(searchTerm);
  10. tempResults = loras.filter(x => filterCondition(x)); // Filter by tagword
  11. } else {
  12. tempResults = loras;
  13. }
  14. // Add final results
  15. let finalResults = [];
  16. tempResults.forEach(t => {
  17. let result = new AutocompleteResult(t.trim(), ResultType.lora)
  18. result.meta = "Lora";
  19. finalResults.push(result);
  20. });
  21. return finalResults;
  22. }
  23. }
  24. async function load() {
  25. if (loras.length === 0) {
  26. try {
  27. loras = (await readFile(`${tagBasePath}/temp/lora.txt`)).split("\n")
  28. .filter(x => x.trim().length > 0) // Remove empty lines
  29. .map(x => x.trim()); // Remove carriage returns and padding if it exists
  30. } catch (e) {
  31. console.error("Error loading lora.txt: " + e);
  32. }
  33. }
  34. }
  35. function sanitize(tagType, text) {
  36. if (tagType === ResultType.lora) {
  37. return `<lora:${text}:${CFG.extraNetworksDefaultMultiplier}>`;
  38. }
  39. return null;
  40. }
  41. PARSERS.push(new LoraParser(LORA_TRIGGER));
  42. // Add our utility functions to their respective queues
  43. QUEUE_FILE_LOAD.push(load);
  44. QUEUE_SANITIZE.push(sanitize);