Spaces:
Running
Running
File size: 2,794 Bytes
f3141ae b470e8d f3141ae 2b894e4 f3141ae b470e8d f3141ae b470e8d f3141ae b470e8d a43df2a b470e8d f3141ae |
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 |
from dataclasses import asdict
import errno
import json
import os
import re
import time
import mesop as me
import components as mex
import handlers
from constants import SAVED_PROMPT_DIRECTORY
from state import State
@me.component
def tool_sidebar():
state = me.state(State)
with mex.icon_sidebar():
if state.mode == "Prompt":
mex.icon_menu_item(
icon="tune",
tooltip="Model settings",
key="dialog_show_model_settings",
on_click=handlers.on_open_dialog,
)
mex.icon_menu_item(
icon="data_object",
tooltip="Set variables",
key="dialog_show_prompt_variables",
on_click=handlers.on_open_dialog,
)
mex.icon_menu_item(
icon="history",
tooltip="Version history",
key="dialog_show_version_history",
on_click=handlers.on_open_dialog,
)
if state.mode == "Eval":
mex.icon_menu_item(
icon="compare",
tooltip="Compare versions",
key="dialog_show_add_comparison",
on_click=handlers.on_open_dialog,
)
mex.icon_menu_item(
icon="upload",
tooltip="Load prompt data",
key="dialog_show_load",
on_click=handlers.on_open_dialog,
)
mex.icon_menu_item(
icon="download",
tooltip="Download prompt data",
on_click=on_click_download,
)
mex.icon_menu_item(
icon="light_mode" if me.theme_brightness() == "dark" else "dark_mode",
tooltip="Switch to " + ("light mode" if me.theme_brightness() == "dark" else "dark mode"),
on_click=on_click_theme_brightness,
)
def on_click_theme_brightness(e: me.ClickEvent):
if me.theme_brightness() == "light":
me.set_theme_mode("dark")
else:
me.set_theme_mode("light")
def on_click_download(e: me.ClickEvent):
state = me.state(State)
cleaned_title = _clean_title(state.title)
filename = f"prompt-{cleaned_title}.json"
_create_directory(SAVED_PROMPT_DIRECTORY)
with open(f"{SAVED_PROMPT_DIRECTORY}/{filename}", "w") as outfile:
output = {
key: value
for key, value in asdict(state).items()
if key
not in (
"temp_title",
"mode",
"comparisons",
"system_prompt_card_expanded",
"prompt_gen_task_description",
)
and not key.startswith("dialog_")
}
json.dump(output, outfile)
state.snackbar_message = f"Prompt exported as {filename}."
state.show_snackbar = True
state.async_action_name = "hide_snackbar"
def _clean_title(title: str) -> str:
return re.sub(r"[^a-z0-9_]", "", title.lower().replace(" ", "_"))
def _create_directory(directory_path):
"""Creates a directory if it doesn't exist."""
try:
os.makedirs(directory_path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
|