File size: 7,138 Bytes
757fbed |
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 |
from markdown_it import MarkdownIt
from mdit_py_plugins import front_matter
from reactpy import component, html
from utils import wait_message
def markdown_to_html(markdown_text):
md = (
MarkdownIt("commonmark", {"breaks": True, "html": True})
.use(front_matter.front_matter_plugin)
)
return md.render(markdown_text)
@component
def MarkdownRenderer(content):
html_content = markdown_to_html(content)
return html.div(
{
"style": {
"fontFamily": "Arial",
"color": "#49454F",
"fontSize": "14px",
},
"dangerouslySetInnerHTML": {"__html": html_content}
}
)
@component
def ExamplePrompts(examples, send_example, first_turn):
example_questions = [example.strip() for example in examples.split(";")] if examples else []
def create_prompt_button(question):
return html.button(
{
"style": {
"backgroundColor": "#FFFFFF",
"borderWidth": "1px",
"borderColor": "#65558F",
"borderRadius": "20px",
"padding": "5px 7px",
"margin": "5px",
"cursor": "pointer",
"color": "#21005D",
"fontSize": "13px",
},
"type": "button",
"onClick": lambda _: send_example(question),
},
question
)
if first_turn:
return html.div(
{
"style": {
"display": "flex",
"transform": "translate(4%, 100%)",
"flexDirection": "column",
"justifyContent": "center",
"width": "90%",
}
},
html.p(
{
"style": {
"fontSize": "16px",
"fontFamily": "Arial",
"color": "#49454F",
"marginBottom": "5px",
"transform": "translateX(3%)",
"textAlign": "left",
}
},
"Queries to try:"
),
html.div(
{
"style": {
"display": "flex",
"flexWrap": "wrap",
"justifyContent": "center",
}
},
*[create_prompt_button(q) for q in example_questions]
)
)
return None
@component
def ChatMessage(user, message):
return html.div(
{
"style": {
"display": "flex",
"width": "75%",
"transform": "translateX(20%)",
"justifyContent": "flex-end" if user == "human" else "flex-start",
"margin": "20px 0px 10px 0px"
}
},
html.div(
{
"style": {
"maxWidth": "50%",
"padding": "0px 15px 0px 10px",
"borderRadius": "15px",
"backgroundColor": "#E8DEF8" if user == "human" else "#ECE6F0",
"color": "#000000",
}
},
MarkdownRenderer(message)
)
)
@component
def Logs(messages, log_entries, show_logs, set_show_logs, show_logs_button):
if (len(messages) > 0) and (len(log_entries) > 0) and (messages[-1]["message"] != wait_message) and (show_logs_button):
if not show_logs:
return html.div(
{
"style": {
"display": "flex",
"transform": "translateX(75%)",
"width": "20%",
"margin": "0px",
}
},
html.button(
{
"style": {
"position": "flex",
"background": "none",
"color": "#757575",
"border": "none",
"cursor": "pointer",
"fontFamily": "Arial",
"fontSize": "14px",
},
"type": "button",
"onClick": lambda _: set_show_logs(True)
},
"Show Logs"
)
)
else:
return html.div(
{
"style": {
"display": "flex",
"transform": "translateX(20%)",
"border": "2px solid #e2dfdf",
"borderRadius": "10px",
"width": "75%",
}
},
html.div(
[
html.p(
{
"style": {
"fontSize": "14px",
"marginLeft": "10px",
}
},
entry
) for entry in log_entries
],
html.button(
{
"style": {
"position": "flex",
"background": "none",
"color": "#757575",
"border": "none",
"cursor": "pointer",
"fontFamily": "Arial",
"fontSize": "14px",
"marginBottom": "10px",
"marginLeft": "5px",
},
"type": "button",
"onClick": lambda _: set_show_logs(False)
},
"Hide Logs"
)
)
)
return None
@component
def ChatUI(messages, first_turn, examples, send_example, log_entries, show_logs, set_show_logs, show_logs_button, collapsed):
def render_chat_content():
if first_turn:
return ExamplePrompts(examples, send_example, first_turn)
else:
return [ChatMessage(msg["user"], msg["message"]) for msg in messages]
return html.div(
{
"style": {
"display": "flex",
"flexDirection": "column",
"padding": "10px",
"backgroundColor": "#F4F1F4",
"overflowY": "auto",
"height": f"calc(100vh - {265 if collapsed else 335}px)",
"marginBottom": "15px",
"paddingBottom": "15px",
},
},
render_chat_content(),
Logs(messages, log_entries, show_logs, set_show_logs, show_logs_button)
) |