Skip to content

Commit 542616e

Browse files
authored
Add image display in streamlit tool display (#349)
Signed-off-by: Kacper Dąbrowski <[email protected]>
1 parent 47e74fe commit 542616e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/rai/rai/agents/integrations/streamlit.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import base64
1516
import inspect
1617
from typing import Any, Callable, Dict, TypeVar
1718

19+
import cv2
20+
import numpy as np
1821
import streamlit as st
1922
from langchain_core.callbacks.base import BaseCallbackHandler
2023
from streamlit.delta_generator import DeltaGenerator
@@ -102,9 +105,19 @@ def on_tool_end(self, output: Any, **kwargs: Any) -> Any:
102105
"""
103106
# We assume that `on_tool_end` comes after `on_tool_start`, meaning output_placeholder exists
104107
if self.tool_output_placeholder:
105-
self.tool_output_placeholder.code(
106-
output.content
107-
) # Display the tool's output
108+
with self.tool_output_placeholder.container():
109+
st.code(output.content) # Display the tool's output
110+
111+
if output.artifact is not None:
112+
if "images" in output.artifact: # Display available images
113+
for image in output.artifact["images"]:
114+
image_cv2 = cv2.imdecode(
115+
np.frombuffer(base64.b64decode(image), np.uint8),
116+
cv2.IMREAD_COLOR,
117+
)
118+
st.image(
119+
image_cv2, channels="BGR", use_container_width=True
120+
)
108121

109122
# Define a type variable for generic type hinting in the decorator, to maintain
110123
# input function and wrapped function return type

0 commit comments

Comments
 (0)