Skip to content

Commit

Permalink
rework io bug
Browse files Browse the repository at this point in the history
  • Loading branch information
salgadev committed Apr 18, 2024
1 parent 9813b6b commit 2e3cdd3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
12 changes: 9 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@

if uploaded_file is not None:
try:
file_ext = uploaded_file.name.split('.')[-1].lower()
pdf_file = io.BytesIO(uploaded_file.read())
docs = ingest(pdf_file, file_ext)
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp:
tmp.write(uploaded_file.read())
file_path = tmp.name

docs = ingest(file_path)
metadata = generate_metadata(docs)
st.write('## Converted Text')
st.write(metadata)

# Clean up the temporary file
os.remove(file_path)

except Exception as e:
st.error(f'Error: {e}')
13 changes: 8 additions & 5 deletions scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

import io

def ingest(file_obj, file_ext='pdf'):
if file_ext == 'pdf':
loader = UnstructuredPDFLoader(file_obj)
elif file_ext == 'txt':
loader = TextLoader(file_obj)
def ingest(file_path):
extension = os.path.splitext(file_path)[1].lower()

if extension == '.pdf':
loader = UnstructuredPDFLoader(file_path)
elif extension == '.txt':
loader = TextLoader(file_path)
else:
raise NotImplementedError('Only .txt or .pdf files are supported')

Expand All @@ -43,6 +45,7 @@ def ingest(file_obj, file_ext='pdf'):
return docs



def generate_metadata(docs):
prompt_template = """
BimDiscipline = ['plumbing', 'network', 'heating', 'electrical', 'ventilation', 'architecture']
Expand Down

0 comments on commit 2e3cdd3

Please sign in to comment.