Spaces:
Running
Running
clementsan commited on
Commit ·
9bf736d
1
Parent(s): 5b34e60
Improve creation of collection name
Browse files19 unmodified lines202122232425153 unmodified lines17918018118218318418518618718818919019119219319419519619719819920020120220320420519 unmodified linesimport torchimport tqdmimport accelerate153 unmodified linesreturn qa_chain# Initialize databasedef initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):# Create list of documents (when valid)list_file_path = [x.name for x in list_file_obj if x is not None]# Create collection_name for vector databaseprogress(0.1, desc="Creating collection name...")collection_name = Path(list_file_path[0]).stem# Fix potential issues from naming convention## Remove spacecollection_name = collection_name.replace(" ","-")## ASCII transliterations of Unicode textcollection_name = unidecode(collection_name)## Limit lenght to 50 characterscollection_name = collection_name[:50]## Enforce start and end as alphanumeric characterif not collection_name[0].isalnum():collection_name[0] = 'A'if not collection_name[-1].isalnum():collection_name[-1] = 'Z'# print('list_file_path: ', list_file_path)print('Collection name: ', collection_name)progress(0.25, desc="Loading document...")# Load document and create splitsdoc_splits = load_doc(list_file_path, chunk_size, chunk_overlap)19 unmodified lines20212223242526153 unmodified lines18018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121219 unmodified linesimport torchimport tqdmimport accelerateimport re153 unmodified linesreturn qa_chain# Generate collection name for vector database# - Use filepath as input, ensuring unicode textdef create_collection_name(filepath):# Extract filename without extensioncollection_name = Path(filepath).stem# Fix potential issues from naming convention## Remove spacecollection_name = collection_name.replace(" ","-")## ASCII transliterations of Unicode textcollection_name = unidecode(collection_name)## Remove special characters#collection_name = re.findall("[\dA-Za-z]*", collection_name)[0]collection_name = re.sub('[^A-Za-z0-9]+', '-', collection_name)## Limit lenght to 50 characterscollection_name = collection_name[:50]print('Filepath: ', filepath)print('Collection name: ', collection_name)return collection_name# Initialize databasedef initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):# Create list of documents (when valid)list_file_path = [x.name for x in list_file_obj if x is not None]# Create collection_name for vector databaseprogress(0.1, desc="Creating collection name...")collection_name = create_collection_name(list_file_path[0])progress(0.25, desc="Loading document...")# Load document and create splitsdoc_splits = load_doc(list_file_path, chunk_size, chunk_overlap)
app.py+20-13