Skip to content

Commit

Permalink
ui implemented and functional
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderGi committed Feb 29, 2024
1 parent 8025f7c commit 1788d71
Show file tree
Hide file tree
Showing 5 changed files with 414 additions and 281 deletions.
8 changes: 4 additions & 4 deletions bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def get_badge_html(self):


class Platform(models.IntegerChoices):
FACEBOOK = 1
INSTAGRAM = (2, "Instagram & FB")
WHATSAPP = 3
SLACK = 4
FACEBOOK = (1, "Facebook")
INSTAGRAM = (2, "Instagram")
WHATSAPP = (3, "WhatsApp")
SLACK = (4, "Slack")

def get_favicon(self):
if self == Platform.WHATSAPP:
Expand Down
36 changes: 23 additions & 13 deletions daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ def clean_query_params(cls, *, example_id, run_id, uid) -> dict:
query_params |= dict(example_id=example_id)
return query_params

def api_url(self, example_id=None, run_id=None, uid=None) -> furl:
query_params = {}
def api_url(
self, example_id=None, run_id=None, uid=None, query_params=None
) -> furl:
query_params = query_params or {}
if run_id and uid:
query_params = dict(run_id=run_id, uid=uid)
elif example_id:
Expand All @@ -171,13 +173,14 @@ def api_url(self, example_id=None, run_id=None, uid=None) -> furl:
def endpoint(self) -> str:
return f"/v2/{self.slug_versions[0]}/"

def get_tab_url(self, tab: str) -> str:
def get_tab_url(self, tab: str, query_params: dict = {}) -> str:
example_id, run_id, uid = extract_query_params(gooey_get_query_params())
return self.app_url(
example_id=example_id,
run_id=run_id,
uid=uid,
tab_name=MenuTabs.paths[tab],
query_params=query_params,
)

def setup_render(self):
Expand Down Expand Up @@ -258,11 +261,7 @@ def _render_header(self):
)

with st.div(className="d-flex align-items-center"):
can_user_edit_run = self.is_current_user_admin() or (
self.request
and self.request.user
and current_run.uid == self.request.user.uid
)
can_user_edit_run = self.can_user_edit_run(current_run)
has_unpublished_changes = (
published_run
and published_run.saved_run != current_run
Expand Down Expand Up @@ -302,6 +301,14 @@ def _render_header(self):
elif is_root_example:
st.write(self.preview_description(current_run.to_dict()))

def can_user_edit_run(self, current_run: SavedRun | None = None) -> bool:
current_run = current_run or self.get_current_sr()
return self.is_current_user_admin() or bool(
self.request
and self.request.user
and current_run.uid == self.request.user.uid
)

def _render_title(self, title: str):
st.write(f"# {title}")

Expand Down Expand Up @@ -1580,12 +1587,15 @@ def _render(pr: PublishedRun):

grid_layout(3, published_runs, _render)

def ensure_authentication(self):
def ensure_authentication(self, next_url: str | None = None):
if not self.request.user or self.request.user.is_anonymous:
redirect_url = furl(
"/login", query_params={"next": furl(self.request.url).set(origin=None)}
)
raise RedirectException(str(redirect_url))
raise RedirectException(self.get_auth_url(next_url))

def get_auth_url(self, next_url: str | None = None) -> str:
return furl(
"/login",
query_params={"next": furl(next_url or self.request.url).set(origin=None)},
).tostr()

def _history_tab(self):
assert self.request, "request must be set to render history tab"
Expand Down
57 changes: 43 additions & 14 deletions daras_ai_v2/bot_integration_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def general_integration_settings(bi: BotIntegration):
value=bi.streaming_enabled,
key=f"_bi_streaming_enabled_{bi.id}",
)
st.caption("Responses will be streamed to the user in real-time if enabled.")
bi.show_feedback_buttons = st.checkbox(
"**👍🏾 👎🏽 Show Feedback Buttons**",
value=bi.show_feedback_buttons,
Expand Down Expand Up @@ -91,6 +92,35 @@ def general_integration_settings(bi: BotIntegration):
st.error(str(e))


def slack_specific_settings(bi: BotIntegration, default_name: str):
if st.session_state.get(f"_bi_reset_{bi.id}"):
st.session_state[f"_bi_name_{bi.id}"] = default_name
st.session_state[f"_bi_slack_read_receipt_msg_{bi.id}"] = (
BotIntegration._meta.get_field("slack_read_receipt_msg").default
)

bi.slack_read_receipt_msg = st.text_input(
"""
##### ✅ Read Receipt
This message is sent immediately after recieving a user message and replaced with the copilot's response once it's ready.
(leave blank to disable)
""",
placeholder=bi.slack_read_receipt_msg,
value=bi.slack_read_receipt_msg,
key=f"_bi_slack_read_receipt_msg_{bi.id}",
)
bi.name = st.text_input(
"""
##### 🪪 Channel Specific Bot Name
This is the name the bot will post as in this specific channel (to be displayed in Slack)
""",
placeholder=bi.name,
value=bi.name,
key=f"_bi_name_{bi.id}",
)
st.caption("Enable streaming messages to Slack in real-time.")


def broadcast_input(bi: BotIntegration):
from bots.tasks import send_broadcast_msgs_chunked
from recipes.VideoBots import VideoBotsPage
Expand All @@ -105,9 +135,9 @@ def broadcast_input(bi: BotIntegration):
)
text = st.text_area(
f"""
##### Broadcast Message
#### Broadcast Message 📢
Broadcast a message to all users of this integration using this bot account. \\
You can also do this via the [API]({api_docs_url}).
You can also do this via the [API]({api_docs_url}) which allows filtering by phone number and more!
""",
key=key + ":text",
placeholder="Type your message here...",
Expand Down Expand Up @@ -136,7 +166,7 @@ def broadcast_input(bi: BotIntegration):

should_confirm_key = key + ":should_confirm"
confirmed_send_btn = key + ":confirmed_send"
if st.button("📢 Send Broadcast", style=dict(height="3.2rem"), key=key + ":send"):
if st.button("📤 Send Broadcast", style=dict(height="3.2rem"), key=key + ":send"):
st.session_state[should_confirm_key] = True
if not st.session_state.get(should_confirm_key):
return
Expand Down Expand Up @@ -164,22 +194,21 @@ def broadcast_input(bi: BotIntegration):
st.button("✅ Yes, Send", key=confirmed_send_btn)


def render_bot_test_link(bi: BotIntegration):
def get_bot_test_link(bi: BotIntegration) -> str | None:
if bi.wa_phone_number:
test_link = (
return (
furl("https://wa.me/", query_params={"text": "Hi"})
/ bi.wa_phone_number.as_e164
)
).tostr()
elif bi.slack_team_id:
test_link = (
return (
furl("https://app.slack.com/client")
/ bi.slack_team_id
/ bi.slack_channel_id
)
).tostr()
elif bi.ig_username:
return (furl("http://instagram.com/") / bi.ig_username).tostr()
elif bi.fb_page_name:
return (furl("https://www.facebook.com/") / bi.fb_page_name).tostr()
else:
return
st.html(
f"""
<a class="btn btn-theme btn-tertiary d-inline-block" target="blank" href="{test_link}">📱 Test</a>
"""
)
return None
41 changes: 40 additions & 1 deletion gooey_ui/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ def write(*objs: typing.Any, unsafe_allow_html=False, **props):
)


