-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUniverseStart.cs
190 lines (151 loc) · 5.97 KB
/
UniverseStart.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
namespace Universe
{
[DisallowMultipleComponent] public class UniverseStart : MonoBehaviour
{
// Here we show how a cube is textured via StableDiffusion, and a
// text completed via GPT-3.
[SerializeField] GameObject testCube = null;
TextAI textAI = null;
void Awake()
{
const string pathPrefix = @"D:\_misc\";
Cache.rootFolder = pathPrefix + "Cache";
string openAIKey = File.ReadAllText(pathPrefix + "openai-key.txt");
TextAI.key = openAIKey;
CoroutineVariant.TextAI.key = openAIKey;
ImageAIDallE.key = openAIKey;
ImageAIStability.key = File.ReadAllText(pathPrefix + "stability-key.txt");
ImageAIReplicate.key = File.ReadAllText(pathPrefix + "replicate-key.txt");
textAI = Misc.GetAddComponent<TextAI>(gameObject);
}
void Start()
{
// TestTextAI();
// TestTextAI_GPT3();
// TestTextAI_Coroutine();
// TestImageAI();
// TestImageAIStability();
// StartCoroutine(TestWhenAll_Coroutine());
}
async void TestTextAI()
{
const string prompt = "Who was Albert Einstein? Thanks!";
System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
string result = await textAI.GetAnswer(prompt, useCache: false, temperature: 0f, showResultInfo: false, maxTokens: 200);
stopwatch.Stop();
Debug.Log($"TestTextAI elapsed time: {stopwatch.ElapsedMilliseconds} ms");
Debug.Log(result);
}
async void TestTextAI_GPT3()
{
const string prompt = "Albert Einstein was";
System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
string result = await textAI.GetAnswer(prompt, useCache: false, temperature: 0f, showResultInfo: false, maxTokens: 200,
model: "text-davinci-003", endpoint: "/v1/completions");
stopwatch.Stop();
Debug.Log($"TestTextAI elapsed time: {stopwatch.ElapsedMilliseconds} ms");
Debug.Log(result);
}
void TestTextAI_Coroutine()
{
CoroutineVariant.TextAI textAICoroutine = Misc.GetAddComponent<CoroutineVariant.TextAI>(gameObject);
const string prompt = "Who was Albert Einstein? Thanks!";
System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
StartCoroutine(
textAICoroutine.GetAnswer(prompt, (string result) =>
{
stopwatch.Stop();
Debug.Log($"TestTextAI elapsed time: {stopwatch.ElapsedMilliseconds} ms");
Debug.Log(result);
},
useCache: false, temperature: 0f, showResultInfo: false, maxTokens: 200
));
}
async void TestWhenAll()
{
Debug.Log("TestWhenAll");
Task<string> a = textAI.GetAnswer("Who was Albert Einstein?", useCache: false);
Task<string> b = textAI.GetAnswer("Who is Susan Sarandon?", useCache: false);
await Task.WhenAll(a, b);
Debug.Log("a: " + a.Result);
Debug.Log("b: " + b.Result);
}
IEnumerator TestWhenAll_Coroutine()
{
Debug.Log("TestWhenAll_Coroutine");
CoroutineVariant.TextAI textAICoroutine = Misc.GetAddComponent<CoroutineVariant.TextAI>(gameObject);
string a = null;
string b = null;
StartCoroutine(textAICoroutine.GetAnswer("Who was Albert Einstein?", (result) => a = result));
StartCoroutine(textAICoroutine.GetAnswer("Who is Susan Sarandon?", (result) => b = result));
yield return new WaitUntil(()=>
{
return !string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b);
});
Debug.Log("a: " + a);
Debug.Log("b: " + b);
}
void TestImageAI()
{
ImageAI imageAI = Misc.GetAddComponent<ImageAI>(gameObject);
string prompt = "person on mountain, minimalist 3d";
Debug.Log("Sending prompt " + prompt);
StartCoroutine(
imageAI.GetImage(prompt, (Texture2D texture) =>
{
Debug.Log("Done.");
Renderer renderer = testCube.GetComponent<Renderer>();
renderer.material.mainTexture = texture;
},
useCache: false,
width: 512, height: 512
));
}
void TestImageAIStability()
{
ImageAIStability imageAI = Misc.GetAddComponent<ImageAIStability>(gameObject);
// StartCoroutine(imageAI.ShowAvailableEngines());
string prompt = "person on mountain, minimalist 3d";
Debug.Log("Sending prompt " + prompt);
StartCoroutine(
imageAI.GetImage(prompt, (Texture2D texture) =>
{
Debug.Log("Done.");
Renderer renderer = testCube.GetComponent<Renderer>();
renderer.material.mainTexture = texture;
},
useCache: false,
width: 512, height: 512
));
}
void TestImageAIDallE()
{
ImageAIDallE imageAI = Misc.GetAddComponent<ImageAIDallE>(gameObject);
string prompt = "wizard's hut, black background, artstation asset";
Debug.Log("Sending prompt " + prompt);
const int size = 1024;
StartCoroutine(
imageAI.GetImage(prompt, (Texture2D texture) =>
{
Debug.Log("Done.");
Renderer renderer = testCube.GetComponent<Renderer>();
Color fillColor = new Color(0f, 0f, 0.2f, 0f);
ImageFloodFill.FillFromSides(
texture, fillColor,
threshold: 0.075f, contour: 5f, bottomAlignImage: true);
renderer.material.mainTexture = texture;
},
useCache: true,
width: size, height: size
));
}
}
}