RJ3vans commited on
Commit
7ae0c6e
·
verified ·
1 Parent(s): d0cc61d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -31
app.py CHANGED
@@ -674,16 +674,16 @@ def tag_SSCCV_span(sentence):
674
  tokens = SSCCVspanTaggingTokenizer.tokenize(
675
  SSCCVspanTaggingTokenizer.decode(SSCCVspanTaggingTokenizer.encode(sentence))
676
  )
677
- inputs = SSCCVspanTaggingTokenizer.encode(sentence, return_tensors="pt")
678
-
679
- outputs = SSCCVspanTaggingModel(inputs)[0]
680
  predictions = torch.argmax(outputs, dim=2)
681
 
682
  SSCCV_span_tagged_tokens = [
683
  (token, SSCCVspan_label_list[prediction])
684
  for token, prediction in zip(tokens, predictions[0].tolist())
685
  ]
686
-
687
  SSCCV_span_tagged_predictions = [p for t, p in SSCCV_span_tagged_tokens]
688
 
689
  aligned_tokens = []
@@ -742,27 +742,42 @@ def tag_SSMN_span(sentence):
742
  if re.search(r"\[\:\]\s*$", sentence):
743
  sentence = sentence + " blah blah blah."
744
 
745
- tokens = SSMNspanTaggingTokenizer.tokenize(
746
- SSMNspanTaggingTokenizer.decode(SSMNspanTaggingTokenizer.encode(sentence))
 
 
 
 
 
747
  )
748
- inputs = SSMNspanTaggingTokenizer.encode(sentence, return_tensors="pt")
749
- if device != "cpu":
750
- inputs = inputs.to(device)
 
751
 
752
  with torch.no_grad():
753
- outputs = SSMNspanTaggingModel(inputs)[0]
754
  predictions = torch.argmax(outputs, dim=2)
755
 
 
 
 
 
 
 
 
756
  ssmn_span_tagged_predictions = [
757
- SSMNspan_label_list[prediction]
758
- for prediction in predictions[0].tolist()
759
  ]
760
 
 
761
  aligned_tokens, aligned_predictions = [], []
762
  aligned_pos_tags, aligned_lemmas = [], []
763
 
764
  sc_sentence = (
765
- clean_for_spaCy(sentence) if "[" in sentence and "]" in sentence else sentence
 
 
766
  )
767
  doc = nlp(sc_sentence)
768
  spacy_pos_tags = [t.tag_ for t in doc]
@@ -783,6 +798,7 @@ def tag_SSMN_span(sentence):
783
  return sign_cleaning(
784
  aligned_tokens, aligned_pos_tags, aligned_lemmas, aligned_predictions
785
  )
 
786
  ###############################################################################
787
 
788
 
@@ -974,31 +990,45 @@ def tag_CMN1_span(sentence):
974
 
975
  ###############################################################################
976
  def tag_CLN_span(sentence):
977
- if re.search("\[\:\]\s*$", sentence):
978
  sentence = sentence + " blah blah blah."
979
 
980
- tokens = CLNspanTaggingTokenizer.tokenize(
981
- CLNspanTaggingTokenizer.decode(CLNspanTaggingTokenizer.encode(sentence))
 
 
 
 
982
  )
983
- inputs = CLNspanTaggingTokenizer.encode(sentence, return_tensors="pt")
984
 
985
- outputs = CLNspanTaggingModel(inputs)[0]
 
 
 
 
 
986
  predictions = torch.argmax(outputs, dim=2)
987
 
988
- CLN_span_tagged_tokens = [
989
- (token, CLNspan_label_list[prediction])
990
- for token, prediction in zip(tokens, predictions[0].tolist())
991
- ]
 
992
 
993
- CLN_span_tagged_predictions = [p for t, p in CLN_span_tagged_tokens]
 
 
994
 
 
995
  aligned_tokens = []
996
  aligned_predictions = []
997
  aligned_pos_tags = []
998
  aligned_lemmas = []
999
 
1000
  sc_sentence = (
1001
- clean_for_spaCy(sentence) if "[" in sentence and "]" in sentence else sentence
 
 
1002
  )
1003
  doc = nlp(sc_sentence)
1004
  spacy_pos_tags = [t.tag_ for t in doc]
@@ -1009,10 +1039,11 @@ def tag_CLN_span(sentence):
1009
 
1010
  for i in range(len(spacy_tokens)):
1011
  for j in a2b[i]:
1012
- aligned_tokens.append(spacy_tokens[i])
1013
- aligned_pos_tags.append(spacy_pos_tags[i])
1014
- aligned_lemmas.append(spacy_lemmas[i])
1015
- aligned_predictions.append(CLN_span_tagged_predictions[j])
 
1016
  break
1017
 
1018
  clean_tokens, clean_pos_tags, clean_lemmas, clean_predictions = sign_cleaning(
@@ -1029,9 +1060,7 @@ def tag_CLN_span(sentence):
1029
  clean_pos_tags,
1030
  clean_lemmas,
1031
  clean_predictions,
1032
- ) # added return
1033
-
1034
-
1035
  ###############################################################################
1036
 
1037
  ###############################################################################
 
674
  tokens = SSCCVspanTaggingTokenizer.tokenize(
675
  SSCCVspanTaggingTokenizer.decode(SSCCVspanTaggingTokenizer.encode(sentence))
676
  )
