Commit ·
c6fcc72
0
Parent(s):
Duplicate from NealCaren/smhunt
Browse files- .gitattributes +36 -0
- README.md +13 -0
- app.py +185 -0
- emerac.png +0 -0
- requirements.txt +5 -0
.gitattributes
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
passages_0.jsonl filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
passages_4.jsonl filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
passages_1.jsonl filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
passages_2.jsonl filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
passages_3.jsonl filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Paragraphs
|
| 3 |
+
emoji: 😻
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.10.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
duplicated_from: NealCaren/smhunt
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import re
|
| 5 |
+
import pickle
|
| 6 |
+
from collections import OrderedDict
|
| 7 |
+
import io
|
| 8 |
+
|
| 9 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
from nltk.tokenize import sent_tokenize
|
| 13 |
+
import nltk
|
| 14 |
+
|
| 15 |
+
import gdown
|
| 16 |
+
import requests
|
| 17 |
+
from PIL import Image
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Trying to figure out some CSS stuff
|
| 21 |
+
|
| 22 |
+
st.markdown(
|
| 23 |
+
"""
|
| 24 |
+
<style>
|
| 25 |
+
.streamlit-expanderHeader {
|
| 26 |
+
font-size: medium;
|
| 27 |
+
}
|
| 28 |
+
</style>
|
| 29 |
+
""",
|
| 30 |
+
unsafe_allow_html=True,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
nltk.download('punkt')
|
| 35 |
+
|
| 36 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
+
|
| 38 |
+
import pandas as pd
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@st.cache
|
| 42 |
+
def load_embeddings():
|
| 43 |
+
url = "https://drive.google.com/uc?export=download&id=1lFYPqb_XY1RiX52E8OwiNd20Xhmqa20L"
|
| 44 |
+
output = "embeddings.npy"
|
| 45 |
+
gdown.download(url, output, quiet=False)
|
| 46 |
+
|
| 47 |
+
corpus_embeddings = np.load(output)
|
| 48 |
+
return corpus_embeddings
|
| 49 |
+
|
| 50 |
+
@st.cache
|
| 51 |
+
def load_data():
|
| 52 |
+
url = "https://drive.google.com/uc?export=download&id=1-2GctHHtRV-zChtibGZYLVgCgohnm-br"
|
| 53 |
+
output = "passages.jsonl"
|
| 54 |
+
gdown.download(url, output, quiet=False)
|
| 55 |
+
|
| 56 |
+
df = pd.read_json(output, lines=True)
|
| 57 |
+
|
| 58 |
+
df.reset_index(inplace=True, drop=True)
|
| 59 |
+
return df
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
st.title('Related Social Movement Articles')
|
| 63 |
+
|
| 64 |
+
st.write('This project is a work-in-progress that searches the abstracts of recently-published articles related to social movements and retrieves the most relevant articles.')
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
with st.spinner(text="Loading data..."):
|
| 68 |
+
df = load_data()
|
| 69 |
+
passages = df['text'].values
|
| 70 |
+
|
| 71 |
+
no_of_graphs=len(df)
|
| 72 |
+
no_of_articles = len(df['cite'].value_counts())
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
notes = f'''Notes:
|
| 76 |
+
* I have found three types of searches work best:
|
| 77 |
+
* Phrases or specific topics, such as "inequality in latin america", "race color skin tone measurement", "audit study experiment gender", or "logistic regression or linear probability model".
|
| 78 |
+
* Citations to well-known works, either using author year ("bourdieu 1984") or author idea ("Crenshaw intersectionality")
|
| 79 |
+
* Questions, like "What is a topic model?" or "How did Weber define bureaucracy?"
|
| 80 |
+
* The search expands beyond exact matching, so "asia social movements" may return paragraphs on Asian-Americans politics and South Korean labor unions.
|
| 81 |
+
* The first search can take up to 10 seconds as the files load. After that, it's quicker to respond.
|
| 82 |
+
* The most relevant paragraph to your search is returned first, along with up to four other related paragraphs from that article.
|
| 83 |
+
* The most relevant sentence within each paragraph, as determined by math, is displayed. Click on it to see the full paragraph.
|
| 84 |
+
* The results are not exhaustive, and seem to drift off even when you suspect there are more relevant articles :man-shrugging:.
|
| 85 |
+
* The dataset currently includes {no_of_graphs:,} paragraphs from {no_of_articles:,} published in the last five years in *Mobilization*, *Social Forces*, *Social Problems*, *Sociology of Race and Ethnicity*, *Gender and Society*, *Socius*, *JHSB*, *Annual Review of Sociology*, and the *American Sociological Review*.
|
| 86 |
+
* Behind the scenes, the semantic search uses [text embeddings](https://www.sbert.net) with a [retrieve & re-rank](https://colab.research.google.com/github/UKPLab/sentence-transformers/blob/master/examples/applications/retrieve_rerank/retrieve_rerank_simple_wikipedia.ipynb) process to find the best matches.
|
| 87 |
+
* Let [me](mailto:neal.caren@unc.edu) know what you think or it looks broken.
|
| 88 |
+
'''
|
| 89 |
+
|
| 90 |
+
# st.markdown(notes)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def sent_trans_load():
|
| 94 |
+
#We use the Bi-Encoder to encode all passages, so that we can use it with sematic search
|
| 95 |
+
bi_encoder = SentenceTransformer('multi-qa-MiniLM-L6-cos-v1')
|
| 96 |
+
bi_encoder.max_seq_length = 256 #Truncate long passages to 256 tokens, max 512
|
| 97 |
+
return bi_encoder
|
| 98 |
+
|
| 99 |
+
def sent_cross_load():
|
| 100 |
+
#We use the Bi-Encoder to encode all passages, so that we can use it with sematic search
|
| 101 |
+
cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
| 102 |
+
return cross_encoder
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
with st.spinner(text="Loading embeddings..."):
|
| 111 |
+
corpus_embeddings = load_embeddings()
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def search(query, top_k=20):
|
| 118 |
+
|
| 119 |
+
##### Sematic Search #####
|
| 120 |
+
# Encode the query using the bi-encoder and find potentially relevant passages
|
| 121 |
+
question_embedding = bi_encoder.encode(query, convert_to_tensor=True).to(device)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
hits = util.semantic_search(question_embedding, corpus_embeddings, top_k=top_k)
|
| 125 |
+
hits = hits[0] # Get the hits for the first query
|
| 126 |
+
##### Re-Ranking #####
|
| 127 |
+
# Now, score all retrieved passages with the cross_encoder
|
| 128 |
+
cross_inp = [[query, passages[hit['corpus_id']]] for hit in hits]
|
| 129 |
+
cross_scores = cross_encoder.predict(cross_inp)
|
| 130 |
+
|
| 131 |
+
# Sort results by the cross-encoder scores
|
| 132 |
+
for idx in range(len(cross_scores)):
|
| 133 |
+
hits[idx]['cross-score'] = cross_scores[idx]
|
| 134 |
+
|
| 135 |
+
# Output of top-5 hits from re-ranker
|
| 136 |
+
print("\n-------------------------\n")
|
| 137 |
+
print("Search Results")
|
| 138 |
+
hits = sorted(hits, key=lambda x: x['cross-score'], reverse=True)
|
| 139 |
+
|
| 140 |
+
hd = OrderedDict()
|
| 141 |
+
for hit in hits[0:30]:
|
| 142 |
+
|
| 143 |
+
row_id = hit['corpus_id']
|
| 144 |
+
cite = df.loc[row_id]['cite']
|
| 145 |
+
#graph = passages[row_id]
|
| 146 |
+
graph = df.loc[row_id]['text']
|
| 147 |
+
|
| 148 |
+
# Find best sentence
|
| 149 |
+
ab_sentences= [s for s in sent_tokenize(graph)]
|
| 150 |
+
cross_inp = [[query, s] for s in ab_sentences]
|
| 151 |
+
cross_scores = cross_encoder.predict(cross_inp)
|
| 152 |
+
thesis = pd.Series(cross_scores, ab_sentences).sort_values().index[-1]
|
| 153 |
+
graph = graph.replace(thesis, f'**{thesis}**')
|
| 154 |
+
|
| 155 |
+
if cite in hd:
|
| 156 |
+
|
| 157 |
+
hd[cite].append(graph)
|
| 158 |
+
else:
|
| 159 |
+
hd[cite] = [graph]
|
| 160 |
+
|
| 161 |
+
for cite, graphs in hd.items():
|
| 162 |
+
st.markdown(cite)
|
| 163 |
+
|
| 164 |
+
for graph in graphs[:5]:
|
| 165 |
+
# refind the Thesis
|
| 166 |
+
thesis = re.findall('\*\*(.*?)\*\*', graph)[0]
|
| 167 |
+
|
| 168 |
+
with st.expander(thesis):
|
| 169 |
+
st.write(f'> {graph}')
|
| 170 |
+
st.write('')
|
| 171 |
+
# print("\t{:.3f}\t{}".format(hit['cross-score'], passages[hit['corpus_id']].replace("\n", " ")))
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
search_query = st.text_area('Enter abstract or search phrase:')
|
| 176 |
+
if search_query!='':
|
| 177 |
+
with st.spinner(text="Searching and sorting results."):
|
| 178 |
+
|
| 179 |
+
placeholder = st.empty()
|
| 180 |
+
with placeholder.container():
|
| 181 |
+
st.image('https://www.dropbox.com/s/yndn6lkesjga9a6/emerac.png?raw=1')
|
| 182 |
+
bi_encoder = sent_trans_load()
|
| 183 |
+
cross_encoder = sent_cross_load()
|
| 184 |
+
search(search_query)
|
| 185 |
+
placeholder.empty()
|
emerac.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
sentence_transformers
|
| 2 |
+
torch
|
| 3 |
+
pandas
|
| 4 |
+
nltk
|
| 5 |
+
gdown
|