Skip to content

LLM model selector with download store #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1e8bead
button width static
amakropoulos Jul 23, 2024
8567438
LLM manager
amakropoulos Jul 23, 2024
c7db57b
move LLMManager Editor inside LLM
amakropoulos Jul 23, 2024
9083761
add trash icon
amakropoulos Jul 23, 2024
abdb8aa
migrate LLMManager Editor to LLM Editor
amakropoulos Jul 23, 2024
fa6a2af
add custom url option
amakropoulos Jul 24, 2024
e65f14a
implement loras to model selection
amakropoulos Jul 24, 2024
5f9fe98
json and button beautification
amakropoulos Jul 24, 2024
8f01628
lora as argument
amakropoulos Jul 24, 2024
b333ef5
UI improvements
amakropoulos Jul 24, 2024
5661ac7
add label field, register LLM to LLMManager and remove model if not t…
amakropoulos Jul 25, 2024
8d4b2eb
expand button, button improvements, set model/lora if not in LLM
amakropoulos Jul 25, 2024
467f078
simplify AddAsset
amakropoulos Jul 25, 2024
ee7c6ce
use LLM manager on Editor mode otherwise GetAssetPath
amakropoulos Jul 25, 2024
a1a0b71
improve build process
amakropoulos Jul 26, 2024
9f29eca
store models in a player pref
amakropoulos Jul 26, 2024
a86ed07
download models with LLMManager
amakropoulos Jul 26, 2024
a785c2b
extract files on android
amakropoulos Jul 26, 2024
6556cd6
move android libraries to Plugins
amakropoulos Jul 26, 2024
5d40198
move android library directly
amakropoulos Jul 26, 2024
94a6f4b
bump LlamaLib to v1.1.6
amakropoulos Jul 26, 2024
af3f14b
create plugin dir first
amakropoulos Jul 26, 2024
e495b62
remove null warning
amakropoulos Jul 26, 2024
849f4d3
allow only one download access
amakropoulos Jul 26, 2024
abe0628
capture and expose download errors
amakropoulos Jul 26, 2024
3c656ef
fix android dll name
amakropoulos Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 25 additions & 117 deletions Editor/LLMBuildProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,150 +2,58 @@
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System;

namespace LLMUnity
{
public class LLMBuildProcessor : MonoBehaviour, IPreprocessBuildWithReport, IPostprocessBuildWithReport
public class LLMBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
public int callbackOrder => 0;
static string tempDir = Path.Combine(Application.temporaryCachePath, "LLMBuildProcessor", Path.GetFileName(LLMUnitySetup.libraryPath));
static List<MovedPair> movedPairs = new List<MovedPair>();
static string movedCache = Path.Combine(tempDir, "moved.json");

[InitializeOnLoadMethod]
private static void InitializeOnLoad()
{
if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
else ResetMoves();
}

// CALLED BEFORE THE BUILD
// called before the build
public void OnPreprocessBuild(BuildReport report)
{
// Start listening for errors when build starts
Application.logMessageReceived += OnBuildError;
HideLibraryPlatforms(report.summary.platform);
HideModels();
if (movedPairs.Count > 0) AssetDatabase.Refresh();
}

// CALLED DURING BUILD TO CHECK FOR ERRORS
private void OnBuildError(string condition, string stacktrace, LogType type)
{
if (type == LogType.Error)
{
// FAILED TO BUILD, STOP LISTENING FOR ERRORS
BuildCompleted();
}
}

// CALLED AFTER THE BUILD
public void OnPostprocessBuild(BuildReport report)
{
BuildCompleted();
}

public void BuildCompleted()
{
Application.logMessageReceived -= OnBuildError;
ResetMoves();
}

static bool MovePath(string source, string target)
{
bool moved = false;
if (File.Exists(source))
{
File.Move(source, target);
moved = true;
}
else if (Directory.Exists(source))
{
Directory.Move(source, target);
moved = true;
}
if (moved)
{
movedPairs.Add(new MovedPair {source = source, target = target});
File.WriteAllText(movedCache, JsonUtility.ToJson(new FoldersMovedWrapper { movedPairs = movedPairs }));
}
return moved;
}

static void MoveAssetAndMeta(string source, string target)
{
MovePath(source + ".meta", target + ".meta");
MovePath(source, target);
}

static void HideLibraryPlatforms(BuildTarget buildPlatform)
{
List<string> platforms = new List<string>(){ "windows", "macos", "linux", "android" };
switch (buildPlatform)
string platform = null;
switch (report.summary.platform)
{
case BuildTarget.StandaloneWindows:
case BuildTarget.StandaloneWindows64:
platforms.Remove("windows");
platform = "windows";
break;
case BuildTarget.StandaloneLinux64:
platforms.Remove("linux");
platform = "linux";
break;
case BuildTarget.StandaloneOSX:
platforms.Remove("macos");
platform = "macos";
break;
case BuildTarget.Android:
platforms.Remove("android");
platform = "android";
break;
case BuildTarget.iOS:
platform = "ios";
break;
}

foreach (string dirname in Directory.GetDirectories(LLMUnitySetup.libraryPath))
{
foreach (string platform in platforms)
{
if (Path.GetFileName(dirname).StartsWith(platform))
{
MoveAssetAndMeta(dirname, Path.Combine(tempDir, Path.GetFileName(dirname)));
}
}
}
LLMBuilder.HideLibraryPlatforms(platform);
LLMBuilder.BuildModels();
AssetDatabase.Refresh();
}

static void HideModels()
// called during build to check for errors
private void OnBuildError(string condition, string stacktrace, LogType type)
{
foreach (LLM llm in FindObjectsOfType<LLM>())
{
if (!llm.downloadOnBuild) continue;
if (llm.modelURL != "") MoveAssetAndMeta(LLMUnitySetup.GetAssetPath(llm.model), Path.Combine(tempDir, Path.GetFileName(llm.model)));
if (llm.loraURL != "") MoveAssetAndMeta(LLMUnitySetup.GetAssetPath(llm.lora), Path.Combine(tempDir, Path.GetFileName(llm.lora)));
}
if (type == LogType.Error) BuildCompleted();
}

static void ResetMoves()
// called after the build
public void OnPostprocessBuild(BuildReport report)
{
if (!File.Exists(movedCache)) return;
List<MovedPair> movedPairs = JsonUtility.FromJson<FoldersMovedWrapper>(File.ReadAllText(movedCache)).movedPairs;
if (movedPairs == null) return;

bool refresh = false;
foreach (var pair in movedPairs) refresh |= MovePath(pair.target, pair.source);
if (refresh) AssetDatabase.Refresh();
File.Delete(movedCache);
BuildCompleted();
}
}

[Serializable]
public struct MovedPair
{
public string source;
public string target;
}

[Serializable]
public class FoldersMovedWrapper
{
public List<MovedPair> movedPairs;
public void BuildCompleted()
{
Application.logMessageReceived -= OnBuildError;
LLMBuilder.Reset();
}
}
}
Loading
Loading