| | import json |
| | from langchain.schema import SystemMessage, HumanMessage |
| | from typing import Dict, Any |
| | from langchain_openai import ChatOpenAI |
| | import nbformat |
| | from io import StringIO |
| | import nbformat, streamlit as st |
| | from PIL import Image |
| | import base64, io |
| |
|
| |
|
| | def style_css(file_name): |
| | with open(file_name) as f: |
| | st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) |
| |
|
| |
|
| | def parse_data_dictionary(input_str: str) -> dict: |
| | """ |
| | Converts a plain text data dictionary into a structured JSON format. |
| | |
| | Args: |
| | input_str (str): Data dictionary in plain text format. |
| | |
| | Returns: |
| | dict: Structured JSON representation of the data dictionary. |
| | """ |
| | lines = input_str.strip().split("\n") |
| | data_dict = {} |
| | for line in lines: |
| | parts = line.split(":") |
| | if len(parts) == 2: |
| | key = parts[0].strip() |
| | value = parts[1].strip() |
| | data_dict[key] = value |
| | return data_dict |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| |
|
| |
|
| |
|
| | def display_notebook(path_or_buffer): |
| | """ |
| | path_or_buffer: can be a file-like object (e.g., UploadedFile) or a StringIO containing JSON text. |
| | """ |
| | try: |
| | nb = nbformat.read(path_or_buffer, as_version=4) |
| | except Exception as err: |
| | st.error(f"Failed to read notebook: {err}") |
| | return |
| |
|
| | for cell in nb.cells: |
| | if cell.cell_type == "markdown": |
| | st.markdown(cell.source) |
| | elif cell.cell_type == "code": |
| | st.code(cell.source) |
| | for out in cell.get("outputs", []): |
| | if out["output_type"] == "stream": |
| | text = out.get("text", "") |
| | st.text(text) |
| | elif out["output_type"] in ("display_data", "execute_result"): |
| | data = out.get("data", {}) |
| | if "text/plain" in data: |
| | st.text(data["text/plain"]) |
| | if "image/png" in data: |
| | try: |
| | img_data = data["image/png"] |
| | |
| | img = Image.open(io.BytesIO(base64.b64decode(img_data))) |
| | st.image(img) |
| | except Exception: |
| | pass |