-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuMainForm.pas
470 lines (404 loc) · 13.2 KB
/
uMainForm.pas
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
unit uMainForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
FMX.StdCtrls, FMX.Layouts, FMX.Controls.Presentation, FMX.Effects, FMX.Edit,
System.Math, REST.Types, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
REST.Response.Adapter, REST.Client, Data.Bind.Components,
Data.Bind.ObjectScope, FMX.TabControl, FMX.Memo.Types, FMX.ScrollBox, FMX.Memo,
FMX.TextLayout, FireDAC.Stan.StorageBin, FMX.ListBox, System.Rtti,
System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.EngExt,
Fmx.Bind.DBEngExt, Data.Bind.DBScope;
type
TMainForm = class(TForm)
VSB: TVertScrollBox;
Layout1: TLayout;
GenerateButton: TButton;
Image1: TImage;
MaterialOxfordBlueSB: TStyleBook;
ToolBar1: TToolBar;
ShadowEffect4: TShadowEffect;
Label1: TLabel;
PromptEdit: TEdit;
Image2: TImage;
SourceImage: TImageControl;
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;
RESTResponseDataSetAdapter1: TRESTResponseDataSetAdapter;
FDMemTable1: TFDMemTable;
RESTClient2: TRESTClient;
RESTRequest2: TRESTRequest;
RESTResponse2: TRESTResponse;
RESTResponseDataSetAdapter2: TRESTResponseDataSetAdapter;
FDMemTable2: TFDMemTable;
APIKeyEdit: TEdit;
APIKeyButton: TButton;
StatusBar1: TStatusBar;
ProgressBar: TProgressBar;
Layout2: TLayout;
Layout3: TLayout;
ImageEdit: TEdit;
OpenButton: TButton;
OpenDialog: TOpenDialog;
TabControl1: TTabControl;
TabItem1: TTabItem;
TabItem2: TTabItem;
XrayMemo: TMemo;
TemplateMemo: TMemo;
Timer1: TTimer;
Timer2: TTimer;
Splitter1: TSplitter;
ModelsMT: TFDMemTable;
VersionEdit: TComboBox;
BindSourceDB1: TBindSourceDB;
BindingsList1: TBindingsList;
LinkListControlToField1: TLinkListControlToField;
ModelLabel: TLabel;
LinkPropertyToFieldText: TLinkPropertyToField;
procedure OpenButtonClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure GenerateButtonClick(Sender: TObject);
procedure APIKeyButtonClick(Sender: TObject);
private
{ Private declarations }
FCurrentMessage: TText;
procedure LResized(Sender: TObject);
public
{ Public declarations }
procedure AddMessage(const AText: String; AAlignLayout: TAlignLayout; ACalloutPosition: TCalloutPosition);
procedure FriendMessage(const S: String);
procedure YourMessage(const S: String);
procedure LabelPaint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
function MemoryStreamToBase64(const MemoryStream: TMemoryStream): string;
end;
var
MainForm: TMainForm;
implementation
{$R *.fmx}
uses
System.NetEncoding, System.Net.Mime, System.JSON, System.Generics.Collections,
System.IOUtils;
function JSONQuote(const AValue: string): string;
var
JSONStr: TJSONString;
begin
JSONStr := TJSONString.Create(AValue);
try
Result := JSONStr.ToJSON;
finally
JSONStr.Free;
end;
end;
function TMainForm.MemoryStreamToBase64(const MemoryStream: TMemoryStream): string;
var
OutputStringStream: TStringStream;
Base64Encoder: TBase64Encoding;
MimeType: string;
begin
MemoryStream.Position := 0;
OutputStringStream := TStringStream.Create('', TEncoding.ASCII);
try
Base64Encoder := TBase64Encoding.Create;
try
Base64Encoder.Encode(MemoryStream, OutputStringStream);
MimeType := 'image/png';
Result := 'data:' + MimeType + ';base64,' + OutputStringStream.DataString.Replace(#13#10,'');
finally
Base64Encoder.Free;
end;
finally
OutputStringStream.Free;
end;
end;
procedure TMainForm.OpenButtonClick(Sender: TObject);
begin
if OpenDialog.Execute then
begin
ImageEdit.Text := OpenDialog.FileName;
SourceImage.Bitmap.LoadFromFile(ImageEdit.Text);
end;
end;
function ParseJSONStrArray(const JSONStr: String): TArray<String>;
var
JSONArray: TJSONArray;
I: Integer;
begin
JSONArray := TJSONObject.ParseJSONValue(JSONStr) as TJSONArray;
try
SetLength(Result, JSONArray.Count);
for I := 0 to JSONArray.Count - 1 do
Result[I] := JSONArray.Items[I].Value;
finally
JSONArray.Free;
end;
end;
function IsJSONArray(const s: string): Boolean;
var
JSONValue: TJSONValue;
begin
Result := False;
try
JSONValue := TJSONObject.ParseJSONValue(s);
try
if JSONValue is TJSONArray then
Result := True;
finally
JSONValue.Free;
end;
except
on E: EJSONException do
// Do nothing; Result remains False
end;
end;
procedure TMainForm.Timer1Timer(Sender: TObject);
begin
RestRequest2.Params[0].Value := 'Token ' + APIKeyEdit.Text;
RESTRequest2.Execute;
var F := FDMemTable2.FindField('status');
if F<>nil then
begin
if F.AsWideString='processing' then
begin
XrayMemo.Lines.Append('GET Request');
XrayMemo.Lines.Append('URL:');
XrayMemo.Lines.Append(RestClient2.BaseURL+'/'+RESTRequest2.Resource+#13#10);
XrayMemo.Lines.Append('Response');
XrayMemo.Lines.Append(RESTResponse2.Content+#13#10);
if FDMemTable2.FindField('output')<>nil then
begin
var LResponse := '';
if IsJSONArray(FDMemTable2.FieldByName('output').AsWideString) then
begin
var OutputArray := ParseJSONStrArray(FDMemTable2.FieldByName('output').AsWideString);
for var I := 0 to High(OutputArray) do
begin
LResponse := LResponse+OutputArray[I];
end;
end
else
LResponse := FDMemTable2.FieldByName('output').AsWideString;
FriendMessage(LResponse);
end;
end
else
if F.AsWideString='succeeded' then
begin
Timer1.Enabled := False;
XrayMemo.Lines.Append('GET Request');
XrayMemo.Lines.Append('URL:');
XrayMemo.Lines.Append(RestClient2.BaseURL+'/'+RESTRequest2.Resource+#13#10);
XrayMemo.Lines.Append('Response');
XrayMemo.Lines.Append(RESTResponse2.Content+#13#10);
var LResponse := '';
if IsJSONArray(FDMemTable2.FieldByName('output').AsWideString) then
begin
var OutputArray := ParseJSONStrArray(FDMemTable2.FieldByName('output').AsWideString);
for var I := 0 to High(OutputArray) do
begin
LResponse := LResponse+OutputArray[I];
end;
end
else
LResponse := FDMemTable2.FieldByName('output').AsWideString;
FriendMessage(LResponse);
FCurrentMessage := nil;
ProgressBar.Visible := False;
GenerateButton.Enabled := True;
end
else
if F.AsWideString='failed' then
begin
Timer1.Enabled := False;
XrayMemo.Lines.Append('GET Request');
XrayMemo.Lines.Append('URL:');
XrayMemo.Lines.Append(RestClient2.BaseURL+'/'+RESTRequest2.Resource+#13#10);
XrayMemo.Lines.Append('Response');
XrayMemo.Lines.Append(RESTResponse2.Content+#13#10);
ProgressBar.Visible := False;
GenerateButton.Enabled := True;
ShowMessage(FDMemTable2.FieldByName('error').AsWideString);
end;
end;
end;
procedure TMainForm.Timer2Timer(Sender: TObject);
begin
if ProgressBar.Value=ProgressBar.Max then
ProgressBar.Value := ProgressBar.Min
else
ProgressBar.Value := ProgressBar.Value+5;
end;
procedure TMainForm.LResized(Sender: TObject);
begin
TCalloutRectangle(TText(Sender).Parent).Height := TText(Sender).Height + TText(Sender).Margins.Top + TText(Sender).Margins.Bottom + TCalloutRectangle(TText(Sender).Parent).Margins.Top + TCalloutRectangle(TText(Sender).Parent).Margins.Bottom;
if (TCalloutRectangle(TText(Sender).Parent).Height<75) then TCalloutRectangle(TText(Sender).Parent).Height := 75;
end;
procedure TMainForm.AddMessage(const AText: String; AAlignLayout: TAlignLayout; ACalloutPosition: TCalloutPosition);
var
CR: TCalloutRectangle;
L: TText;
TmpImg: TCircle;
TmpLayout: TLayout;
begin
CR := TCalloutRectangle.Create(Self);
CR.Parent := VSB;
CR.Align := TAlignLayout.Top;
CR.CalloutPosition := ACalloutPosition;
CR.Margins.Top := 10;
CR.Margins.Bottom := 10;
CR.Margins.Left := 5;
if (ACalloutPosition=TCalloutPosition.Left) then
CR.Margins.Right := CR.Margins.Right + 25;
if (ACalloutPosition=TCalloutPosition.Right) then
CR.Margins.Left := CR.Margins.Left + 25;
CR.Height := 75;
CR.XRadius := 25;
CR.YRadius := CR.XRadius;
CR.Position.Y := 999999;
CR.Fill.Kind := TBrushKind.None;
CR.Stroke.Color := TAlphaColorRec.DkGray;
L := TText.Create(Self);
L.TextSettings.FontColor := TAlphaColorRec.White;
L.Parent := CR;
//L.Align := TAlignLayout.Client;
L.Text := AText;
L.Margins.Top := 10;
L.Margins.Right := 15;
L.Margins.Left := 5;
L.Width := CR.Width-20;
L.WordWrap := True;
L.AutoSize := True;
// L.OnPaint := LabelPaint;
FCurrentMessage := L;
CR.Height := L.Height + L.Margins.Top + L.Margins.Bottom + CR.Margins.Top + CR.Margins.Bottom;
if (CR.Height<75) then CR.Height := 75;
L.Align := TAlignLayout.Client;
L.OnResized := LResized;
TmpLayout := TLayout.Create(Self);
TmpLayout.Parent := CR;
TmpLayout.Align := AAlignLayout;
TmpLayout.Width := 75;
TmpImg := TCircle.Create(Self);
TmpImg.Parent := TmpLayout;
TmpImg.Align := TAlignLayout.Top;
TmpImg.Fill.Kind := TBrushKind.Bitmap;
TmpImg.Stroke.Kind := TBrushKind.None;
case AAlignLayout of
TAlignLayout.Left: TmpImg.Fill.Bitmap.Bitmap.Assign(Image1.Bitmap);
TAlignLayout.Right: TmpImg.Fill.Bitmap.Bitmap.Assign(Image2.Bitmap);
end;
TmpImg.Fill.Bitmap.WrapMode := TWrapMode.TileStretch;
TmpImg.Width := 75;
TmpImg.Margins.Left := 15;
TmpImg.Margins.Right := 15;
TmpImg.Margins.Top := 15;
TmpImg.Margins.Bottom := 15;
VSB.ScrollBy(0,-95);
end;
procedure TMainForm.APIKeyButtonClick(Sender: TObject);
begin
APIKeyEdit.Visible := not APIKeyEdit.Visible;
end;
procedure TMainForm.FriendMessage(const S: String);
begin
if FCurrentMessage<>nil then
FCurrentMessage.Text := S
else
AddMessage(S, TAlignLayout.Left, TCalloutPosition.Left);
end;
procedure TMainForm.YourMessage(const S: String);
begin
AddMessage(S, TAlignLayout.Right, TCalloutPosition.Right);
end;
procedure TMainForm.GenerateButtonClick(Sender: TObject);
begin
if APIKeyEdit.Text='' then
begin
ShowMessage('Enter a Replicate.com API key.');
Exit;
end;
// fuyu-8b needs a file loaded
if ModelLabel.Text='42f23bc876570a46f5a90737086fbc4c3f79dd11753a28eaa39544dd391815e9' then
begin
if ImageEdit.Text='' then
begin
ShowMessage('Load a file from disk or URL for this model.');
Exit;
end;
end;
YourMessage(PromptEdit.Text);
FCurrentMessage := nil;
ProgressBar.Value := 0;
ProgressBar.Visible := True;
GenerateButton.Enabled := False;
Application.ProcessMessages;
var LSourceStream := TMemoryStream.Create;
if ImageEdit.Text.Substring(0,4)<>'http' then
begin
if ImageEdit.Text<>'' then
begin
LSourceStream.LoadFromFile(ImageEdit.Text);
end
else
SourceImage.Bitmap.SaveToStream(LSourceStream);
end;
var LPrompt := '"prompt":';
var LImage := '"image":';
// blip-2 uses "question" instead of "prompt"
if ModelLabel.Text='9109553e37d266369f2750e407ab95649c63eb8e13f13b1f3983ff0feb2f9ef7' then
LPrompt := '"question":';
// these models need img instead of image
if ((ModelLabel.Text='c4c54e3c8c97cd50c2d2fec9be3b6065563ccf7d43787fb99f84151b867178fe') OR (ModelLabel.Text='51a43c9d00dfd92276b2511b509fcb3ad82e221f6a9e5806c54e69803e291d6b')) then
LImage := '"img":';
RestRequest1.Params[0].Value := 'Token ' + APIKeyEdit.Text;
RestRequest1.Params[1].Value := TemplateMemo.Lines.Text.Replace('%prompt%',JSONQuote(PromptEdit.Text))
.Replace('%base64%','"'+MemoryStreamToBase64(LSourceStream)+'"')
.Replace('"prompt":',LPrompt)
.Replace('"image":',LImage)
.Replace('%model%',JSONQuote(ModelLabel.Text));
XrayMemo.Lines.Append('POST Request');
XrayMemo.Lines.Append('URL:');
XrayMemo.Lines.Append(RestClient1.BaseURL+#13#10);
XrayMemo.Lines.Append('Payload:');
XrayMemo.Lines.Append(TemplateMemo.Lines.Text.Replace('%prompt%',JSONQuote(PromptEdit.Text))
.Replace('%base64%','"'+'...'+'"')
.Replace('"prompt":',LPrompt)
.Replace('"image":',LImage)
.Replace('%model%',JSONQuote(ModelLabel.Text)));
XrayMemo.Lines.Append('');
RESTRequest1.Execute;
LSourceStream.Free;
XrayMemo.Lines.Append('Response:');
XrayMemo.Lines.Append(RESTResponse1.Content);
XrayMemo.Lines.Append('');
var F := FDMemTable1.FindField('status');
if F<>nil then
begin
if F.AsWideString='starting' then
begin
RESTRequest2.Resource := FDMemTable1.FieldByName('id').AsWideString;
Timer1.Enabled := True;
end
else
begin
ProgressBar.Visible := False;
GenerateButton.Enabled := True;
ShowMessage(F.AsWideString);
end;
end;
end;
procedure TMainForm.LabelPaint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
begin
// if TLabel(Sender).Tag=0 then
// begin
// TCalloutRectangle(TLabel(Sender).Parent).Height := Max(TLabel(Sender).Height,75);
// TLabel(Sender).Tag := 1;
// end;
end;
end.