Skip to content

Commit

Permalink
Update file.py
Browse files Browse the repository at this point in the history
  • Loading branch information
srdas committed Nov 14, 2024
1 parent 4bd784b commit d713fef
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/jupyter-ai/jupyter_ai/context_providers/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_file_type(self, filepath):
try:
with open(filepath, "rb") as file:
file_header = file.read(4)
if file_header == b"\x89PNG":
if file_header == b"\x89PNG" or file_header == b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a":
return ".png"
elif file_header == b"\xff\xd8\xff\xe0":
return ".jpg"
Expand All @@ -77,6 +77,8 @@ def get_file_type(self, filepath):
return ".gz"
elif file_header == b"\x50\x4b\x03\x04":
return ".zip"
elif file_header == b"\x75\x73\x74\x61\x72":
return ".tar"
elif file_header == b"\x25\x50\x44\x46":
return ".pdf"
else:
Expand Down Expand Up @@ -128,10 +130,16 @@ def _make_command_context(self, command: ContextCommand) -> str:
)
except UnicodeDecodeError:
file_extension = self.get_file_type(filepath)
raise ContextProviderException(
f"The `{file_extension}` file format is not supported for passing context to the LLM. "
f"The `@file` command only supports plaintext files."
)
if file_extension:
raise ContextProviderException(
f"The `{file_extension}` file format is not supported for passing context to the LLM. "
f"The `@file` command only supports plaintext files."
)
else:
raise ContextProviderException(
f"This file format is not supported for passing context to the LLM. "
f"The `@file` command only supports plaintext files."
)
return FILE_CONTEXT_TEMPLATE.format(
filepath=filepath,
content=self._process_file(content, filepath),
Expand Down

0 comments on commit d713fef

Please sign in to comment.