ó
    6lj  ã                  ó  • S r SSKJr  SSKrSSKJr  SSKJrJrJ	r	J
r
  SSKJr  SSKJr  \" \5      R!                  S5      r\" \5      R$                  S	   S
-  S-  rS
rSS jrSS jrSS jr\4SS jjrSSS jjr\S:X  a  \" 5         gg)u�  Load the FinanceBench benchmark JSONL files into the `financebench` schema.

Input: the two files from the FinanceBench open-source release â€”
`financebench_document_information.jsonl` (one row per filing) and
`financebench_open_source.jsonl` (one row per question, with the gold answer
and its evidence passages). The loader applies `schema.sql` (drop + recreate,
so a reload is a clean replace) and inserts everything in one pass.

The playground corpus takes its document identity from the benchmark:
`doc_key_for` maps a benchmark doc_name to the playground storage key, and every
benchmark PDF lives at s3://qubera-docs/financebench/<doc_name>.pdf.
é    )ÚannotationsN)ÚPath)ÚAnyÚLiteralStringÚOptionalÚcast)Úlogger)Údbz
schema.sqlé   ÚfinancebenchÚdatac                óB   • U R                  5       R                  SS5      $ )zvThe playground storage key for a benchmark filing: lowercased, dashes
for underscores. '3M_2022_10K' -> '3m-2022-10k'.Ú_Ú-)ÚlowerÚreplace)Údoc_names    Ú=/home/mande/repo/quber/src/quber/playground/benchmark/load.pyÚdoc_key_forr      s   € ð �>‰>Ó×#Ñ# C¨Ó-Ð-ó    c                ó0   • U b  U R                  SS5      $ S$ )zŠStrip NUL characters: Postgres text fields reject 0x00, and evidence
full-page text comes from the same PDF text layers that produce them.NÚ Ú )r   )Úss    r   Ú_pg_textr   $   s   € ð %&¡Mˆ1�9‰9�V˜RÓ Ð;°tÐ;r   c                ó¾   • U R                  5       R                  5        Vs/ s H/  oR                  5       (       d  M  [        R                  " U5      PM1     sn$ s  snf ©N)Ú	read_textÚ
splitlinesÚstripÚjsonÚloads)ÚpathÚlines     r   Ú_rowsr%   *   s>   € Ø)-¯©Ó)9×)DÑ)DÔ)FÓWÒ)F Ï*É*Ï,ÓŒD�JŠJ�tÖÑ)FÑWÐWùÒWs
   ¡A½Ac                óà  • [        U S-  5      n[        U S-  5      n[        5       n/ nU HJ  nUS   U;   a  [        R                  " SU5        M%  UR	                  US   5        UR                  U5        ML     Un[        R                  " 5        nUR                  [        [        [        R                  5       5      5        U H‰  nUR                  SUS   UR                  S5      UR                  S5      UR                  S5      [        UR                  S	5      =(       d    S
5      UR                  S5      [         SUS    S345        M‹     SnU GH=  nUR                  SUS   US   UR                  S5      UR                  S5      [        UR                  S5      =(       d    S
5      [!        US   5      [!        US   5      [!        UR                  S5      5      UR                  S5      4	5        UR                  S5      =(       d    /  H|  n	UR                  SUS   U	R                  S5      =(       d    US   U	R                  S5      [!        U	R                  S5      5      [!        U	R                  S5      5      45        US-  nM~     GM@     SSS5        [        R"                  " S[%        U5      [%        U5      WS 9  [%        U5      [%        U5      U4$ ! , (       d  f       NN= f)!zlRebuild the financebench schema from the JSONL files.

Returns (documents, questions, evidence) row counts.
z'financebench_document_information.jsonlzfinancebench_open_source.jsonlr   z=Duplicate doc_name in document information, keeping first: {}zµINSERT INTO financebench.documents
                   (doc_name, company, gics_sector, doc_type, doc_period, doc_link, s3_key)
                   VALUES (%s, %s, %s, %s, %s, %s, %s)ÚcompanyÚgics_sectorÚdoc_typeÚ
doc_periodr   Údoc_linkÚ/z.pdfr   a  INSERT INTO financebench.questions
                   (financebench_id, doc_name, question_type, question_reasoning,
                    domain_question_num, question, answer, justification, dataset_subset_label)
                   VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)Úfinancebench_idÚquestion_typeÚquestion_reasoningÚdomain_question_numÚquestionÚanswerÚjustificationÚdataset_subset_labelÚevidencezÂINSERT INTO financebench.evidence
                       (financebench_id, doc_name, evidence_page_num, evidence_text, evidence_text_full_page)
                       VALUES (%s, %s, %s, %s, %s)Úevidence_page_numÚevidence_textÚevidence_text_full_pageé   NzHLoaded financebench: {d} documents, {q} questions, {e} evidence passages)ÚdÚqÚe)r%   Úsetr	   ÚwarningÚaddÚappendr
   ÚconnectÚexecuter   r   ÚSCHEMA_FILEr   ÚgetÚstrÚ	S3_PREFIXr   ÚsuccessÚlen)
Údata_dirÚdocsÚ	questionsÚseenÚdedupedr:   ÚconnÚ
n_evidencer;   Úevs
             r   ÚloadrQ   .   sž  € ô
 �ÐEÑEÓF€DÜ�hÐ!AÑAÓB€Iô
 “U€DØ€GÛˆØˆZ‰=˜DÓ Ü�NŠNÐZÐ\]Ô^ÙØ�‰��:‘ÔØ�‰�qÖñ ð €Dä	�ŠŒ˜à�‰”Tœ-¬×)>Ñ)>Ó)@ÓAÔBãˆAØ�L‰Lð:ð �j‘MØ—E‘E˜)Ó$Ø—E‘E˜-Ó(Ø—E‘E˜*Ó%Ü˜Ÿ™˜lÓ+×1¨rÓ2Ø—E‘E˜*Ó%Ü �k  1 Z¡= /°Ð6ðö	ñ ð  ˆ
ÜˆAØ�L‰LðBð
 Ð'Ñ(Ø�j‘MØ—E‘E˜/Ó*Ø—E‘EÐ.Ó/Ü˜Ÿ™Ð3Ó4×:¸Ó;Ü˜Q˜z™]Ó+Ü˜Q˜x™[Ó)Ü˜QŸU™U ?Ó3Ó4Ø—E‘EÐ0Ó1ð
ôð" —e‘e˜JÓ'×-¨2Ò-�Ø—‘ð6ð Ð+Ñ,ØŸ™˜zÓ*×;¨a°
©mØŸ™Ð2Ó3Ü  §¡¨Ó!8Ó9Ü  §¡Ð(AÓ!BÓCðô	ð ˜a‘’
ô .ñ% ÷+ 
ôl ‡N‚NØRÜ
ˆd‹)Ü
ˆi‹.Ø
ò	ô ˆt‹9”c˜)“n jÐ0Ð0÷y 
�ús   ÂHKË
K-c                óÄ   • SS K nUR                  SS9nUR                  S[        [        5      SS9  UR                  U 5      n[        [        UR                  5      5        g )Nr   z0Load the FinanceBench JSONL files into Postgres.)Údescriptionz
--data-dirz%directory holding the two JSONL files)ÚdefaultÚhelp)	ÚargparseÚArgumentParserÚadd_argumentrE   ÚDEFAULT_DATA_DIRÚ
parse_argsrQ   r   rI   )ÚargvrV   ÚpÚargss       r   Úmainr^   ‚   sQ   € Ûà×ÑÐ,^ÐÐ_€AØ‡N�N�<¬Ô-=Ó)>ÐEl€NÑmØ�<‰<˜Ó€DÜŒˆd�m‰mÓ	Õr   Ú__main__)r   rE   ÚreturnrE   )r   úOptional[str]r`   ra   )r#   r   r`   zlist[dict[str, Any]])rI   r   r`   ztuple[int, int, int]r   )r[   zOptional[list[str]]r`   ÚNone)Ú__doc__Ú
__future__r   r!   Úpathlibr   Útypingr   r   r   r   Úlogurur	   Úquber.playgroundr
   Ú__file__Ú	with_namerC   ÚparentsrY   rF   r   r   r%   rQ   r^   Ú__name__© r   r   Ú<module>rn      sŽ   ðñõ #ã Ý ß 5Ó 5å å á�8‹n×&Ñ& |Ó4€Ù˜“>×)Ñ)¨!Ñ,¨~Ñ=ÀÑFÐ à€	ô.ô<ôXð +ö Q1öhð ˆzÓÙ…Fð r   