xfbai commited on
Commit
81b9405
·
verified ·
1 Parent(s): c858635

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -57
app.py CHANGED
@@ -1,16 +1,15 @@
 
1
  import gradio as gr
2
- import rebiber
3
  import os
4
  import uuid
5
-
6
 
7
- # Load Bib Database
8
- filepath = os.path.abspath(rebiber.__file__).replace("__init__.py","")
9
  bib_list_path = os.path.join(filepath, "bib_list.txt")
10
  abbr_tsv_path = "abbr.tsv"
11
 
12
  bib_db = rebiber.construct_bib_db(bib_list_path, start_dir=filepath)
13
-
14
  abbr_dict = rebiber.normalize.load_abbr_tsv(abbr_tsv_path)
15
 
16
 
@@ -23,55 +22,54 @@ def process(input_bib, shorten, remove_keys, deduplicate, sort):
23
  with open(f"input_{random_id}.bib", "w") as f:
24
  f.write(input_bib.replace("\t", " "))
25
  all_bib_entries = rebiber.load_bib_file(f"input_{random_id}.bib")
26
- print("# Input Bib Entries:", len(all_bib_entries))
27
  abbr_dict_pass = []
28
  if shorten:
29
  abbr_dict_pass = abbr_dict
30
- rebiber.normalize_bib(bib_db, all_bib_entries, f"output_{random_id}.bib",
31
- abbr_dict=abbr_dict_pass,
32
- deduplicate=deduplicate,
33
- sort=sort,
34
- removed_value_names=remove_keys)
 
 
 
 
35
  with open(f"output_{random_id}.bib") as f:
36
  output_bib = f.read().replace("\n ", "\n ")
37
  # delete both files
38
- # print(output_bib)
39
  return output_bib, random_id, gr.update(visible=True)
40
 
41
 
 
 
 
 
 
 
 
 
42
  example_input = """
43
- @article{lin2020birds,
44
- title={Birds have four legs?! NumerSense: Probing Numerical Commonsense Knowledge of Pre-trained Language Models},
45
- author={Lin, Bill Yuchen and Lee, Seyeon and Khanna, Rahul and Ren, Xiang},
46
- journal={arXiv preprint arXiv:2005.00683},
 
 
 
 
 
 
47
  year={2020}
48
- }
49
- @inproceedings{Lin2020CommonGenAC,
50
- title={CommonGen: A Constrained Text Generation Challenge for Generative Commonsense Reasoning},
51
- author={Bill Yuchen Lin and Minghan Shen and Wangchunshu Zhou and Pei Zhou and Chandra Bhagavatula and Yejin Choi and Xiang Ren},
52
- booktitle={Findings},
53
- year={2020}
54
- }
55
- """
56
 
57
  examples = [[example_input]]
58
 
59
-
60
- # iface = gr.Interface(fn=process,
61
- # inputs=gr.inputs.Textbox(lines=30, label="Input BIB"),
62
- # outputs=gr.outputs.Textbox(label="Output BIB").style(show_copy_button=True),
63
- # examples=examples,
64
- # allow_flagging="never"
65
- # )
66
-
67
-
68
-
69
-
70
-
71
  with gr.Blocks() as demo:
72
-
73
  gr.Markdown(
74
- '''# Rebiber: A tool for normalizing bibtex with official info.
75
  <table>
76
  <tr>
77
  <td>
@@ -98,16 +96,34 @@ with gr.Blocks() as demo:
98
  Apart from handling outdated arXiv citations, __Rebiber__ also normalizes citations in a unified way (DBLP-style), supporting abbreviation and value selection.
99
 
100
  </span>
101
- '''
102
  )
103
-
104
  with gr.Row():
105
  with gr.Column(scale=3):
