-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathMobileDemo.cs
106 lines (93 loc) · 2.9 KB
/
MobileDemo.cs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using UnityEngine;
using LLMUnity;
using UnityEngine.UI;
using System.Threading.Tasks;
namespace LLMUnitySamples
{
public class MobileDemo : MonoBehaviour
{
public LLMCharacter llmCharacter;
public GameObject ChatPanel;
public InputField playerText;
public Text AIText;
public GameObject ErrorText;
public GameObject DownloadPanel;
public Scrollbar progressBar;
public Text progressText;
async void Start()
{
playerText.onSubmit.AddListener(onInputFieldSubmit);
playerText.interactable = false;
await DownloadThenWarmup();
}
async Task DownloadThenWarmup()
{
ChatPanel.SetActive(false);
DownloadPanel.SetActive(true);
bool downloadOK = await LLM.WaitUntilModelSetup(SetProgress);
if (!downloadOK)
{
ErrorText.SetActive(true);
}
else
{
DownloadPanel.SetActive(false);
ChatPanel.SetActive(true);
await WarmUp();
}
}
async Task WarmUp()
{
AIText.text += $"Warming up the model...";
await llmCharacter.Warmup();
AIText.text = "";
AIReplyComplete();
}
void SetProgress(float progress)
{
progressText.text = ((int)(progress * 100)).ToString() + "%";
progressBar.size = progress;
}
void onInputFieldSubmit(string message)
{
playerText.interactable = false;
AIText.text = "...";
_ = llmCharacter.Chat(message, SetAIText, AIReplyComplete);
}
public void SetAIText(string text)
{
AIText.text = text;
}
public void AIReplyComplete()
{
playerText.interactable = true;
playerText.Select();
playerText.text = "";
}
public void CancelRequests()
{
llmCharacter.CancelRequests();
AIReplyComplete();
}
public void ExitGame()
{
Debug.Log("Exit button clicked");
Application.Quit();
}
bool onValidateWarning = true;
bool onValidateInfo = true;
void OnValidate()
{
if (onValidateWarning && !llmCharacter.remote && llmCharacter.llm != null && llmCharacter.llm.model == "")
{
Debug.LogWarning($"Please select a model in the {llmCharacter.llm.gameObject.name} GameObject!");
onValidateWarning = false;
}
if (onValidateInfo)
{
Debug.Log($"Select 'Download On Start' in the {llmCharacter.llm.gameObject.name} GameObject to download the models when the app starts.");
onValidateInfo = false;
}
}
}
}