domenicrosati commited on
Commit
7fbcea5
β€’
1 Parent(s): 7a77b3d

initial smoke test

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. README.md +1 -1
  3. app.py +128 -0
  4. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .streamlit
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Scite Qa Demo
3
  emoji: 🐠
4
  colorFrom: pink
5
  colorTo: indigo
 
1
  ---
2
+ title: CitationQA - Answers from science with citations
3
  emoji: 🐠
4
  colorFrom: pink
5
  colorTo: indigo
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+
6
+ SCITE_API_KEY = st.secrets["SCITE_API_KEY"]
7
+
8
+ def remove_html(x):
9
+ soup = BeautifulSoup(x, 'html.parser')
10
+ text = soup.get_text()
11
+ return text
12
+
13
+ def search(term, limit=10):
14
+ search = f"https://api.scite.ai/search?mode=citations&term={term}&limit={limit}&offset=0&user_slug=domenic-rosati-keW5&compute_aggregations=false"
15
+ req = requests.get(
16
+ search,
17
+ headers={
18
+ 'Authorization': f'Bearer {SCITE_API_KEY}'
19
+ }
20
+ )
21
+ return (
22
+ remove_html('\n'.join(['\n'.join([cite['snippet'] for cite in doc['citations']]) for doc in req.json()['hits']])),
23
+ [(doc['doi'], doc['citations'], doc['title']) for doc in req.json()['hits']]
24
+ )
25
+
26
+
27
+ def find_source(text, docs):
28
+ for doc in docs:
29
+ if text in remove_html(doc[1][0]['snippet']):
30
+ new_text = text
31
+ for snip in remove_html(doc[1][0]['snippet']).split('.'):
32
+ if text in snip:
33
+ new_text = snip
34
+ return {
35
+ 'citation_statement': doc[1][0]['snippet'].replace('<strong class="highlight">', '').replace('</strong>', ''),
36
+ 'text': new_text,
37
+ 'from': doc[1][0]['source'],
38
+ 'supporting': doc[1][0]['target'],
39
+ 'source_title': doc[2],
40
+ 'source_link': f"https://scite.ai/reports/{doc[0]}"
41
+ }
42
+ return {
43
+ 'citation_statement': '',
44
+ 'text': text,
45
+ 'from': '',
46
+ 'supporting': ''
47
+ }
48
+
49
+ @st.experimental_singleton
50
+ def init_models():
51
+ question_answerer = pipeline("question-answering", model='sultan/BioM-ELECTRA-Large-SQuAD2-BioASQ8B')
52
+ return question_answerer
53
+
54
+ qa_model = init_models()
55
+
56
+
57
+ def card(title, context, score, link):
58
+ return st.markdown(f"""
59
+ <div class="container-fluid">
60
+ <div class="row align-items-start">
61
+ <div class="col-md-12 col-sm-12">
62
+ <br>
63
+ <span>
64
+ {context}
65
+ [<b>Score: </b>{score}]
66
+ </span>
67
+ <br>
68
+ <b>From <a href="{link}">{title}</a></b>
69
+ </div>
70
+ </div>
71
+ </div>
72
+ """, unsafe_allow_html=True)
73
+
74
+ st.title("Scientific Question Answering with Citations")
75
+
76
+ st.write("""
77
+ Ask a scientific question and get an answer drawn from [scite.ai](https://scite.ai) corpus of over 1.1bn citation statements.
78
+ Answers are linked to source documents containing citations where users can explore further evidence from scientific literature for the answer.
79
+ """)
80
+
81
+ st.markdown("""
82
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
83
+ """, unsafe_allow_html=True)
84
+
85
+ def run_query(query):
86
+ context, orig_docs = search(query)
87
+ if not context.strip():
88
+ return st.markdown("""
89
+ <div class="container-fluid">
90
+ <div class="row align-items-start">
91
+ <div class="col-md-12 col-sm-12">
92
+ Sorry... no results for that question! Try another.
93
+ </div>
94
+ </div>
95
+ </div>
96
+ """)
97
+
98
+ results = []
99
+ model_results = qa_model(question=query, context=context, top_k=10)
100
+ for result in model_results:
101
+ support = find_source(result['answer'], orig_docs)
102
+ results.append({
103
+ "answer": support['text'],
104
+ "title": support['source_title'],
105
+ "link": support['source_link'],
106
+ "context": support['citation_statement'],
107
+ "score": result['score']
108
+ })
109
+
110
+
111
+
112
+ sorted_result = sorted(results, key=lambda x: x['score'], reverse=True)
113
+ sorted_result = list({
114
+ result['context']: result for result in sorted_result
115
+ }.values())
116
+
117
+
118
+ for r in sorted_result:
119
+ answer = r["answer"]
120
+ ctx = r["context"].replace(answer, f"<mark>{answer}</mark>").replace('<cite', '<a').replace('</cite', '</a').replace('data-doi="', 'href="https://scite.ai/reports/')
121
+ title = r["title"].replace("_", " ")
122
+ score = round(r["score"], 4)
123
+ card(title, ctx, score, r['link'])
124
+
125
+ query = st.text_input("Ask scientific literature a question", "")
126
+
127
+ if query != "":
128
+ run_query(query)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ requests
3
+ beautifulsoup4
4
+ streamlit==1.2.0