11import os
22import subprocess
3+ import appdirs
4+ import shutil
35from pkg_resources import resource_filename
46
57from discordai_modelizer import __name__ as pkg_name
@@ -10,9 +12,10 @@ def create_model(bot_token: str, openai_key: str, channel_id: str, user_id: str,
1012 max_entry_count = 1000 , reduce_mode = "even" , base_model = "none" , clean = False , redownload = False ):
1113 os .environ ["OPENAI_API_KEY" ] = openai_key
1214 channel_user = f"{ channel_id } _{ user_id } "
15+ files_path = appdirs .user_data_dir (appauthor = "Adib Baji" , appname = "discordai" )
1316
1417 # Download logs
15- if not os .path .isfile (f"{ channel_user } _logs.json" ) or redownload :
18+ if not os .path .isfile (f"{ files_path } / { channel_user } _logs.json" ) or redownload :
1619 print ("INFO: Exporting chat logs using DiscordChatExporter..." )
1720 print ("INFO: This may take a few minutes to hours depending on the message count of the channel" )
1821 print ("INFO: Progress will NOT be saved if cancelled" )
@@ -28,45 +31,47 @@ def create_model(bot_token: str, openai_key: str, channel_id: str, user_id: str,
2831 "--filter" , f"from:'{ user_id } '"
2932 ])
3033 print ("--------------------------DiscordChatExporter---------------------------" )
34+ shutil .move (f"{ channel_user } _logs.json" , f"{ files_path } /{ channel_user } _logs.json" )
35+ print (f"INFO: Logs saved to { files_path } /{ channel_user } _logs.json" )
3136 else :
32- print ("INFO: Chat logs detected locally... Skipping download." )
37+ print (f "INFO: Chat logs detected locally at { files_path } / { channel_user } _logs.json ... Skipping download." )
3338
3439 # Parse logs
3540 print ("INFO: Parsing chat logs into a openAI compatible dataset..." )
36- parse_logs (f"{ channel_user } _logs.json" , user_id , thought_time )
41+ parse_logs (f"{ files_path } / { channel_user } _logs.json" , user_id , thought_time )
3742
3843 # Prepare and reduce dataset
3944 print ("INFO: Cleaning up generated dataset..." )
4045 try :
41- os .remove (f"{ channel_user } _data_set_prepared.jsonl" )
46+ os .remove (f"{ files_path } / { channel_user } _data_set_prepared.jsonl" )
4247 except FileNotFoundError :
4348 pass
4449 subprocess .run ([
4550 "openai" , "tools" , "fine_tunes.prepare_data" ,
46- "-f" , f"{ channel_user } _data_set.jsonl" ,
51+ "-f" , f"{ files_path } / { channel_user } _data_set.jsonl" ,
4752 "-q"
4853 ])
49- if os .path .isfile (f"{ channel_user } _data_set_prepared.jsonl" ):
50- get_lines (f"{ channel_user } _data_set_prepared.jsonl" , max_entry_count , reduce_mode )
54+ if os .path .isfile (f"{ files_path } / { channel_user } _data_set_prepared.jsonl" ):
55+ get_lines (f"{ files_path } / { channel_user } _data_set_prepared.jsonl" , max_entry_count , reduce_mode )
5156 else :
52- get_lines (f"{ channel_user } _data_set.jsonl" , max_entry_count , reduce_mode )
57+ get_lines (f"{ files_path } / { channel_user } _data_set.jsonl" , max_entry_count , reduce_mode )
5358
5459 # Train customized openAI model
5560 if base_model in ["davinci" , "curie" , "babbage" , "ada" ]:
5661 print ("INFO: Training customized openAI model..." )
5762 print ("INFO: This may take a few minutes to hours depending on the size of the dataset and the selected base model" )
58- if os .path .isfile (f"{ channel_user } _data_set_prepared.jsonl" ):
63+ if os .path .isfile (f"{ files_path } / { channel_user } _data_set_prepared.jsonl" ):
5964 subprocess .run ([
6065 "openai" , "api" , "fine_tunes.create" ,
61- "-t" , f"{ channel_user } _data_set_prepared.jsonl" ,
66+ "-t" , f"{ files_path } / { channel_user } _data_set_prepared.jsonl" ,
6267 "-m" , base_model ,
6368 "--suffix" , user_id ,
6469 "--no_check_if_files_exist"
6570 ])
6671 else :
6772 subprocess .run ([
6873 "openai" , "api" , "fine_tunes.create" ,
69- "-t" , f"{ channel_user } _data_set.jsonl" ,
74+ "-t" , f"{ files_path } / { channel_user } _data_set.jsonl" ,
7075 "-m" , base_model ,
7176 "--suffix" , user_id ,
7277 "--no_check_if_files_exist"
@@ -77,7 +82,7 @@ def create_model(bot_token: str, openai_key: str, channel_id: str, user_id: str,
7782 # Clean up generated files
7883 if clean :
7984 try :
80- os .remove (f"{ channel_user } _data_set.jsonl" )
81- os .remove (f"{ channel_user } _data_set_prepared.jsonl" )
85+ os .remove (f"{ files_path } / { channel_user } _data_set.jsonl" )
86+ os .remove (f"{ files_path } / { channel_user } _data_set_prepared.jsonl" )
8287 except FileNotFoundError :
8388 pass
0 commit comments