-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuCard.pas
415 lines (372 loc) · 11.3 KB
/
uCard.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
unit uCard;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
FMX.Objects, FMX.Layouts, 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,
FMX.Controls.Presentation, Data.Bind.EngExt, Fmx.Bind.DBEngExt, System.Rtti,
System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.Components,
Data.Bind.DBScope, FMX.ImgList, REST.Types, REST.Client,
REST.Response.Adapter, Data.Bind.ObjectScope, System.Net.URLClient,
System.Net.HttpClient, System.Net.HttpClientComponent, FMX.Platform,
FMX.Memo.Types, FMX.ScrollBox, FMX.Memo, FireDAC.Stan.StorageBin;
type
TCardFrame = class(TFrame)
Layout1: TLayout;
FeedImage: TImage;
Layout2: TLayout;
MenuButton: TButton;
ProfileCircle: TCircle;
NameLabel: TLabel;
Layout3: TLayout;
DownloadButton: TButton;
LoveButton: TButton;
CommentButton: TButton;
GoButton: TButton;
DescLabel: TLabel;
FollowButton: TButton;
TimeLabel: TLabel;
DataMT: TFDMemTable;
BindSourceDB1: TBindSourceDB;
BindingsList1: TBindingsList;
LinkPropertyToFieldFillBitmapBitmap: TLinkPropertyToField;
LinkPropertyToFieldText: TLinkPropertyToField;
LinkPropertyToFieldText4: TLinkPropertyToField;
LinkPropertyToFieldText5: TLinkPropertyToField;
LinkPropertyToFieldBitmap: TLinkPropertyToField;
Glyph1: TGlyph;
Glyph2: TGlyph;
Glyph3: TGlyph;
Timer1: TTimer;
RESTRequest2: TRESTRequest;
FDMemTable2: TFDMemTable;
RESTResponseDataSetAdapter2: TRESTResponseDataSetAdapter;
RESTResponse2: TRESTResponse;
RESTClient2: TRESTClient;
NetHTTPClient1: TNetHTTPClient;
ProgressBar: TProgressBar;
Layout4: TLayout;
Line1: TLine;
Timer2: TTimer;
EditPromptButton: TButton;
CopyResponseButton: TButton;
CopyLabelButton: TButton;
OutputMemo: TMemo;
LinkControlToField1: TLinkControlToField;
LLMLabel: TLabel;
LinkPropertyToFieldText2: TLinkPropertyToField;
CopyPromptResponseButton: TButton;
CopyPromptButton: TButton;
OAIRESTResponse: TRESTResponse;
OAIMemTable: TFDMemTable;
OAIRESTResponseDataSetAdapter: TRESTResponseDataSetAdapter;
OAIRESTRequest: TRESTRequest;
OAIRESTClient: TRESTClient;
Layout5: TLayout;
procedure FollowButtonClick(Sender: TObject);
procedure LoveButtonClick(Sender: TObject);
procedure CommentButtonClick(Sender: TObject);
procedure GoButtonClick(Sender: TObject);
procedure DownloadButtonClick(Sender: TObject);
procedure CommentsLabelClick(Sender: TObject);
procedure ProfileCircleClick(Sender: TObject);
procedure FeedImageDblClick(Sender: TObject);
procedure FrameResize(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure EditPromptButtonClick(Sender: TObject);
procedure CopyResponseButtonClick(Sender: TObject);
procedure CopyLabelButtonClick(Sender: TObject);
procedure CopyPromptResponseButtonClick(Sender: TObject);
procedure CopyPromptButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FMainFeedMT: TFDMemTable;
FProvider: String;
end;
implementation
{$R *.fmx}
uses
Unit1, System.Threading, System.DateUtils, IdHashMessageDigest, System.IOUtils,
System.JSON, uDM;
procedure TextToClipboard(const AString: String);
var
clp: IFMXClipboardService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService) then
begin
clp := IFMXClipboardService(TPlatformServices.Current.GetPlatformService(IFMXClipboardService));
clp.SetClipboard(AString);
end;
end;
function SaveControl(AControl: TControl): String;
var
LMS: TMemoryStream;
LSS: TStringStream;
LName: string;
begin
LName := AControl.Name;
AControl.Name := '';
AControl.Visible := True;
try
LMS := TMemoryStream.Create;
try
LMS.WriteComponent(AControl);
LMS.Position := 0;
LSS := TStringStream.Create;
try
ObjectBinaryToText(LMS, LSS);
Result := LSS.DataString;
finally
LSS.Free;
end;
finally
LMS.Free;
end;
finally
AControl.Visible := False;
AControl.Name := LName;
end;
end;
function MD5(const AString: String): String;
var
LHash: TIdHashMessageDigest5;
begin
LHash := TIdHashMessageDigest5.Create;
try
Result := LHash.HashStringAsHex(AString);
finally
LHash.Free;
end;
end;
procedure TCardFrame.DownloadButtonClick(Sender: TObject);
begin
// Download
MainForm.SaveDialog.FileName := DescLabel.Text + '.png';
if MainForm.SaveDialog.Execute then
begin
FeedImage.Bitmap.SaveToFile(MainForm.SaveDialog.FileName);
end;
end;
procedure TCardFrame.EditPromptButtonClick(Sender: TObject);
begin
MainForm.PromptMemo.Lines.Text := DescLabel.Text;
end;
procedure TCardFrame.CommentButtonClick(Sender: TObject);
begin
// Comment
end;
procedure TCardFrame.CommentsLabelClick(Sender: TObject);
begin
// View Comments
end;
procedure TCardFrame.CopyLabelButtonClick(Sender: TObject);
begin
TextToClipboard(SaveControl(LLMLabel));
end;
procedure TCardFrame.CopyPromptButtonClick(Sender: TObject);
begin
TextToClipboard(DescLabel.Text);
end;
procedure TCardFrame.CopyPromptResponseButtonClick(Sender: TObject);
begin
TextToClipboard(DescLabel.Text + #13#10 + OutputMemo.Lines.Text);
end;
procedure TCardFrame.CopyResponseButtonClick(Sender: TObject);
begin
TextToClipboard(OutputMemo.Lines.Text);
end;
procedure TCardFrame.FeedImageDblClick(Sender: TObject);
begin
// Double Tap Like
LoveButtonClick(Sender);
end;
procedure TCardFrame.FollowButtonClick(Sender: TObject);
begin
// Follow
end;
procedure TCardFrame.FrameResize(Sender: TObject);
begin
FeedImage.Width := Self.Width;
end;
procedure TCardFrame.GoButtonClick(Sender: TObject);
begin
// Go
end;
procedure TCardFrame.LoveButtonClick(Sender: TObject);
begin
// Like
end;
procedure TCardFrame.ProfileCircleClick(Sender: TObject);
begin
// View Profile
end;
function ConcatJSONStrings(const JSONArray: string): string;
var
JSONData: TJSONArray;
I: Integer;
begin
if JSONArray[1]='[' then
begin
Result := '';
JSONData := TJSONObject.ParseJSONValue(JSONArray) as TJSONArray;
try
for I := 0 to JSONData.Count - 1 do
Result := Result + JSONData.Items[I].Value;
finally
JSONData.Free;
end;
end
else
Result := JSONArray;
end;
function ConcatJSONTexts(const JSONArray: string): string;
var
JSONData: TJSONArray;
JSONObj: TJSONObject;
I: Integer;
begin
Result := '';
JSONData := TJSONObject.ParseJSONValue(JSONArray) as TJSONArray;
try
for I := 0 to JSONData.Count - 1 do
begin
JSONObj := JSONData.Items[I] as TJSONObject;
Result := Result + JSONObj.GetValue<string>('text');
end;
finally
JSONData.Free;
end;
end;
function GetMessageContent(const JSONArray: string): string;
var
JSONData: TJSONArray;
MessageObj: TJSONObject;
begin
Result := '';
JSONData := TJSONObject.ParseJSONValue(JSONArray) as TJSONArray;
try
MessageObj := (JSONData.Items[0] as TJSONObject).GetValue<TJSONObject>('message');
Result := MessageObj.GetValue<string>('content');
finally
JSONData.Free;
end;
end;
procedure TCardFrame.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
if FProvider='replicate' then
begin
RESTRequest2.Params[0].Value := 'Token ' + MainForm.APIKeyEdit.Text;
RESTRequest2.Resource := Self.Hint;
RESTRequest2.Execute;
var LStatus := FDMemTable2.FieldByName('status').AsWideString;
if LStatus='succeeded' then
begin
var LOutput := ConcatJSONStrings(FDMemTable2.FieldByName('output').AsWideString);//.Replace('["','').Replace('"]','').Replace('\/','/');
if LOutput<>'' then
begin
ProgressBar.Visible := False;
OutputMemo.Visible := True;
DataMT.Edit;
DataMT.FieldByName('Output').AsString := LOutput;
DataMT.FieldByName('Posted').AsDateTime := Now;
DataMT.Post;
FMainFeedMT.Append;
FMainFeedMT.CopyRecord(DataMT);
FMainFeedMT.Post;
end
else
begin
OutputMemo.Visible := True;
DataMT.Edit;
DataMT.FieldByName('Output').AsWideString := 'failed';
DataMT.Post;
ProgressBar.Visible := False;
end;
Timer1.Enabled := False;
end
else
begin
if ProgressBar.Value=ProgressBar.Max then
ProgressBar.Value := ProgressBar.Min
else
ProgressBar.Value := ProgressBar.Value+5;
Timer1.Enabled := True;
end;
end
else
if (FProvider='openai') OR (FProvider='chatgpt') then
begin
TTask.Run(procedure begin
try
OAIRESTRequest.Params[0].Value := 'Bearer ' + MainForm.OAAPIKeyEdit.Text;
if (FProvider='openai') then
begin
OAIRESTClient.BaseURL := 'https://api.openai.com/v1/completions';
OAIRESTRequest.Params[1].Value := MainForm.OAPredictionMemo.Lines.Text.Replace('%prompt%',MainForm.PromptMemo.Lines.Text)
.Replace('%max_tokens%',MainForm.MaxTokensMemo.Lines.Text)
.Replace('%model%',MainForm.VersionEdit.Text);
end;
if (FProvider='chatgpt') then
begin
OAIRESTClient.BaseURL := 'https://api.openai.com/v1/chat/completions';
OAIRESTRequest.Params[1].Value := MainForm.OAGPTPredictionMemo.Lines.Text.Replace('%prompt%',MainForm.PromptMemo.Lines.Text)
.Replace('%max_tokens%',MainForm.MaxTokensMemo.Lines.Text)
.Replace('%model%',MainForm.VersionEdit.Text);
end;
OAIRESTRequest.Execute;
TThread.Synchronize(nil,procedure begin
Self.Hint := OAIMemTable.FieldByName('id').AsWideString;
var LChoices := '';
if (FProvider='openai') then
LChoices := ConcatJSONTexts(OAIMemTable.FieldByName('choices').AsWideString);
if (FProvider='chatgpt') then
LChoices := GetMessageContent(OAIMemTable.FieldByName('choices').AsWideString);
if LChoices<>'' then
begin
OutputMemo.Visible := True;
DataMT.Edit;
DataMT.FieldByName('Output').AsString := LChoices;
DataMT.FieldByName('Posted').AsDateTime := Now;
DataMT.Post;
FMainFeedMT.Append;
FMainFeedMT.CopyRecord(DataMT);
FMainFeedMT.Post;
end
else
begin
OutputMemo.Visible := True;
DataMT.Edit;
DataMT.FieldByName('Output').AsWideString := 'failed';
DataMT.Post;
end;
end);
finally
TThread.Synchronize(nil,procedure begin
ProgressBar.Visible := False;
//StatusLabel.Text := 'Status: ' + LStatus;
end);
end;
end);
end;
end;
procedure TCardFrame.Timer2Timer(Sender: TObject);
begin
if ProgressBar.Visible = False then
begin
Timer2.Enabled := False;
end
else
begin
if ProgressBar.Value=ProgressBar.Max then
ProgressBar.Value := ProgressBar.Min
else
ProgressBar.Value := ProgressBar.Value+5;
end;
end;
end.