Add function to save settings
Browse files- index.html +212 -56
index.html
CHANGED
@@ -629,6 +629,20 @@ def get_cost_info(prompt_token_count):
|
|
629 |
return f"Estimated input cost: {prompt_token_count + CHAT_TOOLS_TOKENS:,} tokens, Actual total input cost: {actual_total_cost_prompt:,} tokens, Actual total output cost: {actual_total_cost_completion:,} tokens"
|
630 |
|
631 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
632 |
def main():
|
633 |
"""
|
634 |
アプリケーションのメイン関数。Gradioインターフェースを設定し、アプリケーションを起動する。
|
@@ -654,22 +668,39 @@ def main():
|
|
654 |
return defaultValue;
|
655 |
}
|
656 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
657 |
|
658 |
-
|
659 |
-
const endpoint = getItem("endpoint", "https://api.openai.com/v1");
|
660 |
-
const azure_deployment = getItem("azure_deployment", "");
|
661 |
-
const azure_api_version = getItem("azure_api_version", "");
|
662 |
-
const model_name = getItem("model_name", "gpt-4-turbo-preview");
|
663 |
-
const max_tokens = getItem("max_tokens", 4096);
|
664 |
-
const temperature = getItem("temperature", 0.2);
|
665 |
-
const save_chat_history_to_url = getItem("save_chat_history_to_url", false);
|
666 |
-
|
667 |
-
return [platform, endpoint, azure_deployment, azure_api_version, model_name, max_tokens, temperature, save_chat_history_to_url];
|
668 |
};
|
669 |
|
670 |
globalThis.resetSettings = () => {
|
671 |
for (let key in localStorage) {
|
672 |
-
if (key.startsWith(KEY_PREFIX)) {
|
673 |
localStorage.removeItem(key);
|
674 |
}
|
675 |
}
|
@@ -822,56 +853,181 @@ def main():
|
|
822 |
with gr.Tabs():
|
823 |
with gr.TabItem("Settings"):
|
824 |
with gr.Column():
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
829 |
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
js='(x) => saveItem("endpoint", x)', show_progress="hidden")
|
834 |
|
835 |
-
|
836 |
-
|
837 |
-
|
|
|
|
|
|
|
838 |
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
|
843 |
-
with gr.Group():
|
844 |
with gr.Row():
|
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 |
with gr.TabItem("Chat"):
|
877 |
with gr.Row():
|
|
|
629 |
return f"Estimated input cost: {prompt_token_count + CHAT_TOOLS_TOKENS:,} tokens, Actual total input cost: {actual_total_cost_prompt:,} tokens, Actual total output cost: {actual_total_cost_completion:,} tokens"
|
630 |
|
631 |
|
632 |
+
# デフォルト設定値
|
633 |
+
DEFAULT_SETTINGS = {
|
634 |
+
"setting_name": "Default",
|
635 |
+
"platform": "OpenAI",
|
636 |
+
"endpoint": "https://api.openai.com/v1",
|
637 |
+
"azure_deployment": "",
|
638 |
+
"azure_api_version": "",
|
639 |
+
"model_name": "gpt-4-turbo-preview",
|
640 |
+
"max_tokens": 4096,
|
641 |
+
"temperature": 0.2,
|
642 |
+
"save_chat_history_to_url": False
|
643 |
+
};
|
644 |
+
|
645 |
+
|
646 |
def main():
|
647 |
"""
|
648 |
アプリケーションのメイン関数。Gradioインターフェースを設定し、アプリケーションを起動する。
|
|
|
668 |
return defaultValue;
|
669 |
}
|
670 |
};
|
671 |
+
""" + "".join([f"""
|
672 |
+
const default_{setting_key} = {json.dumps(default_value, ensure_ascii=False)};
|
673 |
+
const {setting_key} = getItem("{setting_key}", default_{setting_key});
|
674 |
+
""" for setting_key, default_value in DEFAULT_SETTINGS.items()]) + """
|
675 |
+
const serialized_saved_settings = getItem("saved_settings", []);
|
676 |
+
const default_saved_settings = [[
|
677 |
+
""" + ", ".join([f"{json.dumps(default_value, ensure_ascii=False)}" for _, default_value in DEFAULT_SETTINGS.items()]) + """
|
678 |
+
]];
|
679 |
+
|
680 |
+
saved_settings = [];
|
681 |
+
for (let entry of serialized_saved_settings) {
|
682 |
+
saved_settings.push([
|
683 |
+
entry["setting_name"] || "",
|
684 |
+
entry["platform"] || default_platform,
|
685 |
+
entry["endpoint"] || default_endpoint,
|
686 |
+
entry["azure_deployment"] || default_azure_deployment,
|
687 |
+
entry["azure_api_version"] || default_azure_api_version,
|
688 |
+
entry["model_name"] || default_model_name,
|
689 |
+
entry["max_tokens"] || default_max_tokens,
|
690 |
+
entry["temperature"] || default_temperature,
|
691 |
+
entry["save_chat_history_to_url"] || default_save_chat_history_to_url
|
692 |
+
]);
|
693 |
+
}
|
694 |
+
if (saved_settings.length == 0) {
|
695 |
+
saved_settings = default_saved_settings;
|
696 |
+
}
|
697 |
|
698 |
+
return [setting_name, platform, endpoint, azure_deployment, azure_api_version, model_name, max_tokens, temperature, save_chat_history_to_url, saved_settings];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
699 |
};
|
700 |
|
701 |
globalThis.resetSettings = () => {
|
702 |
for (let key in localStorage) {
|
703 |
+
if (key.startsWith(KEY_PREFIX) && !key.startsWith(KEY_PREFIX + "saved_settings") && !key.startsWith(KEY_PREFIX + "setting_name")) {
|
704 |
localStorage.removeItem(key);
|
705 |
}
|
706 |
}
|
|
|
853 |
with gr.Tabs():
|
854 |
with gr.TabItem("Settings"):
|
855 |
with gr.Column():
|
856 |
+
with gr.Column(variant="panel"):
|
857 |
+
with gr.Row():
|
858 |
+
setting_name = gr.Textbox(label="Setting Name", value="Default", interactive=True)
|
859 |
+
setting_name.change(None, inputs=setting_name, outputs=None,
|
860 |
+
js='(x) => saveItem("setting_name", x)', show_progress="hidden")
|
861 |
+
|
862 |
+
with gr.Row():
|
863 |
+
platform = gr.Radio(label="Platform", interactive=True,
|
864 |
+
choices=["OpenAI", "Azure"], value="OpenAI")
|
865 |
+
platform.change(None, inputs=platform, outputs=None,
|
866 |
+
js='(x) => saveItem("platform", x)', show_progress="hidden")
|
867 |
+
|
868 |
+
with gr.Row():
|
869 |
+
endpoint = gr.Textbox(label="Endpoint", interactive=True)
|
870 |
+
endpoint.change(None, inputs=endpoint, outputs=None,
|
871 |
+
js='(x) => saveItem("endpoint", x)', show_progress="hidden")
|
872 |
+
|
873 |
+
azure_deployment = gr.Textbox(label="Azure Deployment", interactive=True)
|
874 |
+
azure_deployment.change(None, inputs=azure_deployment, outputs=None,
|
875 |
+
js='(x) => saveItem("azure_deployment", x)', show_progress="hidden")
|
876 |
|
877 |
+
azure_api_version = gr.Textbox(label="Azure API Version", interactive=True)
|
878 |
+
azure_api_version.change(None, inputs=azure_api_version, outputs=None,
|
879 |
+
js='(x) => saveItem("azure_api_version", x)', show_progress="hidden")
|
|
|
880 |
|
881 |
+
with gr.Group():
|
882 |
+
with gr.Row():
|
883 |
+
api_key_file = gr.File(file_count="single", file_types=["text"],
|
884 |
+
height=80, label="API Key File")
|
885 |
+
api_key = gr.Textbox(label="API Key", type="password", interactive=True)
|
886 |
+
# 注意: 秘密情報をlocalStorageに保存してはならない。他者に秘密情報が盗まれる危険性があるからである。
|
887 |
|
888 |
+
api_key_file.upload(load_api_key, inputs=api_key_file, outputs=api_key,
|
889 |
+
show_progress="hidden")
|
890 |
+
api_key_file.clear(lambda: None, inputs=None, outputs=api_key, show_progress="hidden")
|
891 |
|
|
|
892 |
with gr.Row():
|
893 |
+
model_name = gr.Textbox(label="Model", interactive=True)
|
894 |
+
model_name.change(None, inputs=model_name, outputs=None,
|
895 |
+
js='(x) => saveItem("model_name", x)', show_progress="hidden")
|
896 |
+
|
897 |
+
max_tokens = gr.Number(label="Max Tokens", interactive=True,
|
898 |
+
minimum=0, precision=0, step=1)
|
899 |
+
max_tokens.change(None, inputs=max_tokens, outputs=None,
|
900 |
+
js='(x) => saveItem("max_tokens", x)', show_progress="hidden")
|
901 |
+
|
902 |
+
temperature = gr.Slider(label="Temperature", interactive=True,
|
903 |
+
minimum=0.0, maximum=1.0, step=0.1)
|
904 |
+
temperature.change(None, inputs=temperature, outputs=None,
|
905 |
+
js='(x) => saveItem("temperature", x)', show_progress="hidden")
|
906 |
+
|
907 |
+
save_chat_history_to_url = gr.Checkbox(label="Save Chat History to URL", interactive=True)
|
908 |
+
|
909 |
+
reset_button = gr.Button("Reset Settings")
|
910 |
+
|
911 |
+
with gr.Column(variant="panel"):
|
912 |
+
default_saved_settings = list(DEFAULT_SETTINGS.values())
|
913 |
+
|
914 |
+
saved_settings_df = gr.Dataframe(
|
915 |
+
elem_id="saved_settings",
|
916 |
+
value=[default_saved_settings],
|
917 |
+
headers=["Name", "Platform", "Endpoint", "Azure Deployment", "Azure API Version", "Model", "Max Tokens", "Temperature", "Save Chat History to URL"],
|
918 |
+
row_count=(0, "dynamic"),
|
919 |
+
col_count=(9, "fixed"),
|
920 |
+
datatype=["str", "str", "str", "str", "str", "str", "number", "number", "bool"],
|
921 |
+
type="array",
|
922 |
+
label="Saved Settings",
|
923 |
+
show_label=True,
|
924 |
+
interactive=False
|
925 |
+
)
|
926 |
+
selected_setting = gr.State(None)
|
927 |
+
temp_selected_row_index = gr.JSON(value=None, visible=False)
|
928 |
+
|
929 |
+
|
930 |
+
def select_setting(event: gr.SelectData):
|
931 |
+
return (event.index[0], event.index[1]), event.index[0]
|
932 |
+
|
933 |
+
|
934 |
+
saved_settings_df.select(
|
935 |
+
select_setting, inputs=None, outputs=[selected_setting, temp_selected_row_index], queue=False, show_progress="hidden"
|
936 |
+
).then(
|
937 |
+
None, inputs=temp_selected_row_index, outputs=None, js='(row_index) => { for (let e of document.querySelectorAll("#saved_settings > div > div > button > svelte-virtual-table-viewport > table > tbody > tr")[row_index].children) { e.classList.add("focus"); } }', queue=False, show_progress="hidden"
|
938 |
+
)
|
939 |
+
|
940 |
+
with gr.Row():
|
941 |
+
load_saved_settings_button = gr.Button("Load")
|
942 |
+
append_or_overwrite_saved_settings_button = gr.Button("Append or Overwrite")
|
943 |
+
delete_saved_settings_button = gr.Button("Delete")
|
944 |
+
|
945 |
+
serialized_saved_settings_state = gr.JSON(visible=False)
|
946 |
+
|
947 |
+
|
948 |
+
def load_saved_setting(saved_settings, selected_setting):
|
949 |
+
if not selected_setting:
|
950 |
+
return saved_settings
|
951 |
+
|
952 |
+
def u(x):
|
953 |
+
return gr.update(value=x, interactive=True)
|
954 |
+
|
955 |
+
row_index = selected_setting[0]
|
956 |
+
|
957 |
+
setting_name, platform, endpoint, azure_deployment, azure_api_version, model_name, max_tokens, temperature, save_chat_history_to_url = saved_settings[row_index]
|
958 |
+
|
959 |
+
return u(setting_name), u(platform), u(endpoint), u(azure_deployment), u(azure_api_version), u(model_name), u(max_tokens), u(temperature), u(save_chat_history_to_url), None
|
960 |
+
|
961 |
+
|
962 |
+
load_saved_settings_button.click(load_saved_setting, inputs=[saved_settings_df, selected_setting], outputs=[setting_name, platform, endpoint, azure_deployment, azure_api_version, model_name, max_tokens, temperature, save_chat_history_to_url, selected_setting], queue=False, show_progress="hidden")
|
963 |
+
|
964 |
+
|
965 |
+
def append_or_overwrite_setting(saved_settings, setting_name, platform, endpoint, azure_deployment, azure_api_version, model_name, max_tokens, temperature, save_chat_history_to_url):
|
966 |
+
|
967 |
+
setting_name = setting_name.strip()
|
968 |
+
|
969 |
+
found = False
|
970 |
+
new_saved_settings = []
|
971 |
+
for entry in saved_settings:
|
972 |
+
if entry[0] == setting_name:
|
973 |
+
new_saved_settings.append([setting_name, platform, endpoint, azure_deployment, azure_api_version,model_name, max_tokens, temperature, save_chat_history_to_url])
|
974 |
+
found = True
|
975 |
+
else:
|
976 |
+
new_saved_settings.append(entry)
|
977 |
+
|
978 |
+
if not found:
|
979 |
+
new_saved_settings.append([setting_name, platform, endpoint, azure_deployment, azure_api_version,model_name, max_tokens, temperature, save_chat_history_to_url])
|
980 |
+
|
981 |
+
return new_saved_settings, None
|
982 |
+
|
983 |
+
|
984 |
+
def serialize_saved_settings(saved_settings):
|
985 |
+
serialization_keys = list(DEFAULT_SETTINGS.keys())
|
986 |
+
|
987 |
+
serialized_saved_settings = [
|
988 |
+
{ k: entry[i] for i, k in enumerate(serialization_keys) }
|
989 |
+
for entry in saved_settings
|
990 |
+
]
|
991 |
+
return serialized_saved_settings
|
992 |
+
|
993 |
+
|
994 |
+
append_or_overwrite_saved_settings_button.click(
|
995 |
+
append_or_overwrite_setting, inputs=[saved_settings_df, setting_name, platform, endpoint, azure_deployment, azure_api_version,model_name, max_tokens, temperature, save_chat_history_to_url], outputs=[saved_settings_df, selected_setting], queue=False, show_progress="hidden"
|
996 |
+
).then(
|
997 |
+
serialize_saved_settings, inputs=saved_settings_df, outputs=serialized_saved_settings_state, queue=False, show_progress="hidden",
|
998 |
+
).then(
|
999 |
+
None, inputs=serialized_saved_settings_state, outputs=None, js='(x) => saveItem("saved_settings", x)', queue=False, show_progress="hidden"
|
1000 |
+
)
|
1001 |
+
|
1002 |
+
|
1003 |
+
def delete_setting(saved_settings, selected_setting):
|
1004 |
+
if not selected_setting:
|
1005 |
+
return saved_settings
|
1006 |
+
|
1007 |
+
row_index = selected_setting[0]
|
1008 |
+
new_saved_settings = saved_settings[0:row_index] + saved_settings[row_index + 1:]
|
1009 |
+
|
1010 |
+
if not new_saved_settings:
|
1011 |
+
new_saved_settings.append(default_saved_settings)
|
1012 |
+
|
1013 |
+
return new_saved_settings, None
|
1014 |
+
|
1015 |
+
|
1016 |
+
delete_saved_settings_button.click(
|
1017 |
+
delete_setting, inputs=[saved_settings_df, selected_setting], outputs=[saved_settings_df, selected_setting], queue=False, show_progress="hidden"
|
1018 |
+
).then(
|
1019 |
+
serialize_saved_settings, inputs=saved_settings_df, outputs=serialized_saved_settings_state, queue=False, show_progress="hidden",
|
1020 |
+
).then(
|
1021 |
+
None, inputs=serialized_saved_settings_state, outputs=None, js='(x) => saveItem("saved_settings", x)', queue=False, show_progress="hidden"
|
1022 |
+
)
|
1023 |
+
|
1024 |
+
temp_saved_settings = gr.JSON(visible=False)
|
1025 |
+
temp_saved_settings.change(lambda x: x, inputs=temp_saved_settings, outputs=saved_settings_df, queue=False, show_progress="hidden")
|
1026 |
+
|
1027 |
+
setting_items = [setting_name, platform, endpoint, azure_deployment, azure_api_version, model_name, max_tokens,
|
1028 |
+
temperature, save_chat_history_to_url, temp_saved_settings]
|
1029 |
+
reset_button.click(None, inputs=None, outputs=setting_items,
|
1030 |
+
js="() => resetSettings()", show_progress="hidden")
|
1031 |
|
1032 |
with gr.TabItem("Chat"):
|
1033 |
with gr.Row():
|