677
+ inputs = SSCCVspanTaggingTokenizer.encode(sentence, return_tensors="pt").to(device)
678
+ with torch.no_grad():
679
+ outputs = SSCCVspanTaggingModel(inputs)[0]
680
  predictions = torch.argmax(outputs, dim=2)
681
 
682
  SSCCV_span_tagged_tokens = [
683
  (token, SSCCVspan_label_list[prediction])
684
  for token, prediction in zip(tokens, predictions[0].tolist())
685
  ]
686
+
687
  SSCCV_span_tagged_predictions = [p for t, p in SSCCV_span_tagged_tokens]
688
 
689
  aligned_tokens = []
 
742
  if re.search(r"\[\:\]\s*$", sentence):
743
  sentence = sentence + " blah blah blah."
744
 
745
+ # --- BERT side ---
746
+ # Prefer the full call so you get input_ids + attention_mask
747
+ encoded = SSMNspanTaggingTokenizer(
748
+ sentence,
749
+ return_tensors="pt",
750
+ truncation=True,
751
+ max_length=512,
752
  )
753
+
754
+ # Put every tensor on the *same* device as the model
755
+ model_device = next(SSMNspanTaggingModel.parameters()).device
756
+ encoded = {k: v.to(model_device) for k, v in encoded.items()}
757
 
758
  with torch.no_grad():
759
+ outputs = SSMNspanTaggingModel(**encoded)[0]
760
  predictions = torch.argmax(outputs, dim=2)
761
 
762
+ # Wordpiece tokens for alignment (same string as above)
763
+ tokens = SSMNspanTaggingTokenizer.tokenize(
764
+ SSMNspanTaggingTokenizer.decode(
765
+ SSMNspanTaggingTokenizer.encode(sentence)
766
+ )
767
+ )
768
+
769
  ssmn_span_tagged_predictions = [
770
+ SSMNspan_label_list[p] for p in predictions[0].tolist()
 
771
  ]
772
 
773
+ # --- spaCy side (CPU is fine) ---
774
  aligned_tokens, aligned_predictions = [], []
775
  aligned_pos_tags, aligned_lemmas = [], []
776
 
777
  sc_sentence = (
778
+ clean_for_spaCy(sentence)
779
+ if "[" in sentence and "]" in sentence
780
+ else sentence
781
  )
782
  doc = nlp(sc_sentence)
783
  spacy_pos_tags = [t.tag_ for t in doc]
 
798
  return sign_cleaning(
799
  aligned_tokens, aligned_pos_tags, aligned_lemmas, aligned_predictions
800
  )
801
+
802
  ###############################################################################
803
 
804
 
 
990
 
991
  ###############################################################################
992
  def tag_CLN_span(sentence):
993
+ if re.search(r"\[\:\]\s*$", sentence):
994
  sentence = sentence + " blah blah blah."
995
 
996
+ # --- BERT side ---
997
+ encoded = CLNspanTaggingTokenizer(
998
+ sentence,
999
+ return_tensors="pt",
1000
+ truncation=True,
1001
+ max_length=512,
1002
  )
 
1003
 
1004
+ # Same device as the model (works on Windows CPU and HF GPU/ZeroGPU)
1005
+ model_device = next(CLNspanTaggingModel.parameters()).device
1006
+ encoded = {k: v.to(model_device) for k, v in encoded.items()}
1007
+
1008
+ with torch.no_grad():
1009
+ outputs = CLNspanTaggingModel(**encoded)[0]
1010
  predictions = torch.argmax(outputs, dim=2)
1011
 
1012
+ tokens = CLNspanTaggingTokenizer.tokenize(
1013
+ CLNspanTaggingTokenizer.decode(
1014
+ CLNspanTaggingTokenizer.encode(sentence)
1015
+ )
1016
+ )
1017
 
1018
+ CLN_span_tagged_predictions = [
1019
+ CLNspan_label_list[p] for p in predictions[0].tolist()
1020
+ ]
1021
 
1022
+ # --- spaCy side (CPU) ---
1023
  aligned_tokens = []
1024
  aligned_predictions = []
1025
  aligned_pos_tags = []
1026
  aligned_lemmas = []
1027
 
1028
  sc_sentence = (
1029
+ clean_for_spaCy(sentence)
1030
+ if "[" in sentence and "]" in sentence
1031
+ else sentence
1032
  )
1033
  doc = nlp(sc_sentence)
1034
  spacy_pos_tags = [t.tag_ for t in doc]
 
1039
 
1040
  for i in range(len(spacy_tokens)):
1041
  for j in a2b[i]:
1042
+ if j < len(CLN_span_tagged_predictions):
1043
+ aligned_tokens.append(spacy_tokens[i])
1044
+ aligned_pos_tags.append(spacy_pos_tags[i])
1045
+ aligned_lemmas.append(spacy_lemmas[i])
1046
+ aligned_predictions.append(CLN_span_tagged_predictions[j])
1047
  break
1048
 
1049
  clean_tokens, clean_pos_tags, clean_lemmas, clean_predictions = sign_cleaning(
 
1060
  clean_pos_tags,
1061
  clean_lemmas,
1062
  clean_predictions,
1063
+ )
 
 
1064
  ###############################################################################
1065
 
1066
  ###############################################################################