Skip to content

Commit

Permalink
allow to copy snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
mam10eks committed Feb 4, 2024
1 parent 452551f commit 8899388
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 9 deletions.
85 changes: 85 additions & 0 deletions browser/process_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,90 @@
import yaml
import json

import textwrap

def flatten_tirex_components_to_id(obj, t=None):
ret = {}

if type(obj) != dict:
return ret

if 'tirex_submission_id' in obj:
assert obj['tirex_submission_id'] not in ret
obj['type'] = t
ret[obj['tirex_submission_id']] = obj

for k, v in obj.items():
for i, j in flatten_tirex_components_to_id(v, t if t else k).items():
ret[i] = j

return ret


id_to_component = flatten_tirex_components_to_id(yaml.load(open('links.yml').read(), Loader=yaml.FullLoader))

def get_snippet_to_run_components(component):
component = id_to_component[component['tirex_submission_id']]
component_type = component['type']
dataset_initialization = textwrap.dedent('''
# You can replace Robust04 with other datasets
dataset = pt.get_dataset("irds:disks45/nocr/trec-robust-2004")
''').strip()
snippet = ''

if component_type == 'dataset':
dataset_initialization = ''
ir_datasets_id = component.get('ir_datasets_id')
if ir_datasets_id:
snippet = f'''
dataset = pt.get_dataset('irds:{ir_datasets_id}')
indexer = pt.IterDictIndexer('./index')
indexref = indexer.index(dataset.get_corpus_iter())
'''
else:
snippet = f'''
def get_corpus_iter():
# Iterate over the {component['display_name']} corpus
corpus = ...
for doc in corpus:
yield {{'docno': doc.docno, 'text': doc.content}}
indexer = pt.IterDictIndexer('./index')
indexref = indexer.index(get_corpus_iter())
'''
elif component_type == 'document_processing':
tirex_submission_id = component.get('tirex_submission_id')
if tirex_submission_id:
snippet = f'''
transformed_docs = tira.pt.transform_documents('{tirex_submission_id}', dataset)
'''
elif component_type == 'query_processing':
tirex_submission_id = component.get('tirex_submission_id')
if tirex_submission_id:
snippet = f'''
topics = dataset.get_topics(variant='title')
transformed_queries = tira.pt.transform_queries('{tirex_submission_id}', topics)
'''
elif component_type in ('retrieval', 'reranking'):
tirex_submission_id = component.get('tirex_submission_id')
if tirex_submission_id:
snippet = f'''
run = tira.pt.from_retriever_submission('{tirex_submission_id}', dataset=dataset_id)
'''
elif component_type == 'dataset':
pass
else:
raise ValueError(f'Component type "{component_type}" does not exist...')

if snippet:
snippet = textwrap.dedent(snippet).strip()

if dataset_initialization:
snippet = dataset_initialization + '\n' + snippet

return snippet


def expand_links(component):
links = [*component.get('links', [])]
Expand All @@ -26,6 +110,7 @@ def expand_links(component):
'display_name': 'Submission in TIREx',
'href': f'/submissions/{tirex_submission_id}',
})
component['snippet'] = get_snippet_to_run_components(component)

if links:
component['links'] = links
Expand Down
18 changes: 9 additions & 9 deletions browser/src/components/ComponentOverview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<template v-slot:default="{ isActive }">
<v-card class="bg-grey-darken-3">
<v-card-text content="code">
<loading v-if="!code" loading="true"/>
<code-snippet v-if="code" :title="'Example code snippet for ' + vectorizedComponents[index][i-1]?.display_name" :code="code" expand_message=""/>
</v-card-text>

Expand Down Expand Up @@ -143,9 +142,7 @@ export default {
return componentSet;
},
fetch_code(index: number, i: number) {
this.code = ''
get('/api/tirex-snippet?component='+ this.vectorizedComponents[index][i].tirex_submission_id)
.then((message) => {this.code = message['context']['snippet']})
this.code = this.vectorizedComponents[index][i].snippet
},
colorOfComponent(c:string) : string {
return this.colors[c] ?? "grey"
Expand All @@ -171,8 +168,8 @@ export default {
is_collapsed(component:any) {
return !this.computed_expanded_entries.includes(component.display_name)
},
filtered_sub_components(component:any) : {display_name: string, subItems: number, pos: number, links: any[], focus_type: string|undefined|null, component_type: string|undefined|null, tirex_submission_id: string|undefined|null}[] {
let ret: {display_name: string, subItems: number, pos: number, links: any[], focus_type: string|undefined|null, component_type: string|undefined|null, tirex_submission_id: string|undefined|null}[] = []
filtered_sub_components(component:any) : {display_name: string, subItems: number, pos: number, links: any[], focus_type: string|undefined|null, component_type: string|undefined|null, tirex_submission_id: string|undefined|null, snippet: string|undefined|null}[] {
let ret: {display_name: string, subItems: number, pos: number, links: any[], focus_type: string|undefined|null, component_type: string|undefined|null, tirex_submission_id: string|undefined|null, snippet: string|undefined|null}[] = []
if (this.is_collapsed(component) || !component['components']) {
return ret
Expand All @@ -187,7 +184,8 @@ export default {
'links': c.hasOwnProperty('links') ? c['links'] : null,
'focus_type': c.hasOwnProperty('focus_type') ? c['focus_type'] : null,
'component_type': c.hasOwnProperty('component_type') ? c['component_type'] : null,
'tirex_submission_id': c['tirex_submission_id']
'tirex_submission_id': c['tirex_submission_id'],
'snippet': c['snippet'] || null,
})
for (let sub_c of this.filtered_sub_components(c)) {
Expand All @@ -198,7 +196,8 @@ export default {
'links': sub_c['links'],
'focus_type': sub_c.hasOwnProperty('focus_type') ? sub_c['focus_type'] : null,
'component_type': sub_c.hasOwnProperty('component_type') ? sub_c['component_type'] : null,
'tirex_submission_id': sub_c['tirex_submission_id']
'tirex_submission_id': sub_c['tirex_submission_id'],
'snippet': sub_c['snippet'] || null,
})
}
}
Expand Down Expand Up @@ -315,7 +314,8 @@ export default {
'links': subcomponent.links,
'collapsed': this.is_collapsed(subcomponent),
'hide': this.hide_component(subcomponent),
'tirex_submission_id': subcomponent['tirex_submission_id'] || null
'tirex_submission_id': subcomponent['tirex_submission_id'] || null,
'snippet': subcomponent['snippet'] || null
}
}
}
Expand Down
Loading

0 comments on commit 8899388

Please sign in to comment.