-
Notifications
You must be signed in to change notification settings - Fork 0
/
INSPECTA_Dog.py
826 lines (719 loc) · 29.2 KB
/
INSPECTA_Dog.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
# import pydevd_pycharm
# pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True,suspend=False)
"""
@Author: Amer N. Tahat, Collins Aerospace.
Description: INSPECTA_Dog copilot - ChatCompletion, and Multi-Modal Mode.
Date: 1 July 2024
"""
import os.path
import dash
from dash import dcc, html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
import openai
from dotenv import load_dotenv, find_dotenv
from datetime import datetime, timedelta
import zipfile
import base64
import json
import warnings
import time
import os
# Your utility imports
import INSPECTA_Dog_cmd_util
import INSPECTA_dog_system_msgs
from INSPECTA_Dog_cmd_util import *
from git_actions import *
# Ensure necessary directories exist
directories = ["conversation_history", "temp_history", "shared_history", "counter_examples", "proof_analysis"]
INSPECTA_Dog_cmd_util.ensure_writable_directories(directories)
args = get_args() # If --help is passed, argparse prints help and exits here.
# After loading environment variables, modify how openai.api_key is set:
if args.user_open_api_key:
openai.api_key = args.user_open_api_key
else:
load_dotenv(find_dotenv())
openai.api_key = os.getenv('OPENAI_API_KEY')
# -------------------- Dash App Setup --------------------
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
# Add components to the layout for displaying the token count and timer
token_display = html.Div(id='token-count', style={"margin-top": "20px"})
timer_display = html.Div(id='timer-display', style={"margin-top": "20px"})
gear_button = dbc.Button(
[
# Using Font Awesome gear icon
html.I(className="fa fa-cog", style={"margin-right": "5px"}),
"Settings"
],
id="gear-button",
color="secondary",
className="ml-3",
style={"display": "inline-block", "vertical-align": "middle"}
)
app.layout = dbc.Container([
# Footer
html.Footer([
html.P([
html.Span("", style={"font-size": "14px"}),
html.Span("©", style={"font-size": "14px"}),
" Proof of Concept Web Service"
])
], style={
"text-align": "center",
"background-color": "#f1f1f1",
"padding": "10px",
"margin-bottom": "20px"
}),
# Header with logo, title, and gear button
html.Div([
html.Img(
src="assets/coqdog-5.png",
id="app-logo",
style={
"display": "inline-block",
"height": "50px",
"vertical-align": "middle"
}
),
html.H1(
"AGREE-Dog",
style={
"display": "inline-block",
"vertical-align": "middle",
"margin-left": "20px"
}
),
gear_button
], style={"display": "flex", "align-items": "center", "margin-bottom": "20px"}),
# -------------------- Settings Menu --------------------
html.Div(
id='system-message-menu',
style={"display": "none", "margin-bottom": "20px"},
children=[
# 1) System Message choice
dcc.RadioItems(
id='system-message-choice',
options=[
{
"label": "JKind SMTSolvers AI selector",
"value": "Enable JKind SMTSolvers selector",
"disabled": True
},
{"label": "AgreeDog System Message", "value": "AgreeDog"}
],
value="AgreeDog"
),
dbc.Button("Confirm Selection", id='confirm-system-message-button', color="primary", className="mr-1"),
html.Hr(),
# 2) Advanced items
dcc.RadioItems(
id='include-upload-folder',
options=[
{"label": "Load additional context", "value": "yes"},
{"label": "Use Osate2", "value": "no"},
],
value="no",
inline=True,
style={"margin-top": "10px"}
),
dcc.RadioItems(
id='include-requirements-chain',
options=[
{"label": "Include import chain", "value": "yes"},
{"label": "Don't include import chain", "value": "no"},
],
value="no",
inline=True,
style={"margin-top": "10px"}
),
dbc.RadioItems(
id="model-choice",
options=[
{"label": "GPT-4o multi-modal (128k tk)", "value": "gpt-4o"},
{"label": "GPT-4-Turbo (128k tk)", "value": "gpt-4-turbo"},
{"label": "GPT-4 (8k tk)", "value": "gpt-4-0613", "disabled": True},
],
value="gpt-4o",
inline=True,
style={"margin-top": "10px"}
),
dbc.RadioItems(
id="display-mode",
options=[
{"label": "Full History", "value": "full"},
{"label": "Last Response", "value": "last"},
],
value="last",
inline=True,
style={"margin-top": "10px"}
),
]
),
# -------------------- Main Textarea & Buttons --------------------
dcc.Textarea(
id='user-input',
style={"width": "100%", "height": 200},
placeholder='Enter your context...'
),
# The "Enter the start file" input – initially hidden
html.Div(
id='initial-file-div',
style={"display": "none"}, # hidden by default; toggled by callback
children=[
dbc.Input(
id='initial-file',
placeholder='Enter the start file (e.g., file_name)',
type='text',
style={"margin-top": "10px"}
)
]
),
# Buttons row: Submit, Save, Upload Folder (optional)
html.Div([
dbc.Button("Submit", id='submit-button', color="primary", className="mr-1"),
dbc.Button("Save", id='copy-button', color="secondary", className="mr-1"),
# The Upload Folder button, toggled by callback
html.Div(
id='upload-folder-div',
style={"display": "none"},
children=[
dcc.Upload(
id='upload-folder',
children=dbc.Button("Upload Folder", color="secondary", className="mr-1"),
multiple=False
)
]
)
], style={"display": "flex", "align-items": "center", "margin-top": "10px"}),
html.Div(id='upload-status', style={"margin-top": "10px"}),
html.Div(id='copy-status', style={"margin-top": "10px"}),
html.Div(id='response-output', style={"white-space": "pre-line", "margin-top": "20px"}),
# Hidden fields / placeholders
html.Div(
id='conversation-history',
style={'display': 'none'},
children="[]"
),
html.Div(
id='context-added',
style={'display': 'none'},
children="false"
),
token_display,
timer_display,
dbc.Input(
id='commit-message',
placeholder='Enter commit message',
type='text',
style={"margin-top": "20px"}
),
dbc.Button("Git Commit and Push", id='git-commit-push', color="success", className="mr-1"),
html.Div(id='push-status', style={"margin-top": "20px"})
])
# -------------------- Global Timer Variables --------------------
total_api_time = 0
time_frames = []
start_time = None
elapsed_time = timedelta(0)
formatted_elapsed_time = "00:00:00.00"
# -------------------- Callbacks --------------------
# 1) Toggle the advanced menu (system-message-menu) with the GEAR button
@app.callback(
Output('system-message-menu', 'style'),
Input('gear-button', 'n_clicks'),
State('system-message-menu', 'style'),
prevent_initial_call=True
)
def toggle_system_message_menu(gear_clicks, style):
"""
When the gear button is clicked, toggle the display
of the system message menu (show/hide).
"""
if not gear_clicks:
return style # If button was never clicked, do nothing
if style["display"] == "none":
style["display"] = "block"
else:
style["display"] = "none"
return style
# 2) Conditionally show/hide the "Upload Folder" button
@app.callback(
Output('upload-folder-div', 'style'),
Input('include-upload-folder', 'value'),
prevent_initial_call=True
)
def toggle_upload_folder_div(include_upload):
"""
Shows the Upload Folder button if the user selected "Load additional context"
in the Settings menu.
"""
if include_upload == "yes":
return {"display": "block", "margin-left": "10px"}
return {"display": "none"}
# 3) Conditionally show/hide the "Enter the start file" input
@app.callback(
Output('initial-file-div', 'style'),
Input('include-upload-folder', 'value'),
prevent_initial_call=True
)
def toggle_initial_file_div(include_upload):
"""
Shows the "Enter the start file" field if the user selected
"Load additional context" in the Settings menu.
"""
if include_upload == "yes":
return {"display": "block", "margin-top": "10px"}
return {"display": "none"}
@app.callback(
[
Output('conversation-history', 'children'),
Output('response-output', 'children'),
Output('user-input', 'value'),
Output('token-count', 'children'),
Output('timer-display', 'children'),
Output('context-added', 'children')
],
[
Input('confirm-system-message-button', 'n_clicks'),
Input('submit-button', 'n_clicks')
],
[
State('system-message-choice', 'value'),
State('conversation-history', 'children'),
State('context-added', 'children'),
State('model-choice', 'value'),
State('display-mode', 'value'),
State('user-input', 'value'),
State('initial-file', 'value'),
State('include-requirements-chain', 'value'),
State('include-upload-folder', 'value')
]
)
def handle_app_interactions(confirm_n_clicks,
submit_n_clicks,
system_message_choice,
conversation_history_json,
context_added,
model_choice,
display_mode,
user_input,
initial_file,
include_requirements_chain,
include_upload_folder):
"""
Handles both the selection/confirmation of the system message
(when Confirm Selection is clicked)
and the submission of user input.
"""
global start_time, elapsed_time, formatted_elapsed_time, time_frames, total_api_time
time_frames.append(elapsed_time)
reset_timer_variables()
# Determine which button was clicked
ctx = dash.callback_context
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
conversation_history = json.loads(conversation_history_json)
# 2A) System Message Confirmation
if triggered_id == 'confirm-system-message-button' and confirm_n_clicks is not None:
print(f"Confirm button clicked, selected message: {system_message_choice}")
ch_json, resp, usr_val, tkn_count, tmr_display = set_system_message(conversation_history, system_message_choice)
return ch_json, resp, usr_val, tkn_count, tmr_display, context_added
# 2B) User Input Submission
elif triggered_id == 'submit-button' and submit_n_clicks is not None:
if start_time is None:
start_time = datetime.now()
if submit_n_clicks is None:
return (
conversation_history_json,
"Enter your context and press 'Submit'.",
user_input,
f"Tokens used: {0}",
f"Elapsed Time: {formatted_elapsed_time}",
context_added
)
# Initialize conversation if empty
if not conversation_history:
conversation_history = [
{
'role': 'system',
'content': INSPECTA_dog_system_msgs.AGREE_dog_sys_msg
}
]
context_added = "false"
upload_directory = get_resource_path("uploaded_dir")
subdirectories = [
os.path.join(upload_directory, d) for d in os.listdir(upload_directory)
if os.path.isdir(os.path.join(upload_directory, d))
]
if include_upload_folder == "yes" and not subdirectories:
return (
conversation_history_json,
"No subdirectory found in the uploaded directory.",
user_input,
f"Tokens used: {0}",
f"Elapsed Time: {formatted_elapsed_time}",
context_added
)
# If command line arguments are provided
if INSPECTA_Dog_cmd_util.get_args().working_dir is not None:
args = INSPECTA_Dog_cmd_util.get_args()
target_directory = args.working_dir
if args.start_file is not None:
initial_file = args.start_file
else:
# Use uploaded directory if no cmd args
target_directory = subdirectories[0] if subdirectories else ""
print("Target directory:", target_directory)
project_files = read_project_files(target_directory)
# Only add initial context if it's not already added
if context_added == "false":
if include_requirements_chain == "yes" and initial_file and include_upload_folder == "no":
initial_file = remove_file_ext_from_cmd_like_ui(initial_file)
file_context = concatenate_imports(
initial_file,
project_files,
target_directory,
include_requirements_chain,
include_upload_folder,
context_added
)
if file_context and INSPECTA_Dog_cmd_util.get_args().counter_example is not None:
cex_path = INSPECTA_Dog_cmd_util.get_args().counter_example
cex = read_counter_example_file(cex_path)
prompt = set_prompt(file_context, cex)
user_input = f"{prompt}\n{user_input}"
context_added = "true"
elif (
include_requirements_chain == "no"
and initial_file
and include_upload_folder == "no"
and context_added == "false"
and INSPECTA_Dog_cmd_util.get_args().counter_example is not None
):
cex_path = INSPECTA_Dog_cmd_util.get_args().counter_example
cex = read_counter_example_file(cex_path)
start_file_with_ext = INSPECTA_Dog_cmd_util.get_args().start_file
working_dir = INSPECTA_Dog_cmd_util.get_args().working_dir
start_file_path = os.path.join(working_dir, start_file_with_ext)
start_file_content = read_start_file_content(start_file_path)
prompt = set_prompt(start_file_content, cex)
user_input = f"{prompt}\n{user_input}"
context_added = "true"
elif (
include_requirements_chain == "no"
and initial_file
and include_upload_folder == "yes"
):
# If the user chooses to upload and not use requirements chain,
# we do not re-add context from cmd line. user_input stays as-is.
pass
# Append user input to conversation
conversation_history.append({'role': 'user', 'content': user_input})
response_obj = get_completion_from_messages(conversation_history, model=model_choice)
response = response_obj.choices[0].message["content"]
tokens_used = response_obj['usage']['total_tokens']
conversation_history.append({'role': 'assistant', 'content': response})
save_conversation_history_to_file(conversation_history)
display_text = format_display_text(conversation_history, display_mode)
warning = token_warning(model_choice, tokens_used)
total_time_display = total_timedisplay(start_time, time_frames, total_api_time)
return (
json.dumps(conversation_history),
display_text,
"",
f"Tokens used : {tokens_used}" + warning,
total_time_display,
context_added
)
# Default return
return (
conversation_history_json,
"",
"",
"",
(f"Elapsed Time: {formatted_elapsed_time}, "
f"Total API Call Time: {total_api_time:.2f} seconds"),
context_added
)
# -------------------- Utility Functions --------------------
def set_prompt(aadl_content, counter_example_content):
"""Generates the initial prompt with AADL model and counterexample content."""
if INSPECTA_Dog_cmd_util.get_args().counter_example is None:
# If the counterexample content is empty, provide a warning in the prompt
prompt = f"""
For the following AADL model:
{aadl_content}
Warning: No counterexample was generated or provided by AGREE.
There is no cex to explain any further.
Would you like me to assist with something else?
"""
else:
prompt = f"""
Consider the following AADL model:
{aadl_content}
AGREE generated a counterexample:
{counter_example_content}
Can you explain why and how to fix it?
Write the modified AADL code between ``` ```
"""
return prompt
def remove_file_ext_from_cmd_like_ui(initial_file):
if INSPECTA_Dog_cmd_util.get_args().start_file is not None:
initial_file = INSPECTA_Dog_cmd_util.get_args().start_file
initial_file_without_ext = initial_file[:-5]
return initial_file_without_ext
else:
return initial_file
def token_warning(model_choice, tokens_used):
warning = ""
if 7000 <= tokens_used < 8000 and model_choice == "gpt-4-0613":
warning = " Warning: Tokens used are higher than 7000. If you are using GPT-4 8k, consider switching to GPT-3.5 16K."
return warning
def total_timedisplay(start_time, time_frames, total_api_time):
global elapsed_time, formatted_elapsed_time
elapsed_time = datetime.now() - start_time
time_frames.append(elapsed_time)
formatted_elapsed_time = str(elapsed_time)[:10]
total_time_display = f"Elapsed Time: {formatted_elapsed_time}, Total API Call Time: {total_api_time:.2f} seconds"
return total_time_display
def set_system_message(conversation_history, system_message_choice):
if not conversation_history:
conversation_history = [{'role': 'system', 'content': ''}]
if system_message_choice == "CoqDog":
conversation_history[0]['content'] = INSPECTA_dog_system_msgs.COQ_dog_sys_msg
elif system_message_choice == "AgreeDog":
conversation_history[0]['content'] = INSPECTA_dog_system_msgs.AGREE_dog_sys_msg
return json.dumps(conversation_history), "", "", "", ""
def reset_timer_variables():
global start_time, elapsed_time, formatted_elapsed_time
start_time = None
elapsed_time = timedelta(0)
formatted_elapsed_time = "00:00:00.00"
def read_start_file_content(file_path):
try:
with open(file_path, 'r') as f:
file_content = f.read()
return file_content.strip()
except FileNotFoundError:
print(f"File not found: {file_path}")
return ""
def read_generic_file_content(file_path):
with open(file_path, 'r') as f:
return f.read()
def handle_requires(file_path, project_files, files_to_check, processed_files):
with open(file_path, 'r') as f:
for line in f:
line = line.strip()
if line.lower().startswith('with'):
required_packages_line = line[4:].strip().rstrip(';')
required_packages = [pkg.strip() for pkg in required_packages_line.split(',')]
for required_pkg in required_packages:
if (required_pkg in project_files and
required_pkg not in processed_files and
required_pkg not in files_to_check):
files_to_check.append(required_pkg)
def read_project_files(directory):
if INSPECTA_Dog_cmd_util.get_args().working_dir is not None:
agree_files = os.path.join(directory, "_AgreeFiles")
if not os.path.exists(agree_files):
create_agree_files_file(directory)
agree_files = os.path.join(directory, "_AgreeFiles")
else:
agree_files = os.path.join(directory, "packages/_AgreeFiles")
if not os.path.exists(agree_files):
directory = os.path.join(directory, "packages")
INSPECTA_Dog_cmd_util.create_agree_files_file(directory)
agree_files = os.path.join(directory, "/_AgreeFiles")
if not os.path.exists(agree_files):
warnings.warn(
f"The expected '_AgreeFiles' file was not found in {directory}. "
f"Continuing, but some functionality may be affected.",
UserWarning
)
return []
project_files = []
with open(agree_files, 'r') as file:
for line in file:
filename = line.strip()
if filename.endswith(".aadl"):
project_files.append(filename[:-5]) # Remove '.aadl' extension
return project_files
def concatenate_imports(start_file, project_files, folder_path,
include_requirements_chain, include_upload_folder,
context_added):
processed_files = set()
context = ""
files_to_check = [start_file]
while files_to_check:
current_file = files_to_check.pop()
if current_file in processed_files:
continue
processed_files.add(current_file)
if (include_requirements_chain == "yes" and start_file
and context_added == "false" and include_upload_folder == "no"):
file_path = os.path.join(folder_path, current_file + ".aadl")
elif (include_requirements_chain == "yes" and start_file
and context_added == "true" and include_upload_folder == "yes"):
file_path = os.path.join(folder_path, "packages", current_file + ".aadl")
else:
file_path = os.path.join(folder_path, current_file + ".aadl")
if not os.path.exists(file_path):
print(f"{current_file} is not in packages")
continue
if current_file == start_file:
content = read_start_file_content(file_path)
else:
content = read_generic_file_content(file_path)
context += content + "\n"
handle_requires(file_path, project_files, files_to_check, processed_files)
return context
def highlight_keywords(text):
keywords = [
"Lemma", "Proof", "Qed", "assume", "guarantee", "end",
"system", "annex", "agree", "connections", "properties",
"features", "lemma", "aadl", "initially"
]
elements = []
start = 0
while start < len(text):
closest_idx = len(text)
closest_keyword = None
for keyword in keywords:
idx = text.find(keyword, start)
if idx != -1 and idx < closest_idx:
closest_idx = idx
closest_keyword = keyword
if closest_keyword is not None:
if closest_idx != start:
elements.append(text[start:closest_idx])
key_color = 'purple'
if closest_keyword in ["connections"]:
key_color = 'blue'
elements.append(
html.Span(
text[closest_idx:closest_idx + len(closest_keyword)],
style={'color': key_color}
)
)
start = closest_idx + len(closest_keyword)
else:
elements.append(text[start:])
break
return elements
# -------------------- FIXED: format_display_text last response keywords--------------------
def format_display_text(conversation_history, display_mode):
if display_mode == "full":
display_elements = []
for message in conversation_history:
# Skip system messages
if message['role'] == 'system':
continue
if message['role'] == 'user':
label = html.Span("User:", style={'color': 'blue'})
else:
label = html.Span("AGREE-Dog:", style={'color': 'red'})
display_elements.append(label)
display_elements.extend(
highlight_keywords(" " + message['content'] + "\n\n")
)
return display_elements
else: # "last" mode
# We still show the last message, whether user or assistant
last_message = conversation_history[-1]
if last_message['role'] == 'system':
# If for some reason the last message is system,
# you'd want to handle that or just skip it.
# One approach: skip to the last non-system message.
# For simplicity, let's just return an empty list or a note:
return [html.Span("(No user/assistant message yet.)", style={'color': 'gray'})]
if last_message['role'] == 'user':
label = html.Span("User:", style={'color': 'blue'})
else:
label = html.Span("AGREE-Dog:", style={'color': 'red'})
highlighted_last = highlight_keywords(" " + last_message['content'] + "\n\n")
return [label] + highlighted_last
# ----------------------------------------------------------
@app.callback(
Output('copy-status', 'children'),
Input('copy-button', 'n_clicks'),
State('conversation-history', 'children'),
prevent_initial_call=True
)
def copy_conversation_history(n_clicks, conversation_history_json):
global total_api_time
if n_clicks is None:
return "Click the button to copy the conversation history."
total_time_display = f"Total API Call Time: {total_api_time:.2f} seconds"
conversation_history = json.loads(conversation_history_json)
conversation_history.append({'role': 'system', 'content': total_time_display})
source_file = 'conversation_history/conversation_history.json'
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
destination_dir = 'temp_history'
destination_file = f'{destination_dir}/conversation_history_{timestamp}.json'
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
with open(destination_file, 'w') as file:
json.dump(conversation_history, file, indent=4)
return f"Successfully copied to {destination_file}"
@app.callback(
Output('upload-status', 'children'),
[Input('upload-folder', 'contents')],
[State('upload-folder', 'filename'),
State('include-upload-folder', 'value')]
)
def handle_upload(contents, filename, include_upload):
if contents is None or include_upload != "yes":
return " "
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
current_dir_path = os.path.abspath(".")
upload_directory = os.path.join(current_dir_path, "uploaded_dir")
if not os.path.exists(upload_directory):
os.makedirs(upload_directory)
zip_path = os.path.join(upload_directory, filename)
with open(zip_path, "wb") as file:
file.write(decoded)
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(upload_directory)
return "Folder uploaded and extracted successfully!"
def get_completion_from_messages(messages, temperature=0.7, model="gpt-4-0613"):
global total_api_time
start_time = time.time() # Record the start time
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
)
end_time = time.time() # Record the end time
api_call_time = end_time - start_time
total_api_time += api_call_time
return response
def save_conversation_history_to_file(conversation_history, dir_name='conversation_history'):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
file_name = f"{dir_name}/conversation_history.json"
json_string = json.dumps(conversation_history, indent=4)
with open(file_name, 'w') as file:
file.write(json_string)
@app.callback(
Output('push-status', 'children'),
Input('git-commit-push', 'n_clicks'),
State('commit-message', 'value'),
prevent_initial_call=True
)
def commit_and_push(n_clicks, commit_message):
if n_clicks is not None:
current_directory_1 = os.getcwd()
try:
success, message = git_commit_push(commit_message)
os.chdir(current_directory_1)
return message
except Exception as e:
os.chdir(current_directory_1)
return f"An error occurred: {e}"
# -------------------- Main --------------------
if __name__ == '__main__':
print("Starting the Dash server...")
try:
app.run_server(debug=False, host='127.0.0.1', port=8050)
except KeyboardInterrupt:
print("Shutting down the server gracefully.")