|
12 | 12 |
|
13 | 13 | # Start page
|
14 | 14 | st.title("🎥->🤗 Youtube to Companion")
|
15 |
| -st.write( |
16 |
| - "Create your AI companion and chat about your favorite youtube video's" |
17 |
| -) |
| 15 | +st.write("Create your AI companion and chat about your favorite youtube video's") |
18 | 16 |
|
19 | 17 | sidebar()
|
20 | 18 |
|
|
26 | 24 | col1, col2 = st.columns(2)
|
27 | 25 |
|
28 | 26 | col1.subheader("Attributes")
|
29 |
| - companion_template = col2.selectbox("Templates (Optional)", options=["<none>", *get_companions()]) |
| 27 | + companion_template = col2.selectbox( |
| 28 | + "Templates (Optional)", options=["<none>", *get_companions()] |
| 29 | + ) |
30 | 30 | if companion_template != "<none>":
|
31 | 31 | print(companion_template)
|
32 | 32 | companion = get_companion_attributes(companion_template.lower())
|
33 | 33 | else:
|
34 | 34 | companion = {}
|
35 | 35 |
|
36 |
| - personality = st.text_input("Name", value=companion.get("name", ""), |
37 |
| - placeholder="The name of your companion") |
38 |
| - byline = st.text_input("Byline", value=companion.get("byline", ""), |
39 |
| - placeholder="The byline of your companion") |
40 |
| - identity = st.text_input("Identity", value=companion.get("identity", ""), |
41 |
| - placeholder="The identity of your companion") |
42 |
| - behavior = st.text_input("Behavior", value=companion.get("behavior", ""), |
43 |
| - placeholder="The behavior of your companion") |
44 |
| - st.session_state.companion_profile_img = st.text_input("Profile picture", value=companion.get("profile_image", ""), |
45 |
| - placeholder="The profile picture of your companion") |
| 36 | + personality = st.text_input( |
| 37 | + "Name", |
| 38 | + value=companion.get("name", ""), |
| 39 | + placeholder="The name of your companion", |
| 40 | + ) |
| 41 | + byline = st.text_input( |
| 42 | + "Byline", |
| 43 | + value=companion.get("byline", ""), |
| 44 | + placeholder="The byline of your companion", |
| 45 | + ) |
| 46 | + identity = st.text_input( |
| 47 | + "Identity", |
| 48 | + value=companion.get("identity", ""), |
| 49 | + placeholder="The identity of your companion", |
| 50 | + ) |
| 51 | + behavior = st.text_input( |
| 52 | + "Behavior", |
| 53 | + value=companion.get("behavior", ""), |
| 54 | + placeholder="The behavior of your companion", |
| 55 | + ) |
| 56 | + st.session_state.companion_profile_img = st.text_input( |
| 57 | + "Profile picture", |
| 58 | + value=companion.get("profile_image", ""), |
| 59 | + placeholder="The profile picture of your companion", |
| 60 | + ) |
46 | 61 |
|
47 | 62 | st.session_state.companion_first_message = st.text_input(
|
48 | 63 | label="First message",
|
49 |
| - placeholder="The first message your companion sends when a new conversation starts.") |
| 64 | + placeholder="The first message your companion sends when a new conversation starts.", |
| 65 | + ) |
50 | 66 |
|
51 | 67 | st.subheader("Long term memory")
|
52 | 68 | youtube_video_url = st.text_input("Youtube Video URL")
|
53 | 69 |
|
54 | 70 | if st.button("🤗 Spin up your companion"):
|
55 | 71 |
|
56 |
| - st.session_state.instance = instance = get_instance(to_snake(personality), config={ |
57 |
| - "name": personality, |
58 |
| - "byline": byline, |
59 |
| - "identity": identity, |
60 |
| - "behavior": behavior, |
61 |
| - }) |
| 72 | + st.session_state.instance = instance = get_instance( |
| 73 | + to_snake(personality), |
| 74 | + config={ |
| 75 | + "name": personality, |
| 76 | + "byline": byline, |
| 77 | + "identity": identity, |
| 78 | + "behavior": behavior, |
| 79 | + }, |
| 80 | + ) |
62 | 81 |
|
63 | 82 | if youtube_video_url:
|
64 | 83 | with st.spinner("Companion is watching the video 👀..."):
|
65 | 84 | add_resource(
|
66 | 85 | instance.invocation_url,
|
67 | 86 | str(instance.client.config.api_key),
|
68 |
| - youtube_video_url) |
| 87 | + youtube_video_url, |
| 88 | + ) |
69 | 89 |
|
70 |
| - st.balloons() |
71 |
| - st.experimental_rerun() |
| 90 | + st.balloons() |
| 91 | + st.experimental_rerun() |
72 | 92 |
|
73 | 93 | else:
|
74 | 94 | instance = st.session_state.instance
|
|
79 | 99 | st.experimental_rerun()
|
80 | 100 |
|
81 | 101 | st.header(f"Start chatting with {companion_name}")
|
82 |
| - if st.session_state.get("companion_profile_img"): |
83 |
| - st.image(st.session_state.companion_profile_img) |
84 | 102 |
|
85 | 103 | if "messages" not in st.session_state:
|
86 | 104 | st.session_state["messages"] = [
|
87 | 105 | {"role": "assistant", "content": st.session_state.companion_first_message}
|
88 | 106 | ]
|
89 | 107 |
|
| 108 | + companion_img = st.session_state.get("companion_profile_img") |
90 | 109 | for msg in st.session_state.messages:
|
91 |
| - st.chat_message(msg["role"]).write(msg["content"]) |
| 110 | + if msg["role"] == "assistant": |
| 111 | + st.chat_message(msg["role"], avatar=companion_img).write(msg["content"]) |
| 112 | + else: |
| 113 | + st.chat_message(msg["role"]).write(msg["content"]) |
92 | 114 |
|
93 | 115 | if prompt := st.chat_input():
|
94 | 116 | get_api_key()
|
95 |
| - |
96 | 117 | st.session_state.messages.append({"role": "user", "content": prompt})
|
97 | 118 | st.chat_message("user").write(prompt)
|
98 | 119 | with st.chat_message("assistant"):
|
99 | 120 | with st.spinner("Thinking..."):
|
100 |
| - response = instance.invoke("prompt", prompt=prompt) |
101 |
| - st.write(response) |
| 121 | + responses = instance.invoke("prompt", prompt=prompt) |
| 122 | + for response in responses: |
| 123 | + print(response) |
| 124 | + mime_type = response["mimeType"] |
| 125 | + if mime_type is None: |
| 126 | + st.write(response["text"]) |
| 127 | + elif "audio" in mime_type: |
| 128 | + st.audio(response["url"]) |
102 | 129 | st.session_state.messages.append({"role": "assistant", "content": response})
|
0 commit comments