Spaces:
Sleeping
Sleeping
add qdrant collection mapper (for visual rag)
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208"""Filename mapping between v1 (text) and visual Qdrant collections.
V1 Collection (BAAI-bge-m3): - No .pdf extension - Format: "Annual Consolidated OAG 2020", "Gulu District Local Government 2022"
Visual Collection (colSmol-500M-v2): - Has .pdf extension - Format: "Annual Consolidated OAG audit reports 2020.pdf", "Gulu DLG Report of Auditor General 2022.pdf""""
import refrom typing import List, Optional
V1_TO_VISUAL_PATTERNS = [ { 'v1_pattern': r'^Annual Consolidated OAG\s*(\d{4})$', 'visual_format': 'Annual Consolidated OAG audit reports {year}.pdf' }, { 'v1_pattern': r'^Gulu District Local Government\s*(\d{4})$', 'visual_format': 'Gulu DLG Report of Auditor General {year}.pdf' }, { 'v1_pattern': r'^Kalangala District Local Government\s*(\d{4})$', 'visual_format': 'Kalangala DLG Report of Auditor General {year}.pdf' }, { 'v1_pattern': r'^KCCA\s*(\d{4})$', 'visual_format': 'KCCA Report of Auditor General {year}.pdf' }, { 'v1_pattern': r'^Ministry of Agriculture, Animal Industry and Fisheries\s*(\d{4})$', 'visual_format': 'MAAIF Report of Auditor General {year}.pdf' }, { 'v1_pattern': r'^Ministry of Works and Transport \(MoWT\)\s*(\d{4})$', 'visual_format': 'MWTS Report of Auditor General {year}.pdf' },]
VISUAL_COLLECTION_FILENAMES = [ "Annual Consolidated OAG audit reports 2018.pdf", "Annual Consolidated OAG audit reports 2019.pdf", "Annual Consolidated OAG audit reports 2020.pdf", "Annual Consolidated OAG audit reports 2021.pdf", "Annual Consolidated OAG audit reports 2022.pdf", "Gulu DLG Report of Auditor General 2018.pdf", "Gulu DLG Report of Auditor General 2019.pdf", "Gulu DLG Report of Auditor General 2020.pdf", "Gulu DLG Report of Auditor General 2021.pdf", "Gulu DLG Report of Auditor General 2022.pdf", "KCCA Report of Auditor General 2018.pdf", "KCCA Report of Auditor General 2019.pdf", "KCCA Report of Auditor General 2020.pdf", "KCCA Report of Auditor General 2021.pdf", "KCCA Report of Auditor General 2022.pdf", "Kalangala DLG Report of Auditor General 2018.pdf", "Kalangala DLG Report of Auditor General 2019.pdf", "Kalangala DLG Report of Auditor General 2020.pdf", "Kalangala DLG Report of Auditor General 2021.pdf", "Kalangala DLG Report of Auditor General 2022.pdf", "MAAIF Report of Auditor General 2019.pdf", "MAAIF Report of Auditor General 2021.pdf", "MAAIF Report of Auditor General 2022.pdf", "MWTS Report of Auditor General 2019.pdf", "MWTS Report of Auditor General 2020.pdf", "MWTS Report of Auditor General 2021.pdf", "MWTS Report of Auditor General 2022.pdf",]
VISUAL_TO_V1_MAP = { "Annual Consolidated OAG audit reports": "Annual Consolidated OAG ", "Gulu DLG Report of Auditor General": "Gulu District Local Government ", "Kalangala DLG Report of Auditor General": "Kalangala District Local Government ", "KCCA Report of Auditor General": "KCCA ", "MAAIF Report of Auditor General": "Ministry of Agriculture, Animal Industry and Fisheries ", "MWTS Report of Auditor General": "Ministry of Works and Transport (MoWT) ",}
def v1_filename_to_visual(v1_filename: str) -> Optional[str]: """ Convert a v1 filename to visual collection format. Args: v1_filename: Filename from v1 collection (e.g., "Annual Consolidated OAG 2020") Returns: Visual collection filename or None if no mapping found """ for pattern_info in V1_TO_VISUAL_PATTERNS: match = re.match(pattern_info['v1_pattern'], v1_filename.strip()) if match: year = match.group(1) return pattern_info['visual_format'].format(year=year) if not v1_filename.endswith('.pdf'): candidate = v1_filename.strip() + '.pdf' if candidate in VISUAL_COLLECTION_FILENAMES: return candidate return None
def v1_filenames_to_visual(v1_filenames: List[str]) -> List[str]: """ Convert a list of v1 filenames to visual collection format. Returns only filenames that have valid mappings. """ visual_filenames = [] for v1_fn in v1_filenames: visual_fn = v1_filename_to_visual(v1_fn) if visual_fn: visual_filenames.append(visual_fn) return visual_filenames
def visual_filename_to_v1(visual_filename: str) -> Optional[str]: """ Convert a visual filename back to v1 format. """ fn = visual_filename.replace('.pdf', '').strip() for visual_prefix, v1_prefix in VISUAL_TO_V1_MAP.items(): if fn.startswith(visual_prefix): year = fn.replace(visual_prefix, '').strip() return f"{v1_prefix}{year}" return fn
def get_visual_filter_options() -> dict: """ Get filter options for visual collection. These are the actual filenames available in the visual collection. """ years = set() for fn in VISUAL_COLLECTION_FILENAMES: match = re.search(r'(\d{4})\.pdf$', fn) if match: years.add(match.group(1)) v1_style_filenames = [] for visual_fn in VISUAL_COLLECTION_FILENAMES: v1_fn = visual_filename_to_v1(visual_fn) if v1_fn: v1_style_filenames.append(v1_fn) return { 'filenames': sorted(set(v1_style_filenames)), 'years': sorted(years), 'sources': ['Consolidated'], 'districts': [] }
def translate_filters_for_visual(filters: dict) -> dict: """ Translate filters from v1 format to visual collection format. Args: filters: Dict with keys like 'filenames', 'years', 'sources', 'districts' Returns: Translated filters for visual collection """ translated = {} if filters.get('filenames'): visual_filenames = v1_filenames_to_visual(filters['filenames']) if visual_filenames: translated['filenames'] = visual_filenames if filters.get('years'): translated['years'] = filters['years'] if filters.get('sources'): translated['sources'] = filters['sources'] return translated
if __name__ == "__main__": print("Testing v1 to visual filename conversion:\n") test_cases = [ "Annual Consolidated OAG 2018", "Annual Consolidated OAG 2019", "Annual Consolidated OAG 2020", "Gulu District Local Government 2022", "KCCA 2022", "Kalangala District Local Government 2021", "Unknown Report 2023", ] for v1_fn in test_cases: visual_fn = v1_filename_to_visual(v1_fn) print(f" V1: {v1_fn!r}") print(f" ->: {visual_fn!r}") print() print("\nVisual filter options:") options = get_visual_filter_options() print(f" Filenames: {options['filenames'][:5]}...") print(f" Years: {options['years']}")
src/config/visual_filename_mapping.pyAdded+208