Spaces:
Runtime error
Runtime error
chendelong1999
commited on
Commit
•
f3fb4c2
1
Parent(s):
28f7b76
Add application file
Browse files- .gitattributes +1 -0
- app.py +152 -0
- name_mapping.json +3 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.json filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import requests
|
3 |
+
import pprint
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
import gradio as gr
|
7 |
+
import requests
|
8 |
+
from bs4 import BeautifulSoup
|
9 |
+
|
10 |
+
def get_dblp_bibref(title):
|
11 |
+
|
12 |
+
print(f'DBLP query: {title}')
|
13 |
+
|
14 |
+
try:
|
15 |
+
# Replace spaces in the title with '+'
|
16 |
+
title = title.replace(' ', '+')
|
17 |
+
|
18 |
+
# Send a GET request to the DBLP search page with the paper title
|
19 |
+
response = requests.get(f'https://dblp.org/search/publ/api?q={title}&format=xml')
|
20 |
+
soup = BeautifulSoup(response.content, 'lxml')
|
21 |
+
|
22 |
+
# Get the URL of the first paper in the search results
|
23 |
+
url = soup.select_one('url').text + '.bib'
|
24 |
+
response = requests.get(url)
|
25 |
+
|
26 |
+
return response.text
|
27 |
+
except Exception as e:
|
28 |
+
return f'Error during get bibref from DBLP: {e}'
|
29 |
+
|
30 |
+
# set pprint width
|
31 |
+
pp = pprint.PrettyPrinter(width=128)
|
32 |
+
API_KEY = 'eRLnjZeWSs4gHjSemy5af1X7IbugACFg1tSX6F3R'
|
33 |
+
FIELDS = "paperId,title,url,year,authors,venue,abstract,citationCount,openAccessPdf,fieldsOfStudy,publicationDate,citations,references"
|
34 |
+
|
35 |
+
|
36 |
+
# def get_name_mapping(venues_data='/nfs/delong/data/s2orc/s2ag_full/publication-venues'):
|
37 |
+
# name_mapping = {} # from full name to abbreviated name
|
38 |
+
# for file in os.listdir(venues_data):
|
39 |
+
# with open(os.path.join(venues_data, file), 'r') as f:
|
40 |
+
# venues = [json.loads(line) for line in f.readlines()]
|
41 |
+
# print(f"Total number of venues in {file}: {len(venues)}")
|
42 |
+
# for venue in venues:
|
43 |
+
# if len(venue['alternate_names'])>0:
|
44 |
+
# # name_mapping[venue['name']] = venue['alternate_names'][0]
|
45 |
+
# # instead of using the first alternate name, use the shortest one
|
46 |
+
# name_mapping[venue['name']] = min(venue['alternate_names'], key=len)
|
47 |
+
|
48 |
+
# name_mapping['Neural Information Processing Systems'] = 'NeurIPS'
|
49 |
+
|
50 |
+
# print(f'loaded {len(name_mapping)} venues from {venues_data}')
|
51 |
+
# return name_mapping
|
52 |
+
|
53 |
+
|
54 |
+
# name_mapping = get_name_mapping()
|
55 |
+
# json.dump(name_mapping, open('name_mapping.json', 'w'), indent=4)
|
56 |
+
|
57 |
+
name_mapping = json.load(open('name_mapping.json', 'r'))
|
58 |
+
print(f'loaded {len(name_mapping)} venues from name_mapping.json')
|
59 |
+
|
60 |
+
def search_paper_title_semanticscholar(title):
|
61 |
+
url = "https://api.semanticscholar.org/graph/v1/paper/search"
|
62 |
+
headers = {"Accept": "application/json", "x-api-key": API_KEY}
|
63 |
+
params = {"query": title, "limit": 1}
|
64 |
+
|
65 |
+
response = requests.get(url, headers=headers, params=params)
|
66 |
+
|
67 |
+
if response.status_code == 200:
|
68 |
+
data = response.json()
|
69 |
+
if data['total']!=0:
|
70 |
+
paper_id = data['data'][0]['paperId']
|
71 |
+
url = f"https://api.semanticscholar.org/graph/v1/paper/{paper_id}"
|
72 |
+
|
73 |
+
params = {"fields": FIELDS}
|
74 |
+
response = requests.get(url, headers=headers, params=params)
|
75 |
+
if response.status_code == 200:
|
76 |
+
data = response.json()
|
77 |
+
return data
|
78 |
+
else:
|
79 |
+
print(f"Error: {response.status_code}")
|
80 |
+
return None
|
81 |
+
else:
|
82 |
+
print("No paper found with the given title.")
|
83 |
+
return None
|
84 |
+
else:
|
85 |
+
print(f"Error: {response.status_code}")
|
86 |
+
return None
|
87 |
+
|
88 |
+
def get_abbreviated_venue(name):
|
89 |
+
if name in name_mapping:
|
90 |
+
return name_mapping[name]
|
91 |
+
else:
|
92 |
+
return name
|
93 |
+
|
94 |
+
def get_md_citation(paper_info):
|
95 |
+
|
96 |
+
# citation_str = paper_info['authors'][0]['name'] + " *et al.* "
|
97 |
+
citation_str = ', '.join([author['name'] for author in paper_info['authors']]) + '. '
|
98 |
+
citation_str += f"[**{paper_info['title']}**]({paper_info['url']}). "
|
99 |
+
citation_str += f"*{get_abbreviated_venue(paper_info['venue'])}*"
|
100 |
+
# citation_str += f" ({paper_info['year']})."
|
101 |
+
citation_str += f" ({paper_info['publicationDate'][:-3].replace('-', '.')})."
|
102 |
+
return citation_str
|
103 |
+
|
104 |
+
def summarize_paper_info(paper_info):
|
105 |
+
info_str = ""
|
106 |
+
# info_str += f"**Venue**: {paper_info['venue']}\n\n"
|
107 |
+
|
108 |
+
author_str = ''
|
109 |
+
for author in paper_info['authors']:
|
110 |
+
author_str += f"[{author['name']}](https://www.semanticscholar.org/author/{author['authorId']}), "
|
111 |
+
author_str = author_str[:-2]
|
112 |
+
|
113 |
+
info_str += f"**Authors**: {author_str}\n\n"
|
114 |
+
|
115 |
+
info_str += f"**Abstract**: {paper_info['abstract']}\n\n"
|
116 |
+
info_str += f"**Citation Count**: {paper_info['citationCount']}\n\n"
|
117 |
+
return info_str
|
118 |
+
|
119 |
+
def get_output(title):
|
120 |
+
print(f"Title query: {title}")
|
121 |
+
|
122 |
+
paper_info = search_paper_title_semanticscholar(title)
|
123 |
+
if paper_info is not None:
|
124 |
+
citation_str = get_md_citation(paper_info)
|
125 |
+
else:
|
126 |
+
citation_str = "No paper found with that title."
|
127 |
+
|
128 |
+
citation_str = f"""
|
129 |
+
**Citation**:
|
130 |
+
\n\n{citation_str}\n\n
|
131 |
+
**Markdown source code**:\n\n```markdown\n{citation_str}\n```\n\n
|
132 |
+
**BibTex**\n\n```bibtex\n{get_dblp_bibref(paper_info['title'])}\n```\n\n
|
133 |
+
---
|
134 |
+
\n\n{summarize_paper_info(paper_info)}\n\n
|
135 |
+
"""
|
136 |
+
|
137 |
+
print(citation_str)
|
138 |
+
|
139 |
+
return citation_str
|
140 |
+
|
141 |
+
def main():
|
142 |
+
iface = gr.Interface(fn=get_output,
|
143 |
+
inputs=gr.components.Textbox(lines=2, label="Please input the title of the paper"),
|
144 |
+
outputs="markdown",
|
145 |
+
allow_flagging='never',
|
146 |
+
title="Citation Tool",
|
147 |
+
)
|
148 |
+
iface.launch()
|
149 |
+
|
150 |
+
if __name__=="__main__":
|
151 |
+
main()
|
152 |
+
|
name_mapping.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:36b822eb89d6d428f67c84a4611be067f3ba2231f35ad703752994a71fb761ac
|
3 |
+
size 14314956
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
lxml
|
2 |
+
gradio
|
3 |
+
beautifulsoup4
|