Skip to content

Commit

Permalink
Merge pull request stanford-oval#147 from sureenheer/main
Browse files Browse the repository at this point in the history
Fixed OSError [Errno 63] File name too long
  • Loading branch information
shaoyijia authored Aug 21, 2024
2 parents 53b7f1e + 376d2b2 commit 5c9e714
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions frontend/demo_light/demo_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from knowledge_storm.lm import OpenAIModel
from knowledge_storm.rm import YouRM
from knowledge_storm.storm_wiki.modules.callback import BaseCallbackHandler
from knowledge_storm.utils import truncate_filename
from stoc import stoc


Expand Down
7 changes: 4 additions & 3 deletions frontend/demo_light/pages_util/CreateNewArticle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import demo_util
import streamlit as st
from demo_util import DemoFileIOHelper, DemoTextProcessingHelper, DemoUIHelper
from demo_util import DemoFileIOHelper, DemoTextProcessingHelper, DemoUIHelper, truncate_filename


def create_new_article_page():
Expand Down Expand Up @@ -33,6 +33,7 @@ def create_new_article_page():

st.session_state["page3_topic_name_cleaned"] = st.session_state["page3_topic"].replace(
' ', '_').replace('/', '_')
st.session_state["page3_topic_name_truncated"] = truncate_filename(st.session_state["page3_topic_name_cleaned"])
if not pass_appropriateness_check:
st.session_state["page3_write_article_state"] = "not started"
alert = st.warning(st.session_state["page3_warning_message"], icon="⚠️")
Expand Down Expand Up @@ -65,7 +66,7 @@ def create_new_article_page():
callback_handler=st_callback_handler
)
conversation_log_path = os.path.join(st.session_state["page3_current_working_dir"],
st.session_state["page3_topic_name_cleaned"], "conversation_log.json")
st.session_state["page3_topic_name_truncated"], "conversation_log.json")
demo_util._display_persona_conversations(DemoFileIOHelper.read_json_file(conversation_log_path))
st.session_state["page3_write_article_state"] = "final_writing"
status.update(label="brain**STORM**ing complete!", state="complete")
Expand Down Expand Up @@ -96,7 +97,7 @@ def create_new_article_page():
# display polished article
current_working_dir_paths = DemoFileIOHelper.read_structure_to_dict(
st.session_state["page3_current_working_dir"])
current_article_file_path_dict = current_working_dir_paths[st.session_state["page3_topic_name_cleaned"]]
current_article_file_path_dict = current_working_dir_paths[st.session_state["page3_topic_name_truncated"]]
demo_util.display_article_page(selected_article_name=st.session_state["page3_topic_name_cleaned"],
selected_article_file_path_dict=current_article_file_path_dict,
show_title=True, show_main_article=True)
4 changes: 2 additions & 2 deletions knowledge_storm/storm_wiki/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .modules.storm_dataclass import StormInformationTable, StormArticle
from ..interface import Engine, LMConfigs
from ..lm import OpenAIModel
from ..utils import FileIOHelper, makeStringRed
from ..utils import FileIOHelper, makeStringRed, truncate_filename


class STORMWikiLMConfigs(LMConfigs):
Expand Down Expand Up @@ -283,7 +283,7 @@ def run(self,
"No action is specified. Please set at least one of --do-research, --do-generate-outline, --do-generate-article, --do-polish-article")

self.topic = topic
self.article_dir_name = topic.replace(' ', '_').replace('/', '_')
self.article_dir_name = truncate_filename(topic.replace(' ', '_').replace('/', '_'))
self.article_output_dir = os.path.join(self.args.output_dir, self.article_dir_name)
os.makedirs(self.article_output_dir, exist_ok=True)

Expand Down
14 changes: 14 additions & 0 deletions knowledge_storm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@

logging.getLogger("httpx").setLevel(logging.WARNING) # Disable INFO logging for httpx.

def truncate_filename(filename, max_length=125):
"""Truncate filename to max_length to ensure the filename won't exceed the file system limit.
Args:
filename: str
max_length: int, default to 125 (usual path length limit is 255 chars)
"""

if len(filename) > max_length:
truncated_filename = filename[:max_length]
logging.warning(f"Filename is too long. Filename is truncated to {truncated_filename}.")
return truncated_filename

return filename

def load_api_key(toml_file_path):
try:
Expand Down

0 comments on commit 5c9e714

Please sign in to comment.