-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_v2.py
1471 lines (1266 loc) · 49.2 KB
/
server_v2.py
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import base64
import hashlib
import math
import random
import shutil
import subprocess
import sys, os
import urllib
from urllib.parse import quote
import ffmpeg
#from langchain_community.llms.ollama import Ollama
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from flask import Flask, request, send_file, jsonify, render_template, send_from_directory
from flask_cors import CORS
import yaml
import torch
import glob
import moztts
import re
import markdown
from searxing import SearXing
#from langchain_community.llms import Ollama as OllamaLangchain
#from ollama_langchain import OllamaLangchain
import sys
import datetime
import signal
import requests
import json
import whisper
VERSION='2'
app = Flask(__name__)
CORS(app)
MODEL_PATH="models/"
last_loader=""
requrl="http://127.0.0.1"
with open('oai.yaml') as f:
oai_config = yaml.safe_load(f)
with open('tts_config.yaml') as f:
tts_config = yaml.safe_load(f)
global mozTTS
min_response_tokens = 4
break_on_newline = True
LOG_DIR = "logs/"
LOG_FILE = "_logs.txt"
MINIMUM_MEMORABLE = 4
MIN_KW_LENGTH = 4
current_log_file = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
""" read default values """
with open(f'model_configs/defaults.yaml') as f:
default_model_config = yaml.safe_load(f)
with open('system_config.yaml') as f:
system_config = yaml.safe_load(f)
""" read Templates """
# if system_config['do_wav2lip']:
# with open('./templates/chat_line_animated.tpl.html') as f:
# chat_line="\n".join(f.readlines())
# else:
with open('./templates/chat_line.tpl.html') as f:
chat_line="\n".join(f.readlines())
def write_system_config():
"""
Write the system_config dictionary to the system_config.yaml file.
:return: None
"""
global system_config
with open('system_config.yaml', 'w') as f:
# Write the dictionary to the file
yaml.dump(system_config, f)
def generate_ability_string():
"""
Generate ability string. Disabled for now.
:return: None
"""
global persona_config, abilities, ability_string
with open('abilities.yaml') as f:
abilities = yaml.safe_load(f)
ability_string = f"For {persona_config['name']}: these additional actions are available to you \n"
for k in abilities:
ability_string += f'{abilities[k]} by saying "CMD_{k}: [insert Keywords or URL here]"\n'
ability_string += 'Use only one command at a time!'
print(ability_string)
#### OVERRIDE :
ability_string = ''
def save_log(userline, botline):
"""
:param userline: The line of text representing the user's input.
:param botline: The line of text representing the bot's response.
:return: None
"""
global current_log_file
try:
if system_config["log"] is True:
with open(f'logs/{current_log_file}.txt', 'a') as f:
f.write(f'user: {userline}\n{botline}\n\n')
except:
print("Error writing Log.")
def tabby_load_model(model):
"""
Load the specified model.
:param model: The name of the model to load.
:return: None
Example usage:
>>> tabby_load_model("my_model")
Loading my_model
200
{"message": "Model loaded successfully"}
"""
global model_config, loaded_model
print(f"Loading {model}")
url = f"{oai_config['tby_server']}/v1/model/load"
payload = {
"name": model,
"max_seq_len": model_config['max_seq_len'],
"gpu_split_auto": True,
"gpu_split": [
0
],
"rope_scale": 1,
"rope_alpha": model_config['alpha_value'],
"no_flash_attention": True,
"low_mem": False,
# "draft": {
# "draft_model_name": "string",
# "draft_rope_alpha": 1
# }
}
headers = {
"Content-Type": "application/json",
"x-admin-key": oai_config['tby_admin']
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
loaded_model = model
print(response.status_code)
print(response.text)
def tabby_unload():
"""
Unloads the Tabby model.
:return: None
"""
print(f"Unloading {model}")
url = f"{oai_config['tby_server']}/v1/model/unload"
print(url)
headers = {
"Content-Type": "application/json",
"x-admin-key": oai_config['tby_admin']
}
response = requests.post(url, headers=headers)
def ollama_restart():
subprocess.run(["sudo", "/bin/systemctl", "restart", "ollama"])
def ollama_generate(original_prompt, client):
global persona_config,system_config, historyMP, usernames
rs = {
"model": persona_config["model"]+":"+persona_config["tag"],
"messages": [],
"stream": False,
"context": "",
"keep_alive": 100000,
"options": {}
}
with open(f"parameters/{model_config['loader']}_{persona_config['parameters']}.yaml") as f:
rs['options'] = yaml.safe_load(f)
seed = random.randint(math.ceil(sys.float_info.min), math.floor(sys.float_info.max))
print(f"Seed: {seed}")
rs["options"]["seed"]=seed
rs["options"]["num_predict"] = persona_config['max_answer_token']
if 'stop' in model_config.keys():
rs["options"]["stop"] = model_config["stop"].split(',')
e = {
"role": ""
"content" ""
}
current_time = datetime.datetime.now()
now = current_time.strftime("%H:%M:%S on %A, %d %B %Y")
rs["context"] = f"It is {now}\n.The user's name is {usernames[client]}.\n{persona_config['context']}"
if original_prompt.strip() != "":
rs = generate_history_array(rs,client)
else:
e["role"] = "user"
e["content"] = "please continue."
rs = generate_history_array(rs, client)
rs["messages"].append(e)
url = f"{oai_config['ollama_server']}/api/chat"
headers = {
"Content-Type": "application/json",
}
# print( "\n:::::::::\n")
# print(json.dumps(rs, indent=4))
# print( "\n:::::::::\n")
# response = requests.post(url, data=json.dumps(rs, indent=0))
rs_stream = requests.post(url, data=json.dumps(rs, indent=0), stream=False)
response = ""
js = ""
if rs_stream.ok:
for line in rs_stream.iter_lines():
if line: # filter out keep-alive new lines
js = json.loads(line.decode('utf-8'))
# print("stream",js)
response += js["message"]["content"]
if js['done']:
break
else:
print('Error:', rs_stream.status_code)
print("Last response : ",js)
# rsjson = json.loads(response.text)
try:
prompt_speed = "{0:.2f}".format((js['prompt_eval_count']/float(js['prompt_eval_duration']/100000000)))
answer_speed = "{0:.2f}".format((js['eval_count']/float(js['eval_duration']/100000000)))
print( f"ollama: token stats:\n {prompt_speed} t/s)\n {answer_speed} t/s)\n")
except:
print( "Could not compute token stats")
# return rsjson["message"]["content"]
return response
def tabby_generate(original_prompt,client):
"""
Generate a response using the Tabby API.
:param original_prompt: The prompt for generating the response.
:return: The generated response.
"""
global persona_config,system_config, historyMP, usernames
with open(f"parameters/{model_config['loader']}_{persona_config['parameters']}.yaml") as f:
parameters = yaml.safe_load(f)
current_time = datetime.datetime.now()
now = current_time.strftime("%H:%M:%S on %A, %d %B %Y")
if original_prompt.strip() != "":
prompt = f"It is {now}\n.The user's name is {usernames[client]}.\n{persona_config['context']}\nThe chat so far :\n" + generate_history_string(client) + "\n" + \
model_config["bot"]
else:
prompt = "The chat so far :\n" + generate_history_string(client) + "\nplease continue." + model_config["bot"]
seed = random.randint(math.ceil(sys.float_info.min), math.floor(sys.float_info.max))
print(f"Seed: {seed}")
parameters["seed"] = seed
parameters['model']=persona_config['model']
parameters['prompt']=prompt
parameters['max_tokens']=persona_config['max_answer_token']
parameters['user']=usernames[client]
if 'stop' in model_config.keys():
parameters["stop"] = model_config["stop"].split(',')
url = f"{oai_config['tby_server']}/v1/completions"
headers = {
"Content-Type": "application/json",
"x-api-key": oai_config['tby_admin']
}
answer = ""
while answer.strip() == "":
response = requests.post(url, data=json.dumps(parameters, indent=0), headers=headers)
print(response.status_code)
print("response : ",response.text)
rsjson = json.loads(response.text)
answer = rsjson["choices"][0]['text']
return answer
def word_count(prompt):
return len(prompt.split())
def tabby_count_tokens(prompt):
"""
:param prompt: The text prompt to be tokenized.
:return: The number of tokens in the given prompt.
"""
if model_config['loader'] != 'tabby':
return word_count(prompt)
url = f"{oai_config['tby_server']}/v1/token/encode"
headers = {
"Content-Type": "application/json",
"x-api-key": oai_config['tby_admin']
}
body = {
"add_bos_token": True,
"encode_special_tokens": True,
"decode_special_tokens": True,
"text": prompt
}
raw = requests.post(url, data=json.dumps(body, indent=4), headers=headers)
response=json.loads(raw.text)
return response["length"]
generators = {
'tabby': tabby_generate,
'ollama': ollama_generate
}
# Used for making text xml compatible, needed for voice pitch and speed control
table = str.maketrans({
"<": "<",
">": ">",
"&": "&",
"'": "'",
'"': """,
})
def count_tokens(txt):
"""
:param txt: the string to count tokens from
:return: the number of tokens in the string
"""
if model_config['loader'] != 'tabby':
return word_count(txt)
return tabby_count_tokens(txt)
def generate_chat_lines(user, bot, upimg_name=None, genimg_name=None):
"""
:param user: The user's message to be replaced in the chat line.
:param bot: The bot's response to be replaced in the chat line.
:return: The generated chat line with user and bot messages replaced.
"""
## moving the animation to the top of the screen and keeping the portrait
## in the chatlines
if upimg_name is not None:
with open('templates/upload.tpl.html', "r") as f:
uploadTpl = f.read()
user = (uploadTpl.replace("%%imgname%%", upimg_name.replace("/","").replace("\\","")).
replace("%%user%%", user)).replace("%%rnd%%",str(random.randint(0,99999999999)))
if genimg_name is not None:
with open('templates/genimg.tpl.html', "r") as f:
uploadTpl = f.read()
bot = (uploadTpl.replace("%%imgname%%", genimg_name.replace("/","").replace("\\","").replace("generated","generated/")).
replace("%%bot%%", bot)).replace("%%rnd%%",str(random.randint(0,99999999999)))
rs = (chat_line.replace("%%user%%", user)\
.replace("%%bot%%", bot)\
.replace('%%botname%%', persona_config["name"])
.replace("%%rnd%%", str(random.randint(0,99999999999))))
return rs
"""
finds out at which index the history buffer contains at least half of the context
this is needed to create memories and to truncate the history buffer accordingly
"""
def find_half_of_context(client):
"""
Find the cutting point in the history list that corresponds to half the number of tokens in the model configuration.
:return: The cutting point index in the history list.
"""
global historyMP, model_config
half = model_config['max_seq_len']/2
print(f"looking for {half} tokens ...")
cutting_point = 0
token_count = 0
for e in historyMP[client]:
token_count += count_tokens(e[1])
if token_count > half:
return cutting_point
cutting_point += 1
return cutting_point
def generate_history_array( rs, client):
global model_config, historyMP, persona_config
for h in historyMP[client]:
e = {
'role': '',
'content': ''
}
if h[0]=="s":
e["role"] = "system"
e["content"] = h[1]
elif h[0]=="u":
e["role"] = "user"
e["content"] = h[1]
else:
e["role"] = "assistant"
e["content"] = h[1]
rs["messages"].append(e)
return rs
def generate_history_string(client):
"""
Generates a formatted string representing the conversation history.
:return: A string representing the conversation history.
"""
global model_config, historyMP
rs = ""
for h in historyMP[client]:
if h[0]=="s":
if "system" in model_config:
rs += model_config["system"].replace("%%prompt%%",h[1])+"\n"
else:
rs += model_config["user"].replace("%%prompt%%",h[1])+"\n"
elif h[0]=="u":
rs += model_config["user"].replace("%%prompt%%",h[1]) + "\n"
else:
rs += model_config["bot"]+" "+h[1] + "\n"
return rs
def truncate_history(client):
"""
Truncates the chat history to half its original length and adds a summary of the discussion to the remaining history.
:return: None
"""
global historyMP, persona_config
current_history = historyMP[client]
print("len full:",count_tokens(generate_history_string(client)))
half = find_half_of_context(client)
print("half :", half)
historyMP[client] = historyMP[client][:half]
print("len half:",count_tokens(generate_history_string(client)))
summary = generate_memory(client)
historyMP[client] = current_history[half:]
print("len half after cut :",count_tokens(generate_history_string(client)))
historyMP[client].insert(0, "\nsummary of the discussion this far :"+summary+"\ n")
print("len with context :",count_tokens(generate_history_string(client)))
print("len with context :",count_tokens(generate_history_string(client)))
def generate_keywords(client, prompt=""):
"""
Generate keywords based on the provided prompt.
:param prompt: optional prompt string (default: "")
:return: tuple containing the summary and keywords generated
"""
summary=prompt
_keywords = []
if prompt == "":
summary=generate("Please summarize the discussion this far.", client,True)
print('summary generated : ', summary)
_keywords=generate ( "give me single keywords for this paragraph:"+summary, client,True)
keywords={}
for k in _keywords:
if len(k) < MIN_KW_LENGTH:
continue
keywords.append(k)
return(summary, keywords)
def generate_memory(client):
"""
Generate Memory
This method generates memory based on the retrieved summary and keywords. It stores the memory in the global `history` variable.
:return: The generated summary of the memory.
"""
global historyMP
(summary, keywords) = generate_keywords(client)
return summary
def context_management(client):
"""
This method is used for context management. It checks if the number of tokens in the history string generated is greater than or equal to the maximum allowed sequence length minus the
* maximum number of answer tokens and the modulo of the maximum sequence length with 10. If the condition is true, it prints "Context Overflow" and truncates the history.
:return: None
"""
if count_tokens(generate_history_string( client)) >= (model_config['max_seq_len']-(persona_config['max_answer_token']+(model_config['max_seq_len']%10))):
print("Context Overflow.")
truncate_history(client)
def load_model(model):
"""
:param model: The name of the model to load.
:return: None
This method is used to load a specific model using the provided name. It first loads the model configuration from a YAML file based on the given model name. If the file does not exist
* or cannot be loaded, an empty dictionary is used as the model configuration.
Next, it checks if any keys in the default model configuration are missing in the loaded model configuration and adds them if necessary. It also replaces placeholders in the 'bot' and
* 'user' values of the model configuration with the appropriate values from the persona and system configurations, respectively.
If the loaded model is different from the model currently stored in the persona configuration, the current model is unloaded and the specified model is loaded using the 'tabby_load_model
*' function.
Finally, the loaded model is printed for verification.
Example usage:
load_model("model_name")
"""
global model_config, system_config, persona_config, loaded_model, last_loader
## Load Model Configuration (maximum context, template, ...)
try:
with open(f'model_configs/{model.replace(":","_")}.yaml') as f:
model_config = yaml.safe_load(f)
except:
model_config = {}
return
# just unload any potentially loaded models from tabby if we are using ollama
if model_config["loader"] == "tabby" and last_loader == "ollama":
last_loader = "tabby"
ollama_restart()
elif model_config["loader"] == "ollama":
try:
tabby_unload()
except:
pass
last_loader = "ollama"
loaded_model = persona_config["model"]
print("\n\nloaded model :", loaded_model, "\n")
return
for k in default_model_config.keys():
if k not in model_config.keys():
model_config[k] = default_model_config[k]
model_config['bot'] = model_config['bot'].replace("%%botname%%", persona_config["name"])
model_config['user'] = model_config['user'].replace("%%username%%", system_config["username"])
if(loaded_model != persona_config["model"]):
tabby_unload()
tabby_load_model(model)
loaded_model = persona_config["model"]
print("\n\nloaded model :", loaded_model,"\n")
def configure(persona, client=None):
"""
:param persona: The name of the persona to configure. This is used to load the corresponding persona configuration file located in the "personas" directory.
:return: The result of calling the generate_chat_lines method with the configured prompt and cut_output.
"""
global last_loader, old_persona, persona_config, generator, initialized,tokenizer, \
cache, parameters, model_config, mozTTS, system_config, pf, redo_persona_context, \
redo_greetings, persona_dbid, loaded_model
try:
with open(f'./personas/{persona}.yaml') as f:
persona_config = yaml.safe_load(f)
except:
if not initialized:
with open(f'./personas/{system_config["fallback"]}.yaml') as f:
persona_config = yaml.safe_load(f)
else:
generate_tts("Sorry. There is no persona with that name.")
return generate_chat_lines(f"I summon {persona}.","Sorry. There is no persona with that name.")
if initialized:
old_persona = persona_config["name"]
else:
old_persona = "sleeping"
system_config['persona'] = persona
write_system_config()
if "language" not in persona_config:
persona_config['language'] = ''
#generate_ability_string()
# if initialized:
# generate_memory()
amnesia( None)
print(persona_config["model"])
if 'parameters' not in persona_config:
persona_config['parameters'] = 'default'
#with open(MODEL_PATH+persona_config['model']+'/config.json') as f:
# cfg = json.load(f)
mozTTS = moztts.MozTTS()
mozTTS.load_model(persona_config['voice'])
initialized = True
last_character = persona_config["name"]
load_model(persona_config['model'])
# do the greetings
prompt = f'{persona_config["context"]}\n You are {last_character}\n'
if system_config['do_greeting'] is True:
cut_output = persona_config['greeting']
else:
cut_output = f"{persona_config['name']} appears."
redo_persona_context = True
#context_management()
generate_tts(cut_output)
save_log(prompt, cut_output)
return generate_chat_lines(prompt, htmlize(cut_output))
def htmlize(text):
"""
Convert the plaintext `text` into HTML format.
:param text: The plaintext to be converted.
:return: The converted HTML string.
"""
# pattern = "```(.+?)```"
# text = re.sub(pattern,"<code>\1</code>",text)
text = markdown.markdown(text, extensions=['fenced_code', 'codehilite'])
text = text.replace("\\n","<br/>")
return text
def fixHash27(s):
"""
Fixes the hash symbol encoded as "'" and replaces it with the character "'".
:param s: the input string with encoded hash symbol "'"
:return: the fixed string with hash symbol replaced
"""
s=s.replace("'","'")
return s
def xmlesc(txt):
"""
:param txt: The input text to be escaped
:return: The escaped text
"""
return txt.translate(table)
def run_w2l_blocking( target):
command = "./w2l.sh"
subprocess.call([command, target])
def run_w2l_api( target):
global persona_config
if "disable_w2l" in persona_config and persona_config["disable_w2l"] == True:
print("\n\nbypassing W2L\n\n")
try:
os.remove("/media/GINTONIC/AIArtists/beezlechat/video/result.mp4")
except:
pass
video = ffmpeg.input(f"video/{target}_idle.mp4").video
audio = ffmpeg.input("/media/GINTONIC/AIArtists/beezlechat/audio/tts.wav").audio
out = ffmpeg.output(video, audio, "/media/GINTONIC/AIArtists/beezlechat/video/result.mp4", vcodec='copy',
acodec='aac', strict='experimental')
out.run()
return
rs={
"checkpoint_path": "checkpoints/wav2lip.pth",
"face": f"/media/GINTONIC/AIArtists/Wav2Lip/targets/{target}_talk_long.mp4",
"audio": "/media/GINTONIC/AIArtists/beezlechat/audio/tts.wav",
"outfile": "/media/GINTONIC/AIArtists/beezlechat/video/result.mp4"
}
requests.post(oai_config['w2l_endpoint'], data=json.dumps(rs, indent=0))
def generate_tts(string):
"""
:param string: The text to be converted to speech.
:return: The path of the generated audio file.
"""
global persona_config, mozTTS, system_config
if system_config['do_tts'] != True:
return
string = fixHash27(string)
original_string = string
output_file = "./audio/tts.wav"
if string == '':
string = '*Empty reply, try regenerating*'
else:
output_file = './audio/tts.wav'
if os.path.exists(output_file):
os.unlink(output_file)
mozTTS.moztts(string, persona_config['voice'], persona_config['speaker'], persona_config['language'], output_file)
if system_config['do_wav2lip']:
while not os.path.exists(output_file):
pass
try:
run_w2l_api(persona_config['name'])
except:
print("w2l failed.")
system_config['do_wav2lip'] = False
return output_file
def generate_image(prompt, client):
global persona_config, system_config, oai_config
if persona_config["generate_img"] == False:
generate_tts("I'm sorry, but there is no space left to load an image generator.")
return generate_chat_lines(f"{prompt}", f"I'm sorry, but there is no space left to load an image generator.")
pattern = ".*generate an image of (.+)$"
matches = re.match(pattern, prompt)
if matches is not None:
clean_prompt = matches.group(1)
else:
pattern = ".*\/imagine (.+)$"
matches = re.match(pattern, prompt)
if matches is not None:
clean_prompt = matches.group(1)
else:
print("Looking for a topic.")
topic = generate("what is the topic of this page? summarize the content of this random wikipedia page https://en.wikipedia.org/wiki/Special:Random", client, True)
clean_prompt = f" something about the topic mentioned in \"{topic}\""
sdprompt = generate(f"write a stable diffusion prompt to generate a high quality image of {clean_prompt}. Add keywords about the medium (for example: photography, drawing, painting, 3D, ...)"
f"style (for example: photo realistic, cartoon, anime, surrealistic, pointillism, etc), detail level and lighting at the end of the prompt.", client, True)
print(f"prompt : '{clean_prompt}'")
with (open("invoke/t2i-generation.json", "r") as f):
invokePrompt = json.load(f)
sdprmpt = sdprompt.replace("\n","").replace("\"","'")
invokePrompt["prompt"] = invokePrompt["prompt"].replace("%%PROMPT%%", sdprmpt)
invokePrompt["checkpoint"] = invokePrompt["checkpoint"].replace("%%MODEL%%", persona_config["checkpoint"])
invokePrompt["firstphase_width"] = persona_config["image_size"]
invokePrompt["firstphase_height"] = persona_config["image_size"]
print(f"\n:::::::::::\n{json.dumps(invokePrompt, indent=4)}\n::::::::::::::\n")
rs_raw = requests.post(f'{oai_config["a1111"]}sdapi/v1/txt2img', json=invokePrompt, headers={"Content-Type": "application/json"})
rs = json.loads(rs_raw.text)
# Get current date as a string
current_date = str(datetime.datetime.now())
# Combine data with current date
combined_data = f"{sdprmpt}{current_date}"
# Generate SHA256 hash
hash_object = hashlib.sha256(combined_data.encode())
hex_dig = hash_object.hexdigest()
fname = f"generated/{hex_dig}.png"
img = base64.b64decode(rs['images'][0])
try:
with open(f"uploads/{fname}", 'wb') as f:
f.write(img)
except:
generate_tts("Something went wrong!")
return generate_chat_lines(prompt, "Something went wrong.", client)
generate_tts(f"Here is what I came up with : '{sdprompt}'")
genlines = generate_chat_lines(f"{prompt}", f"Here is what I came up with : '{sdprompt}'", None, fname)
if persona_config['look_at_gen'] == True:
analysis = image_analysis(rs['images'][0], fname, client, "This is the image that was generated based on your prompt.")
generate_tts(f"Here is what I came up with : '{sdprompt}'")
return genlines
def check_meta_cmds(prompt, client):
"""
Check if prompt matches any meta commands and return a corresponding action.
:param prompt: the input prompt to be checked
:return: a tuple containing the command and additional parameters if applicable
"""
cmd = None
news_sources = [
'https://news.yahoo.com',
'https://news.bbc.co.uk',
'https://www.aljazeera.com',
'https://www.npr.org',
'https://www.tageschau.de',
'https://news.google.com',
'https://en.wikinews.org/wiki/Main_Page'
]
display_personas = [
'who can i talk to?'
]
clean_prompt = prompt.lower().strip()
print("\n\nClean Prompt :", clean_prompt, "\n\n")
if clean_prompt == ("forget everything"):
return (amnesia, None)
if clean_prompt in display_personas:
print("display_personas")
return (personas_table, None)
if clean_prompt.startswith("check the news."):
source = random.choice(news_sources)
print(f"\nNews Source selected : {source}\n")
return (None,f"Read {source}. summarize and give me your thoughts about the five first headlines. Translate them to English if they aren't in English")
if "generate an image of" in clean_prompt or "/imagine" in clean_prompt:
print("imagine found.")
return (generate_image, clean_prompt)
if "ask wikipedia about " in clean_prompt:
pattern = ".*ask wikipedia about [\'\"]([ a-zA-Z0-9_\-\']+)[\'\"].*"
matches = re.match(pattern, clean_prompt)
if matches is not None:
query = matches.group(1)
return (None, Searxing.ask_wikipedia_search(query))
else:
pattern = ".*ask wikipedia about ([a-zA-Z0-9_\-\']+)[^\w]*"
matches = re.match(pattern, clean_prompt)
if matches is not None:
query = matches.group(1)
return (None, Searxing.ask_wikipedia_search(query))
if clean_prompt.startswith("switch to "):
model_name_raw = clean_prompt.split("switch to ")[1]
model_name = model_name_raw
model_tag = "latest"
if ":" in model_name_raw:
model_name = model_name_raw.split(":")[0]
model_tag = model_name_raw.split(":")[1]
persona_config["model"] = model_name
persona_config["tag"] = model_tag
load_model(model_name)
return(None,None)
pattern = ".*[iI] summon ([a-zA-Z_0-9\-]+).*"
matches = re.match(pattern, clean_prompt)
if matches is not None and len(matches.groups()) > 0:
return (configure, matches.group(1).lower().capitalize())
pattern = ".*my name is ([a-zA-Z_0-9]+).*"
matches = re.match(pattern, prompt)
if matches is not None and len(matches.groups()) > 0:
set_username(matches.group(1).lower().capitalize(), client)
return (None,None)
if "##shutdown now##" in prompt.lower():
shutdown_action()
exit()
return (cmd, None)
def generate(prompt, client, raw=False):
global generators, redo_persona_context, persona_config, token_count, model_config, parameters, tokenizer, generator, initialized, ability_string, historyMP, usernames
if initialized == False:
configure(system_config['persona'])
if client not in historyMP:
initialize_historyMP(client)
(cmd, prmtr) = check_meta_cmds(prompt, client)
if cmd != None:
if prmtr != None:
return htmlize(cmd(prmtr, client))
else:
return htmlize(cmd(client))
elif prmtr != None:
prompt = prmtr
original_prompt = prompt
if model_config['loader'] == 'ollama' and prompt.startswith("(langchain)"):
olc = OllamaLangchain()
fn = Searxing.extract_file_name(prompt)
url = Searxing.extract_url(prompt)
q = Searxing.extract_query(prompt)
vector = None
do_langchain = True
if url != "":
vector = olc.open_url(url=url, ollama_server=oai_config['ollama_server'],model_tag=f"{persona_config['model']}:{persona_config['tag']}")
elif fn != "":
vector = olc.open_pdf(path=fn, ollama_server=oai_config['ollama_server'],model_tag=f"{persona_config['model']}:{persona_config['tag']}")
elif q != "":
url = f"{Searxing.config['searx_server']}?q={q}&format=json"
vector = olc.open_url(url=url, ollama_server=oai_config['ollama_server'],
model_tag=f"{persona_config['model']}:{persona_config['tag']}")
else:
do_langchain = False
if do_langchain:
ctx = ""
# if (redo_persona_context):
# ctx = persona_config['context']
# if system_config['do_greeting']:
# ctx = persona_config['greeting'] + ctx
rs_raw = olc.query(vectorstore=vector,prompt=ctx+"\n"+prompt, ollama_server=oai_config['ollama_server'],model_tag=f"{persona_config['model']}:{persona_config['tag']}")
print("\n\n___________________\n",rs_raw,"\n___________________\n\n")
rs = rs_raw["result"]
generate_tts(rs)
historyMP[client].append(["u", prompt])
historyMP[client].append(["b", rs])
return generate_chat_lines(prompt, htmlize(rs))
searxPrompt = Searxing.check_for_trigger(prompt, model_config['max_seq_len']/2, count_tokens)
if searxPrompt == '':
searxPrompt = prompt
if( redo_persona_context):
ctx = persona_config['context']
if system_config['do_greeting']:
ctx = persona_config['greeting']+ctx
# save the user prompt in the history (with or without model-specific prompt formats)
# history.append(model_config["user"].replace("%%prompt%%", searxPrompt+"\n"))
historyMP[client].append(["u",searxPrompt])
if model_config['loader'] == 'tabby':
context_management(client)
cut_output = generators[model_config['loader']](original_prompt, client)
print(cut_output)
if raw:
return cut_output
historyMP[client].append(["b",cut_output])
if model_config['loader'] == 'tabby':
token_count = count_tokens(generate_history_string(client))
print(f'Token count: {token_count}')
generate_tts(cut_output)
save_log(original_prompt, cut_output)
return generate_chat_lines(original_prompt, htmlize(cut_output))
def image_analysis(img, img_name, client, user_prompt=None):
global persona_config, system_config, historyMP, usernames
url = f"{oai_config['ollama_server']}/api/chat"
print("len: ", len(img))
headers = {
"Content-Type": "application/json",
}
rs = {
"model": system_config['multimodal'],
"stream": False,
"messages": [{
"role":"user",
"content": "describe this image in great details, please.",
"images": [img]
}],
"options": {}
}
with open(f"parameters/ollama_{persona_config['parameters']}.yaml") as f:
rs['options'] = yaml.safe_load(f)
seed = random.randint(math.ceil(sys.float_info.min), math.floor(sys.float_info.max))
rs["options"]["seed"] = seed
rs["options"]["stop"] = ['USER:','</s>']
#rsai = requests.post(url, headers=headers, data=json.dumps(rs, indent=0))
# print(rsai.json())
# response = rsai.json()['message']['content']
jsdata = json.dumps(rs, indent=2)
rs_stream = requests.post(url, headers=headers, data=jsdata, stream=False)
response = rs_stream.json()['message']['content']
if client not in historyMP:
initialize_historyMP(client)
user_output = user_prompt
if user_prompt == None:
user_prompt = "[System: This is your analysis of a file the user just uploaded.]"
user_output ="describe this image in great details, please."
historyMP[client].append(["u", user_prompt])
historyMP[client].append(["b",response])
generate_tts(response)
return generate_chat_lines(user_output, response, urllib.parse.quote(img_name))
def initialize_historyMP(client):
global system_config, historyMP
historyMP[client] = []
usernames[client] = system_config['username']
def list_personas():
"""
Return a formatted string containing HTML code for a dropdown list of personas.
:return: A string containing HTML code for the dropdown list of personas.
"""
global persona_config
personas = glob.glob("./personas/*.yaml")
personas.sort()
rs = ""
persona = ""
for p_raw in personas:
p=p_raw.replace(".yaml","").replace("./personas/","")
selected=""
if p == persona_config["name"]:
selected = "selected"
persona = p
rs +=f"<option value='{p}' {selected}>{p}</option>"
return persona
def list_models():
"""
Return a string containing HTML options for selecting model configurations.
:return: A string containing HTML options for selecting model configurations.
"""
global model_config,loaded_model
models_bin = glob.glob("./models/*")
models_config_raw = glob.glob("./model_configs/*.yaml")
models_config = []
models = []
print('\nconfigs available:')
for p_raw in models_config_raw:
p = p_raw.replace(".yaml", "").replace("./model_configs/", "")
models_config.append(p)
print('\nmodels available:')
for b_raw in models_bin:
b = b_raw.replace("./models/", "")
if b in models_config:
models.append(f"./model_configs/{b}.yaml")
else:
print(f"\n{b} (tabby) doesn't have a valid config")