106
- input_bib = gr.Textbox(lines=15, label="Input BIB", value=example_input, interactive=True)
107
- removekeys = gr.CheckboxGroup(["url", "biburl", "address", "publisher", "pages", "doi", "volume", "bibsource"],
108
- value=[False, False, False, False, False, False, False, False],
109
- label="Remove Keys", info="Which keys to remove?")
110
- shorten = gr.Checkbox(label="Abbreviation", info="Shorten the conference/journal names (e.g., `Proceedings of the 2020 International Conference of ...` --> `Proc. of ICML')", value=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  dedup = gr.Checkbox(label="Deduplicate entries.", value=False)
112
  sort = gr.Checkbox(label="Sort alphabetically by ID.", value=False)
113
  with gr.Row():
@@ -115,22 +131,32 @@ with gr.Blocks() as demo:
115
  button = gr.Button("Submit")
116
  ex_uuid = gr.Text(label="UUID")
117
  ex_uuid.visible = False
 
118
  with gr.Column(scale=3):
119
- output=gr.Textbox(label="Output BIB (Note that you can copy the output bib file by clicking the top-right button.)").style(show_copy_button=True, interactive=False)
 
 
 
 
120
  download_btn = gr.Button("Generate Bib File")
121
  download_btn.visible = False
122
- download_content = gr.outputs.File()
123
- download_content.visible = False
124
- def download_file(ex_uuid):
125
- global download_content
126
- # Replace this with your code to generate/download the file
127
- file_path = f"output_{ex_uuid}.bib"
128
- download_content.update(visible=False)
129
- return file_path, gr.update(visible=True)
130
- download_btn.click(download_file, inputs=ex_uuid, outputs=[download_content,download_content])
131
- button.click(process, inputs=[input_bib, shorten, removekeys, dedup, sort], outputs=[output, ex_uuid, download_btn], api_name = "process")
 
 
 
 
132
  def clean(text):
133
  return ""
 
134
  clr_button.click(clean, input_bib, input_bib)
135
  # gr.Interface(fn=process,
136
  # outputs=gr.outputs.Textbox(label="Output BIB").style(show_copy_button=True),
@@ -167,4 +193,4 @@ Ren, Xiang},
167
  url = {https://aclanthology.org/2020.emnlp-main.557},
168
  year = {2020}
169
  }
170
- """
 
1
+ # coding:utf-8
2
  import gradio as gr
3
+ import rebiber
4
  import os
5
  import uuid
 
6
 
7
+ # Load Bib Database
8
+ filepath = os.path.abspath(rebiber.__file__).replace("__init__.py", "")
9
  bib_list_path = os.path.join(filepath, "bib_list.txt")
10
  abbr_tsv_path = "abbr.tsv"
11
 
12
  bib_db = rebiber.construct_bib_db(bib_list_path, start_dir=filepath)
 
13
  abbr_dict = rebiber.normalize.load_abbr_tsv(abbr_tsv_path)
14
 
15
 
 
22
  with open(f"input_{random_id}.bib", "w") as f:
23
  f.write(input_bib.replace("\t", " "))
24
  all_bib_entries = rebiber.load_bib_file(f"input_{random_id}.bib")
25
+ # print("# Input Bib Entries:", len(all_bib_entries))
26
  abbr_dict_pass = []
27
  if shorten:
28
  abbr_dict_pass = abbr_dict
29
+ rebiber.normalize_bib(
30
+ bib_db,
31
+ all_bib_entries,
32
+ f"output_{random_id}.bib",
33
+ abbr_dict=abbr_dict_pass,
34
+ deduplicate=deduplicate,
35
+ sort=sort,
36
+ removed_value_names=remove_keys,
37
+ )
38
  with open(f"output_{random_id}.bib") as f:
39
  output_bib = f.read().replace("\n ", "\n ")
40
  # delete both files
 
41
  return output_bib, random_id, gr.update(visible=True)
42
 
43
 
44
+ def download_file(ex_uuid):
45
+ global download_content
46
+ # Replace this with your code to generate/download the file
47
+ file_path = f"output_{ex_uuid}.bib"
48
+ download_content.update(visible=False)
49
+ return file_path, gr.update(visible=True)
50
+
51
+
52
  example_input = """