def center(direction="column") -> state.NestingCtx:
return div(
style={
"display": "flex",
"justifyContent": "center",
"alignItems": "center",
"textAlign": "center",
"flexDirection": direction,
}
)


def newline():
html("<br/>")


def markdown(body: str | None, *, unsafe_allow_html=False, **props):
if body is None:
return _node("markdown", body="", **props)
Expand Down Expand Up @@ -198,6 +214,7 @@ def columns(
*,
gap: str = None,
responsive: bool = True,
column_props: dict = {},
**props,
) -> tuple[state.NestingCtx, ...]:
if isinstance(spec, int):
Expand All @@ -206,7 +223,10 @@ def columns(
props.setdefault("className", "row")
with div(**props):
return tuple(
div(className=f"col-lg-{p} {'col-12' if responsive else f'col-{p}'}")
div(
className=f"col-lg-{p} {'col-12' if responsive else f'col-{p}'}",
**column_props,
)
for w in spec
if (p := f"{round(w / total_weight * 12)}")
)
Expand Down Expand Up @@ -491,6 +511,23 @@ def button(
return bool(state.session_state.pop(key, False))


def anchor(
label: str,
href: str,
*,
type: typing.Literal["primary", "secondary", "tertiary", "link"] = "secondary",
disabled: bool = False,
unsafe_allow_html: bool = False,
**props,
):
className = f"btn btn-theme btn-{type} " + props.pop("className", "")
style = props.pop("style", {})
if disabled:
style["pointerEvents"] = "none"
with tag("a", href=href, className=className, style=style, **props):
markdown(dedent(label), unsafe_allow_html=unsafe_allow_html)


form_submit_button = button


Expand Down Expand Up @@ -608,6 +645,7 @@ def horizontal_radio(
disabled: bool = False,
checked_by_default: bool = True,
label_visibility: LabelVisibility = "visible",
button_props: dict = {},
) -> T | None:
if not options:
return None
Expand All @@ -628,6 +666,7 @@ def horizontal_radio(
type="primary",
className="replicate-nav " + ("active" if value == option else ""),
disabled=disabled,
**button_props,
):
state.session_state[key] = value = option
state.experimental_rerun()
Expand Down
Loading

0 comments on commit 1788d71

Please sign in to comment.