Skip to content

Commit

Permalink
UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Enias Cailliau committed Jul 26, 2023
1 parent 31bc992 commit f2a07c6
Show file tree
Hide file tree
Showing 14 changed files with 445 additions and 50 deletions.
7 changes: 3 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pytimeparse
termcolor
steamship~=2.17.18
langchain==0.0.200
steamship@git+https://github.com/steamship-core/python-client@ec/review-mixins
langchain==0.0.200
scrapetube
34 changes: 25 additions & 9 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
from steamship.invocable import Config
from steamship.invocable.mixins.indexer_pipeline_mixin import IndexerPipelineMixin

from personalities import get_personality
from tools.selfie import SelfieTool
from tools.utils import clean_text
from tools.video_message import VideoMessageTool
from utils import clean_text

TEMPERATURE = 0.7
MAX_FREE_MESSAGES = 5
Expand All @@ -41,9 +40,17 @@ class GirlFriendGPTConfig(TelegramTransportConfig):
chat_ids: str = Field(
default="", description="Comma separated list of whitelisted chat_id's"
)
personality: str = Field(
description="The personality you want to deploy. Pick one of the personalities listed here: "
"https://github.com/EniasCailliau/GirlfriendGPT/tree/main/src/personalities"
name: str = Field(
description="The name of your companion"
)
byline: str = Field(
description="The byline of your companion"
)
identity: str = Field(
description="The identity of your companion"
)
behavior: str = Field(
description="The behavior of your companion"
)
use_gpt4: bool = Field(
True,
Expand All @@ -52,9 +59,15 @@ class GirlFriendGPTConfig(TelegramTransportConfig):
)


SYSTEM_PROMPT = """You are Sacha and are currently talking to Jessica.
{personality}
SYSTEM_PROMPT = """You are {self.name}, {self.byline}.
Who you are:
{identity_str}
How you behave:
{behavior_str}
NOTE: Some functions return images, video, and audio files. These multimedia files will be represented in messages as
UUIDs for Steamship Blocks. When responding directly to a user, you SHOULD print the Steamship Blocks for the images,
Expand Down Expand Up @@ -82,7 +95,10 @@ def __init__(self, **kwargs):
llm=ChatOpenAI(self.client, model_name=model_name, temperature=TEMPERATURE),
)
self._agent.PROMPT = SYSTEM_PROMPT.format(
personality=get_personality(self.config.personality).format()
name=self.config.name,
byline=self.config.byline,
identity=self.config.identity,
behavior=self.config.behavior,
)

# This Mixin provides HTTP endpoints that connects this agent to a web client
Expand Down
28 changes: 0 additions & 28 deletions src/test_importer.py

This file was deleted.

5 changes: 0 additions & 5 deletions src/utils.py

This file was deleted.

23 changes: 19 additions & 4 deletions steamship.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "package",
"handle": "girlfriend-gpt-bot",
"version": "2.2.0",
"version": "2.2.1-rc.7",
"description": "",
"author": "",
"entrypoint": "Unused",
Expand Down Expand Up @@ -39,15 +39,30 @@
"description": "Comma separated list of whitelisted chat_id's",
"default": ""
},
"personality": {
"name": {
"type": "string",
"description": "The personality you want to deploy. Pick one of the personalities listed here: https://github.com/EniasCailliau/GirlfriendGPT/tree/main/src/personalities",
"description": "The name of your companion",
"default": null
},
"byline": {
"type": "string",
"description": "The byline of your companion",
"default": null
},
"identity": {
"type": "string",
"description": "The identity of your companion",
"default": null
},
"behavior": {
"type": "string",
"description": "The behavior of your companion",
"default": null
},
"use_gpt4": {
"type": "boolean",
"description": "If True, use GPT-4. Use GPT-3.5 if False. GPT-4 generates better responses at higher cost and latency.",
"default": false
"default": true
}
},
"steamshipRegistry": {
Expand Down
102 changes: 102 additions & 0 deletions ui/Companion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import sys
from pathlib import Path

import streamlit as st
from steamship.cli.create_instance import load_manifest

sys.path.append(str((Path(__file__) / "..").resolve()))
st.set_page_config(page_title="🎥->🤗 Youtube to Companion")
from utils.data import get_companions, get_companion_attributes, add_resource
from utils.utils import get_instance, to_snake
from utils.ux import sidebar, get_api_key

# Start page
st.title("🎥->🤗 Youtube to Companion")
st.write(
"Create your AI companion and chat about your favorite youtube video's"
)

sidebar()

manifest = load_manifest()

if not st.session_state.get("instance"):

# TODO: Add dropdown with examples
col1, col2 = st.columns(2)

col1.subheader("Attributes")
companion_template = col2.selectbox("Templates (Optional)", options=["<none>", *get_companions()])
if companion_template != "<none>":
print(companion_template)
companion = get_companion_attributes(companion_template.lower())
else:
companion = {}

personality = st.text_input("Name", value=companion.get("name", ""),
placeholder="The name of your companion")
byline = st.text_input("Byline", value=companion.get("byline", ""),
placeholder="The byline of your companion")
identity = st.text_input("Identity", value=companion.get("identity", ""),
placeholder="The identity of your companion")
behavior = st.text_input("Behavior", value=companion.get("behavior", ""),
placeholder="The behavior of your companion")
st.session_state.companion_profile_img = st.text_input("Profile picture", value=companion.get("profile_image", ""),
placeholder="The profile picture of your companion")

st.session_state.companion_first_message = st.text_input(
label="First message",
placeholder="The first message your companion sends when a new conversation starts.")

st.subheader("Long term memory")
youtube_video_url = st.text_input("Youtube Video URL")

if st.button("🤗 Spin up your companion"):

st.session_state.instance = instance = get_instance(to_snake(personality), config={
"name": personality,
"byline": byline,
"identity": identity,
"behavior": behavior,
})

if youtube_video_url:
with st.spinner("Companion is watching the video 👀..."):
add_resource(
instance.invocation_url,
str(instance.client.config.api_key),
youtube_video_url)

st.balloons()
st.experimental_rerun()

else:
instance = st.session_state.instance
companion_name = instance.config["name"]

if st.button("+ New bot"):
st.session_state.instance = None
st.experimental_rerun()

st.header(f"Start chatting with {companion_name}")
if st.session_state.get("companion_profile_img"):
st.image(st.session_state.companion_profile_img)

if "messages" not in st.session_state:
st.session_state["messages"] = [
{"role": "assistant", "content": st.session_state.companion_first_message}
]

for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])

