improve handling of bad content types

This commit is contained in:
LUIS NOVO 2024-10-27 17:26:56 -03:00
parent 8e91574938
commit 790d59547b
3 changed files with 36 additions and 12 deletions

View file

@ -10,6 +10,12 @@ class DatabaseOperationError(OpenNotebookError):
pass
class UnsupportedTypeException(OpenNotebookError):
"""Raised when an unsupported type is provided."""
pass
class NoSchemaFound(OpenNotebookError):
"""Raised when a database schema is not found."""

View file

@ -16,6 +16,7 @@ from youtube_transcript_api import YouTubeTranscriptApi # type: ignore
from youtube_transcript_api.formatters import TextFormatter # type: ignore
from open_notebook.config import CONFIG
from open_notebook.exceptions import UnsupportedTypeException
class SourceState(TypedDict):
@ -513,7 +514,9 @@ def file_type_edge(data: SourceState):
elif data.get("identified_type").startswith("audio"):
return "extract_audio"
else:
return "end"
raise UnsupportedTypeException(
f"Unsupported file type: {data.get('identified_type')}"
)
workflow = StateGraph(SourceState)

View file

@ -9,6 +9,7 @@ from loguru import logger
from open_notebook.config import UPLOADS_FOLDER
from open_notebook.domain import Asset, Source
from open_notebook.exceptions import UnsupportedTypeException
from open_notebook.graphs.content_process import graph
from open_notebook.graphs.multipattern import graph as transform_graph
from open_notebook.utils import surreal_clean
@ -138,17 +139,31 @@ def add_source(session_id):
logger.debug("Adding source")
with st.status("Processing...", expanded=True):
st.write("Processing document...")
result = graph.invoke(req)
st.write("Saving..")
source = Source(
asset=Asset(url=req.get("url"), file_path=req.get("file_path")),
full_text=surreal_clean(result["content"]),
title=result.get("title"),
)
source.save()
source.add_to_notebook(st.session_state[session_id]["notebook"].id)
st.write("Summarizing...")
source.generate_toc_and_title()
try:
result = graph.invoke(req)
st.write("Saving..")
source = Source(
asset=Asset(url=req.get("url"), file_path=req.get("file_path")),
full_text=surreal_clean(result["content"]),
title=result.get("title"),
)
source.save()
source.add_to_notebook(st.session_state[session_id]["notebook"].id)
st.write("Summarizing...")
source.generate_toc_and_title()
except UnsupportedTypeException:
st.warning(
"This type of content is not supported yet. If you think it should be, let us know on the project Issues's page"
)
st.link_button(
"Go to Github Issues",
url="https://www.github.com/lfnovo/open_notebook/issues",
)
st.stop()
except Exception as e:
st.error(e)
return
st.rerun()