Sean MacAvaney commited on
Commit
3ed7c41
1 Parent(s): 232edb2
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import pandas as pd
2
  import gradio as gr
3
  from pyterrier_doc2query import Doc2Query
@@ -6,6 +7,12 @@ MODEL = 'macavaney/doc2query-t5-base-msmarco'
6
 
7
  doc2query = Doc2Query(MODEL, append=True, num_samples=5)
8
 
 
 
 
 
 
 
9
  def df2code(df):
10
  rows = []
11
  for row in df.itertuples(index=False):
@@ -15,21 +22,32 @@ def df2code(df):
15
  {rows}
16
  ])'''
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def predict(input, model, append, num_samples):
19
  assert model == MODEL
20
  doc2query.append = append
21
  doc2query.num_samples = num_samples
22
- code = f'''
23
- **Code:**
24
-
25
- ```python
26
- import pandas as pd
27
  from pyterrier_doc2query import Doc2Query
28
  doc2query = Doc2Query({repr(model)}, append={append}, num_samples={num_samples})
29
  doc2query({df2code(input)})
30
- ```
31
  '''
32
- return (doc2query(input), code)
33
 
34
  example_inp = pd.DataFrame([
35
  {'docno': '0', 'text': 'The presence of communication amid scientific minds was equally important to the success of the Manhattan Project as scientific intellect was. The only cloud hanging over the impressive achievement of the atomic researchers and engineers is what their success truly meant; hundreds of thousands of innocent lives obliterated.'},
 
1
+ import base64
2
  import pandas as pd
3
  import gradio as gr
4
  from pyterrier_doc2query import Doc2Query
 
7
 
8
  doc2query = Doc2Query(MODEL, append=True, num_samples=5)
9
 
10
+ COLAB_NAME = 'pyterrier_doc2query.ipynb'
11
+ COLAB_INSTALL = '''
12
+ !pip install -q git+https://github.com/terrier-org/pyterrier
13
+ !pip install -q git+https://github.com/terrierteam/pyterrier_doc2query
14
+ '''.strip()
15
+
16
  def df2code(df):
17
  rows = []
18
  for row in df.itertuples(index=False):
 
22
  {rows}
23
  ])'''
24
 
25
+ def code2colab(code):
26
+ enc_code = base64.b64encode((COLAB_INSTALL + '\n\n' + code).encode()).decode()
27
+ url = f'https://colaburl.macavaney.us/?py64={enc_code}&name={COLAB_NAME}'
28
+ return f'<a href="{url}" rel="nofollow" target="_blank" style="float: right;"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" style="margin: 0;" /></a>'
29
+
30
+ def code2md(code):
31
+ return f'''
32
+ {code2colab(code)}
33
+
34
+ **Code:**
35
+
36
+ ```python
37
+ {code}
38
+ ```
39
+ '''
40
+
41
  def predict(input, model, append, num_samples):
42
  assert model == MODEL
43
  doc2query.append = append
44
  doc2query.num_samples = num_samples
45
+ code = f'''import pandas as pd
 
 
 
 
46
  from pyterrier_doc2query import Doc2Query
47
  doc2query = Doc2Query({repr(model)}, append={append}, num_samples={num_samples})
48
  doc2query({df2code(input)})
 
49
  '''
50
+ return (doc2query(input), code2md(code))
51
 
52
  example_inp = pd.DataFrame([
53
  {'docno': '0', 'text': 'The presence of communication amid scientific minds was equally important to the success of the Manhattan Project as scientific intellect was. The only cloud hanging over the impressive achievement of the atomic researchers and engineers is what their success truly meant; hundreds of thousands of innocent lives obliterated.'},