Spaces:
Sleeping
Sleeping
dbleek
commited on
Commit
·
71ee167
1
Parent(s):
eb6399e
removed unnecessary files
Browse files- milestone-2.py +26 -26
- milestone-3.py +72 -72
milestone-2.py
CHANGED
|
@@ -1,26 +1,26 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from transformers import (AutoTokenizer, TFAutoModelForSequenceClassification,
|
| 3 |
-
pipeline)
|
| 4 |
-
|
| 5 |
-
st.title("CS-GY-6613 Project Milestone 2")
|
| 6 |
-
model_choices = (
|
| 7 |
-
"distilbert-base-uncased-finetuned-sst-2-english",
|
| 8 |
-
"j-hartmann/emotion-english-distilroberta-base",
|
| 9 |
-
"joeddav/distilbert-base-uncased-go-emotions-student",
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
with st.form("Input Form"):
|
| 13 |
-
text = st.text_area("Write your text here:", "CS-GY-6613 is a great course!")
|
| 14 |
-
model_name = st.selectbox("Select a model:", model_choices)
|
| 15 |
-
submitted = st.form_submit_button("Submit")
|
| 16 |
-
|
| 17 |
-
if submitted:
|
| 18 |
-
model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
|
| 19 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
-
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
| 21 |
-
res = classifier(text)
|
| 22 |
-
label = res[0]["label"].upper()
|
| 23 |
-
score = res[0]["score"]
|
| 24 |
-
st.markdown(
|
| 25 |
-
f"This text was classified as **{label}** with a confidence score of **{score}**."
|
| 26 |
-
)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import (AutoTokenizer, TFAutoModelForSequenceClassification,
|
| 3 |
+
pipeline)
|
| 4 |
+
|
| 5 |
+
st.title("CS-GY-6613 Project Milestone 2")
|
| 6 |
+
model_choices = (
|
| 7 |
+
"distilbert-base-uncased-finetuned-sst-2-english",
|
| 8 |
+
"j-hartmann/emotion-english-distilroberta-base",
|
| 9 |
+
"joeddav/distilbert-base-uncased-go-emotions-student",
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
with st.form("Input Form"):
|
| 13 |
+
text = st.text_area("Write your text here:", "CS-GY-6613 is a great course!")
|
| 14 |
+
model_name = st.selectbox("Select a model:", model_choices)
|
| 15 |
+
submitted = st.form_submit_button("Submit")
|
| 16 |
+
|
| 17 |
+
if submitted:
|
| 18 |
+
model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
+
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
| 21 |
+
res = classifier(text)
|
| 22 |
+
label = res[0]["label"].upper()
|
| 23 |
+
score = res[0]["score"]
|
| 24 |
+
st.markdown(
|
| 25 |
+
f"This text was classified as **{label}** with a confidence score of **{score}**."
|
| 26 |
+
)
|
milestone-3.py
CHANGED
|
@@ -1,72 +1,72 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
from datasets import load_dataset
|
| 4 |
-
from transformers import AutoTokenizer
|
| 5 |
-
from transformers import AutoModelForSequenceClassification
|
| 6 |
-
from transformers import pipeline
|
| 7 |
-
|
| 8 |
-
# Load HUPD dataset
|
| 9 |
-
dataset_dict = load_dataset('HUPD/hupd',
|
| 10 |
-
name='sample',
|
| 11 |
-
data_files="https://huggingface.co/datasets/HUPD/hupd/blob/main/hupd_metadata_2022-02-22.feather",
|
| 12 |
-
icpr_label=None,
|
| 13 |
-
train_filing_start_date='2016-01-01',
|
| 14 |
-
train_filing_end_date='2016-01-21',
|
| 15 |
-
val_filing_start_date='2016-01-22',
|
| 16 |
-
val_filing_end_date='2016-01-31',
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
# Process data
|
| 20 |
-
filtered_dataset = dataset_dict['validation'].filter(lambda e: e['decision'] == 'ACCEPTED' or e['decision'] == 'REJECTED')
|
| 21 |
-
dataset = filtered_dataset.shuffle(seed=42).select(range(20))
|
| 22 |
-
dataset = dataset.sort("patent_number")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Create pipeline using model trainned on Colab
|
| 26 |
-
model = torch.load("/workspaces/cs-gy-6613-project/patent_classification(1).pt", map_location=torch.device('cpu'))
|
| 27 |
-
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 28 |
-
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def load_patent():
|
| 33 |
-
selected_application = dataset.select([applications[st.session_state.id]])
|
| 34 |
-
st.session_state.abstract = selected_application['abstract'][0]
|
| 35 |
-
st.session_state.claims = selected_application['claims'][0]
|
| 36 |
-
st.session_state.title = selected_application['title'][0]
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
st.title("CS-GY-6613 Project Milestone 3")
|
| 41 |
-
|
| 42 |
-
# List patent numbers for select box
|
| 43 |
-
applications = {}
|
| 44 |
-
for ds_index, example in enumerate(dataset):
|
| 45 |
-
applications.update({example['patent_number']: ds_index })
|
| 46 |
-
st.selectbox("Select a patent application:", applications, on_change=load_patent, key="id")
|
| 47 |
-
|
| 48 |
-
# Application title displayed for additional context only, not used with model
|
| 49 |
-
st.text_area("Title", key="title", value=dataset[0]['title'], height=50)
|
| 50 |
-
|
| 51 |
-
# Classifier input form
|
| 52 |
-
with st.form('Input Form'):
|
| 53 |
-
abstract = st.text_area("Abstract", key="abstract", value=dataset[0]['abstract'], height=200)
|
| 54 |
-
claims = st.text_area("Claims", key="claims", value=dataset[0]['abstract'], height=200)
|
| 55 |
-
submitted = st.form_submit_button("Get Patentability Score")
|
| 56 |
-
|
| 57 |
-
if submitted:
|
| 58 |
-
selected_application = dataset.select([applications[st.session_state.id]])
|
| 59 |
-
res = classifier(abstract, claims)
|
| 60 |
-
if res[0]["label"] == 'LABEL_0':
|
| 61 |
-
pred = "ACCEPTED"
|
| 62 |
-
elif res[0]["label"] == 'LABEL_1':
|
| 63 |
-
pred = "REJECTED"
|
| 64 |
-
score = res[0]["score"]
|
| 65 |
-
label = selected_application['decision'][0]
|
| 66 |
-
result = st.markdown("This text was classified as **{}** with a confidence score of **{}**.".format(pred, score))
|
| 67 |
-
check = st.markdown("Actual Label: **{}**.".format(label))
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
+
from transformers import AutoModelForSequenceClassification
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
# Load HUPD dataset
|
| 9 |
+
dataset_dict = load_dataset('HUPD/hupd',
|
| 10 |
+
name='sample',
|
| 11 |
+
data_files="https://huggingface.co/datasets/HUPD/hupd/blob/main/hupd_metadata_2022-02-22.feather",
|
| 12 |
+
icpr_label=None,
|
| 13 |
+
train_filing_start_date='2016-01-01',
|
| 14 |
+
train_filing_end_date='2016-01-21',
|
| 15 |
+
val_filing_start_date='2016-01-22',
|
| 16 |
+
val_filing_end_date='2016-01-31',
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Process data
|
| 20 |
+
filtered_dataset = dataset_dict['validation'].filter(lambda e: e['decision'] == 'ACCEPTED' or e['decision'] == 'REJECTED')
|
| 21 |
+
dataset = filtered_dataset.shuffle(seed=42).select(range(20))
|
| 22 |
+
dataset = dataset.sort("patent_number")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Create pipeline using model trainned on Colab
|
| 26 |
+
model = torch.load("/workspaces/cs-gy-6613-project/patent_classification(1).pt", map_location=torch.device('cpu'))
|
| 27 |
+
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 28 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def load_patent():
|
| 33 |
+
selected_application = dataset.select([applications[st.session_state.id]])
|
| 34 |
+
st.session_state.abstract = selected_application['abstract'][0]
|
| 35 |
+
st.session_state.claims = selected_application['claims'][0]
|
| 36 |
+
st.session_state.title = selected_application['title'][0]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
st.title("CS-GY-6613 Project Milestone 3")
|
| 41 |
+
|
| 42 |
+
# List patent numbers for select box
|
| 43 |
+
applications = {}
|
| 44 |
+
for ds_index, example in enumerate(dataset):
|
| 45 |
+
applications.update({example['patent_number']: ds_index })
|
| 46 |
+
st.selectbox("Select a patent application:", applications, on_change=load_patent, key="id")
|
| 47 |
+
|
| 48 |
+
# Application title displayed for additional context only, not used with model
|
| 49 |
+
st.text_area("Title", key="title", value=dataset[0]['title'], height=50)
|
| 50 |
+
|
| 51 |
+
# Classifier input form
|
| 52 |
+
with st.form('Input Form'):
|
| 53 |
+
abstract = st.text_area("Abstract", key="abstract", value=dataset[0]['abstract'], height=200)
|
| 54 |
+
claims = st.text_area("Claims", key="claims", value=dataset[0]['abstract'], height=200)
|
| 55 |
+
submitted = st.form_submit_button("Get Patentability Score")
|
| 56 |
+
|
| 57 |
+
if submitted:
|
| 58 |
+
selected_application = dataset.select([applications[st.session_state.id]])
|
| 59 |
+
res = classifier(abstract, claims)
|
| 60 |
+
if res[0]["label"] == 'LABEL_0':
|
| 61 |
+
pred = "ACCEPTED"
|
| 62 |
+
elif res[0]["label"] == 'LABEL_1':
|
| 63 |
+
pred = "REJECTED"
|
| 64 |
+
score = res[0]["score"]
|
| 65 |
+
label = selected_application['decision'][0]
|
| 66 |
+
result = st.markdown("This text was classified as **{}** with a confidence score of **{}**.".format(pred, score))
|
| 67 |
+
check = st.markdown("Actual Label: **{}**.".format(label))
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|