Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,125 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import panel as pn
|
|
|
|
|
|
|
|
|
|
| 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 |
-
# get history of messages (skipping the intro message)
|
| 47 |
-
# and serialize fleet context messages as "user" role
|
| 48 |
-
messages.extend(
|
| 49 |
-
instance.serialize(role_names={"user": ["user", "Fleet Context"]})[1:]
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
|
|
|
| 55 |
|
|
|
|
| 56 |
message = ""
|
| 57 |
-
async for chunk in
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
message += token
|
| 61 |
yield message
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import os
|
| 3 |
import panel as pn
|
| 4 |
+
from mistralai.async_client import MistralAsyncClient
|
| 5 |
+
from mistralai.models.chat_completion import ChatMessage
|
| 6 |
+
from panel.io.mime_render import exec_with_return
|
| 7 |
|
| 8 |
+
pn.extension("codeeditor", sizing_mode="stretch_width")
|
| 9 |
+
|
| 10 |
+
LLM_MODEL = "mistral-medium"
|
| 11 |
+
SYSTEM_MESSAGE = ChatMessage(
|
| 12 |
+
role="system",
|
| 13 |
+
content=(
|
| 14 |
+
"You are a renowned data visualization expert "
|
| 15 |
+
"with a strong background in matplotlib. "
|
| 16 |
+
"Your primary goal is to assist the user "
|
| 17 |
+
"in edit the code based on user request "
|
| 18 |
+
"using best practices. Simply provide code "
|
| 19 |
+
"in code fences (```). You must have `fig` "
|
| 20 |
+
"as the last line of code"
|
| 21 |
+
),
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
USER_CONTENT_FORMAT = """
|
| 25 |
+
Request:
|
| 26 |
+
{content}
|
| 27 |
+
|
| 28 |
+
Code:
|
| 29 |
+
```python
|
| 30 |
+
{code}
|
| 31 |
+
```
|
| 32 |
+
""".strip()
|
| 33 |
+
|
| 34 |
+
DEFAULT_MATPLOTLIB = """
|
| 35 |
+
import numpy as np
|
| 36 |
+
import matplotlib.pyplot as plt
|
| 37 |
+
|
| 38 |
+
fig = plt.figure()
|
| 39 |
+
ax = plt.axes(title="Plot Title", xlabel="X Label", ylabel="Y Label")
|
| 40 |
+
|
| 41 |
+
x = np.linspace(1, 10)
|
| 42 |
+
y = np.sin(x)
|
| 43 |
+
z = np.cos(x)
|
| 44 |
+
c = np.log(x)
|
| 45 |
+
|
| 46 |
+
ax.plot(x, y, c="blue", label="sin")
|
| 47 |
+
ax.plot(x, z, c="orange", label="cos")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
img = ax.scatter(x, c, c=c, label="log")
|
| 50 |
+
plt.colorbar(img, label="Colorbar")
|
| 51 |
+
plt.legend()
|
| 52 |
+
|
| 53 |
+
# must have fig at the end!
|
| 54 |
+
fig
|
| 55 |
+
""".strip()
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
async def callback(content: str, user: str, instance: pn.chat.ChatInterface):
|
| 59 |
+
# system
|
| 60 |
+
messages = [SYSTEM_MESSAGE]
|
| 61 |
+
|
| 62 |
+
# history
|
| 63 |
+
messages.extend([ChatMessage(**message) for message in instance.serialize()[1:-1]])
|
| 64 |
+
|
| 65 |
+
# new user contents
|
| 66 |
+
user_content = USER_CONTENT_FORMAT.format(
|
| 67 |
+
content=content, code=code_editor.value
|
| 68 |
)
|
| 69 |
+
messages.append(ChatMessage(role="user", content=user_content))
|
| 70 |
|
| 71 |
+
# stream LLM tokens
|
| 72 |
message = ""
|
| 73 |
+
async for chunk in client.chat_stream(model=LLM_MODEL, messages=messages):
|
| 74 |
+
if chunk.choices[0].delta.content is not None:
|
| 75 |
+
message += chunk.choices[0].delta.content
|
|
|
|
| 76 |
yield message
|
| 77 |
|
| 78 |
+
# extract code
|
| 79 |
+
llm_code = re.findall(r"```python\n(.*)\n```", message, re.DOTALL)[0]
|
| 80 |
+
if llm_code.splitlines()[-1].strip() != "fig":
|
| 81 |
+
llm_code += "\nfig"
|
| 82 |
+
code_editor.value = llm_code
|
| 83 |
+
matplotlib_pane.object = exec_with_return(llm_code)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# instantiate widgets and panes
|
| 87 |
+
client = MistralAsyncClient(api_key=os.environ["MISTRAL_API_KEY"])
|
| 88 |
+
chat_interface = pn.chat.ChatInterface(
|
| 89 |
+
callback=callback,
|
| 90 |
+
show_clear=False,
|
| 91 |
+
show_undo=False,
|
| 92 |
+
show_button_name=False,
|
| 93 |
+
message_params=dict(
|
| 94 |
+
show_reaction_icons=False,
|
| 95 |
+
show_copy_icon=False,
|
| 96 |
+
),
|
| 97 |
+
height=700,
|
| 98 |
+
callback_exception="verbose",
|
| 99 |
+
)
|
| 100 |
+
matplotlib_pane = pn.pane.Matplotlib(
|
| 101 |
+
exec_with_return(DEFAULT_MATPLOTLIB),
|
| 102 |
+
sizing_mode="stretch_both",
|
| 103 |
+
)
|
| 104 |
+
code_editor = pn.widgets.CodeEditor(
|
| 105 |
+
value=DEFAULT_MATPLOTLIB,
|
| 106 |
+
sizing_mode="stretch_both",
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# lay them out
|
| 110 |
+
tabs = pn.Tabs(
|
| 111 |
+
("Plot", matplotlib_pane),
|
| 112 |
+
("Code", code_editor),
|
| 113 |
+
)
|
| 114 |
|
| 115 |
+
sidebar = [chat_interface]
|
| 116 |
+
main = [tabs]
|
| 117 |
+
template = pn.template.FastListTemplate(
|
| 118 |
+
sidebar=sidebar,
|
| 119 |
+
main=main,
|
| 120 |
+
sidebar_width=600,
|
| 121 |
+
main_layout=None,
|
| 122 |
+
accent_base_color="#fd7000",
|
| 123 |
+
header_background="#fd7000",
|
| 124 |
+
)
|
| 125 |
+
template.servable()
|