Spaces:
Sleeping
Sleeping
File size: 4,712 Bytes
152844c 86a60b5 152844c 86a60b5 152844c 86a60b5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import pandas as pd
import streamlit as st
from infer import USPPPMModel, USPPPMDataset
import torch
@st.cache_resource
def load_model():
model = USPPPMModel('microsoft/deberta-v3-small')
model.load_state_dict(torch.load('model_weights.pth', map_location=torch.device('cpu')))
model.eval()
ds = USPPPMDataset(model.tokenizer, 133)
return model, ds
def infer(anchor, target, title):
model, ds = load_model()
d = {
'anchor': anchor,
'target': target,
'title': title,
'label': 0
}
x = ds[d][0]
with torch.no_grad():
y = model(x)
return y.cpu().numpy()[0][0]
@st.cache_data
def get_context():
df = pd.read_csv('./fold-0-train.csv')
l = list(set(list(df['title'].values)))
return l
st.set_page_config(
page_title="PatentMatch",
page_icon="🧊",
layout="centered",
initial_sidebar_state="expanded",
)
# fix sidebar
st.markdown("""
<style>
.css-vk3wp9 {
background-color: rgb(255 255 255);
}
.css-18l0hbk {
padding: 0.34rem 1.2rem !important;
margin: 0.125rem 2rem;
}
.css-nziaof {
padding: 0.34rem 1.2rem !important;
margin: 0.125rem 2rem;
background-color: rgb(181 197 227 / 18%) !important;
}
</style>
""", unsafe_allow_html=True
)
hide_st_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
header {visibility: hidden;}
</style>
"""
st.markdown(hide_st_style, unsafe_allow_html=True)
def app():
st.title("PatentMatch: Patent Semantic Similarity Matcher")
#st.markdown("[](https://wandb.ai/<username>/<project_name>?workspace=user-<username>)")
st.markdown(
"""This project is focused on developing a Transformer based NLP model to match phrases
in U.S. patents based on their semantic similarity within a specific
technical domain context. The trained model achieved Pearson correlation coefficient score of 0.745.
[[Source Code]](https://github.com/dataraptor/PatentMatch)
"""
)
st.markdown('---')
# st.selectbox("Select from example",
# [
# "Example 1",
# "Example 2",
# ])
row1_col1, row1_col2, row1_col3 = st.columns(
[0.5, 0.4, 0.4]
)
# with row1_col1:
# frequency = st.selectbox("Section",
# [
# "A: Human Necessities",
# "B: Operations and Transport",
# "C: Chemistry and Metallurgy",
# "D: Textiles",
# "E: Fixed Constructions",
# "F: Mechanical Engineering",
# "G: Physics",
# "H: Electricity",
# "Y: Emerging Cross-Sectional Technologies",
# ])
# with row1_col2:
# class_box = st.selectbox("Class",
# [
# "21",
# "14",
# "23",
# ])
with row1_col1:
l = get_context()
context = st.selectbox("Context", l, l.index('basic electric elements'))
with row1_col2:
anchor = st.text_input("Anchor", "deflect light")
with row1_col3:
target = st.text_input("Target", "bending moment")
if st.button("Predict Scores", type="primary"):
with st.spinner("Predicting scores..."):
score = infer(anchor, target, context)
ss = st.success("Scores predicted successfully!")
score += 2.0
fmt = "{:<.3f}".format(score)
st.subheader(f"Similarity Score: {fmt}")
app()
# Display a footer with links and credits
st.markdown("---")
st.markdown("Built by [Shamim Ahamed](https://www.shamimahamed.com/). Data provided by [Kaggle](https://www.kaggle.com/competitions/us-patent-phrase-to-phrase-matching)")
#st.markdown("Data provided by [The Feedback Prize - ELLIPSE Corpus Scoring Challenge on Kaggle](https://www.kaggle.com/c/feedbackprize-ellipse-corpus-scoring-challenge)")
|