Download src/app_utils.py from ThireshS/NBForge: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/spaces/ThireshS/NBForge/resolve/main/src/app_utils.py
- Command line
-
hf download hf://spaces/ThireshS/NBForge/src/app_utils.py
-
curl -L -o app_utils.py https://huggingface.co/spaces/ThireshS/NBForge/resolve/main/src/app_utils.py
3.84 kB
| 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 generate_notebook_plan_text(plan: Dict[str, Any], output_txt_file: str = "notebook_plan.txt") -> str: | |
| # """ | |
| # Converts a JSON notebook plan into a human-readable text document. | |
| # Args: | |
| # plan (Dict[str, Any]): The JSON notebook plan. | |
| # output_txt_file (str): The filename to save the generated text document. | |
| # Returns: | |
| # str: The generated text content. | |
| # """ | |
| # # Define the system prompt to guide the LLM's behavior | |
| # system_prompt = """ | |
| # You are an expert AI curriculum designer. Given a structured JSON notebook plan, your task is to convert it into a clear, readable text document. The document should outline the notebook's structure, including sections and subsections, specifying whether each is a markdown or code block. For code blocks, include the 'code_rules' provided. For markdown blocks, include the 'description'. Format the output with appropriate headings and bullet points for clarity. | |
| # """ | |
| # # Convert the JSON plan to a formatted string | |
| # plan_str = json.dumps(plan, indent=2) | |
| # # Create the messages for the LLM | |
| # messages = [ | |
| # SystemMessage(content=system_prompt), | |
| # HumanMessage(content=f"Here is the JSON notebook plan:\n\n{plan_str}") | |
| # ] | |
| # # Invoke the LLM with the messages | |
| # response = model.invoke(messages) | |
| # # Extract the generated text content | |
| # text_content = response.content | |
| # # Save the generated text to a file | |
| # with open(output_txt_file, "w", encoding="utf-8") as f: | |
| # f.write(text_content) | |
| # return text_content | |
| 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"] | |
| # base64 decode | |
| img = Image.open(io.BytesIO(base64.b64decode(img_data))) | |
| st.image(img) | |
| except Exception: | |
| pass |