if prompt := st.chat_input():
get_api_key()

st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = instance.invoke("prompt", prompt=prompt)
st.write(response)
st.session_state.messages.append({"role": "assistant", "content": response})
Empty file added ui/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions ui/pages/2_Manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import time

import pandas as pd
import streamlit as st
from steamship import File

from utils.ux import sidebar, get_instance

st.title("Manage your chatbot")

sidebar()


def load_and_show_videos(instance):
files = File.query(instance.client, tag_filter_query='kind is "_type"').files
videos = []
for file in files:
for block in file.blocks:
tag_key_to_value = {tag.kind: tag.name for tag in block.tags}
videos.append(
{
"Title": tag_key_to_value.get("title"),
"source": "https://www.youtube.com/watch?v="
+ tag_key_to_value.get("source"),
"thumbnail_url": tag_key_to_value.get("thumbnail_url"),
"Status": [tag.name for tag in file.tags if tag.kind == "status"][
0
],
}
)
df = pd.DataFrame(videos)
table.dataframe(
df,
column_config={
"Title": st.column_config.LinkColumn("source"),
"thumbnail_url": st.column_config.ImageColumn(label="Thumbnail"),
},
column_order=["thumbnail_url", "Title", "Status"],
)

return videos


instance = get_instance()
refresh_bar = st.progress(0, text="Time till refresh")

table = st.empty()
videos = []
i = 0
#
# if st.button("Add 10 more video's", type="primary"):
# videos = load_and_show_videos(instance)
# index_youtube_channel(st.session_state.channel_url, len(videos), 10)
# i = 0

while True:
refresh_bar.progress(i % 20 / 20, text="Time till refresh")

if i % 20 == 0:
table.text("Loading videos...")
load_and_show_videos(instance)
i += 1
time.sleep(1)
51 changes: 51 additions & 0 deletions ui/pages/3_Share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import streamlit as st

from tools.utils import sidebar, get_instance

sidebar()
st.title("Share your chatbot")
instance = get_instance()

st.subheader("Public URL")

st.code(
f"https://www.steamship.com/embed/chat?userHandle={instance.user_handle}&workspaceHandle={instance.handle}&instanceHandle={instance.handle}"
)

st.subheader("Embeddable iframe")
st.write(
"To add the chatbot any where on your website, add this iframe to your html code"
)

st.code(
"""
<iframe src="https://www.steamship.com/embed/chat?userHandle={instance.user_handle}&workspaceHandle={instance.handle}&instanceHandle={instance.handle}"
width="100%"
height="700"
frameborder="0"
></iframe>
"""
)
st.subheader("Chat bubble")

st.write(
"To add a chat bubble to the bottom right of your website add this script tag to your html"
)
st.code(
"""
<script
src="https://cdn.jsdelivr.net/gh/EniasCailliau/chatbot@main/index.js"
id="https://www.steamship.com/embed/chat?userHandle={instance.user_handle}&workspaceHandle={instance.handle}&instanceHandle={instance.handle}" >
</script>
"""
)

st.subheader("Connect to Telegram")
bot_token = st.text_input("Bot Token", type="password")
st.write(
"Learn how to create one [here](https://github.com/steamship-packages/langchain-agent-production-starter/blob/main/docs/register-telegram-bot.md)"
)
if st.button("Connect"):
response = instance.invoke("connect_telegram", bot_token=bot_token)
if response == "OK":
st.info("Chatbot successfully connected to Telegram ✅")
Empty file added ui/pages/__init__.py
Empty file.
Empty file added ui/utils/__init__.py
Empty file.
Loading

0 comments on commit f2a07c6

Please sign in to comment.