53
+ @article{lin2020birds,
54
+ title={Birds have four legs?! NumerSense: Probing Numerical Commonsense Knowledge of Pre-trained Language Models},
55
+ author={Lin, Bill Yuchen and Lee, Seyeon and Khanna, Rahul and Ren, Xiang},
56
+ journal={arXiv preprint arXiv:2005.00683},
57
+ year={2020}
58
+ }
59
+ @inproceedings{Lin2020CommonGenAC,
60
+ title={CommonGen: A Constrained Text Generation Challenge for Generative Commonsense Reasoning},
61
+ author={Bill Yuchen Lin and Minghan Shen and Wangchunshu Zhou and Pei Zhou and Chandra Bhagavatula and Yejin Choi and Xiang Ren},
62
+ booktitle={Findings},
63
  year={2020}
64
+ }
65
+ """
 
 
 
 
 
 
66
 
67
  examples = [[example_input]]
68
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  with gr.Blocks() as demo:
70
+
71
  gr.Markdown(
72
+ """# Rebiber: A tool for normalizing bibtex with official info.
73
  <table>
74
  <tr>
75
  <td>
 
96
  Apart from handling outdated arXiv citations, __Rebiber__ also normalizes citations in a unified way (DBLP-style), supporting abbreviation and value selection.
97
 
98
  </span>
99
+ """
100
  )
101
+
102
  with gr.Row():
103
  with gr.Column(scale=3):
104
+ input_bib = gr.Textbox(
105
+ lines=15, label="Input BIB", value=example_input, interactive=True
106
+ )
107
+ removekeys = gr.CheckboxGroup(
108
+ [
109
+ "url",
110
+ "biburl",
111
+ "address",
112
+ "publisher",
113
+ "pages",
114
+ "doi",
115
+ "volume",
116
+ "bibsource",
117
+ ],
118
+ # value=[False, False, False, False, False, False, False, False],
119
+ label="Remove Keys",
120
+ info="Which keys to remove?",
121
+ )
122
+ shorten = gr.Checkbox(
123
+ label="Abbreviation",
124
+ info="Shorten the conference/journal names (e.g., `Proceedings of the 2020 International Conference of ...` --> `Proc. of ICML')",
125
+ value=False,
126
+ )
127
  dedup = gr.Checkbox(label="Deduplicate entries.", value=False)
128
  sort = gr.Checkbox(label="Sort alphabetically by ID.", value=False)
129
  with gr.Row():
 
131
  button = gr.Button("Submit")
132
  ex_uuid = gr.Text(label="UUID")
133
  ex_uuid.visible = False
134
+
135
  with gr.Column(scale=3):
136
+ output = gr.Textbox(
137
+ label="Output BIB (Note that you can copy the output bib file by clicking the top-right button.)",
138
+ show_copy_button=True,
139
+ interactive=False,
140
+ )
141
  download_btn = gr.Button("Generate Bib File")
142
  download_btn.visible = False
143
+ download_content = gr.File()
144
+ download_content.visible = False
145
+
146
+ download_btn.click(
147
+ download_file, inputs=ex_uuid, outputs=[download_content, download_content]
148
+ )
149
+
150
+ button.click(
151
+ process,
152
+ inputs=[input_bib, shorten, removekeys, dedup, sort],
153
+ outputs=[output, ex_uuid, download_btn],
154
+ api_name="process",
155
+ )
156
+
157
  def clean(text):
158
  return ""
159
+
160
  clr_button.click(clean, input_bib, input_bib)
161
  # gr.Interface(fn=process,
162
  # outputs=gr.outputs.Textbox(label="Output BIB").style(show_copy_button=True),
 
193
  url = {https://aclanthology.org/2020.emnlp-main.557},
194
  year = {2020}
195
  }
196
+ """