Spaces:
Runtime error
Runtime error
Update app.py
Browse files91 unmodified lines929394959697989910010110210310410510610710810911011111211325 unmodified lines139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172337 unmodified lines510511512513514515516517518205 unmodified lines72472572672772872973091 unmodified linesfor tag in entity_tags:start = tag['start']end = tag['end']if end<len(sentence)-1:token = sentence[start:end] # Adjust for 0-based indexingelse:token = sentence[start:end+1]tag_name = f"[{tag['entity_group']}]"if tag['entity_group'] == 'PP':# Replace [PP] with [PAUSE]tag_name = '[PAUSE]'else:tag_name = ''tagged_tokens.append(f"{token}{tag_name}")# Return the sentence with [PAUSE] replacementreturn " ".join(tagged_tokens)def get_split_sentences(sentence, entity_tags):25 unmodified lines# If the sentence ends without a [PAUSE] token, add the final sentenceif current_sentence:split_sentences.append(" ".join(current_sentence))return split_sentences# def get_split_sentences(sentence, entity_tags):# split_sentences = []# # Initialize a variable to hold the current sentence# current_sentence = []# # Process the entity tags to split the sentence# for tag in entity_tags:# if tag['entity_group'] == 'PP':# if current_sentence:# print(current_sentence)# split_sentences.append(" ".join(current_sentence))# current_sentence = [] # Reset the current sentence# else:# start = tag['start']# end = tag['end']# token = sentence[start - 1:end] # Adjust for 0-based indexing# current_sentence.append(token)# # If the sentence ends without a [PAUSE] token, add the final sentence# if current_sentence:# split_sentences.append(" ".join(current_sentence))# return split_sentences337 unmodified lines)# Additional stylingax.set_title("Importance Score per Token", size=25)ax.set_xlabel("Token")ax.set_ylabel("Importance Value")ax.set_xticks(range(len(df["token"])))ax.set_xticklabels(df["token"], rotation=45)205 unmodified linesattribution_df1 = process_integrated_gradients(split_sentence, self._gpt2tokenizer, self.model)if i < len(self.split_sentences) - 1:# Add a row with [PAUSE] and value 0 at the endpause_row = pd.DataFrame({'token': '[PAUSE]', 'importance_value': 0},index=[len(attribution_df1)])attribution_df1 = pd.concat([attribution_df1,pause_row], ignore_index=True)dataframes_list.append(attribution_df1)91 unmodified lines929394959697989910010110210310410510610710810911011111211311411511611711825 unmodified lines144145146147148149150151152337 unmodified lines490491492493494495496497498205 unmodified lines70470570670770870971071191 unmodified linesfor tag in entity_tags:start = tag['start']end = tag['end']if end < len(sentence) - 1:token = sentence[start:end] # Adjust for 0-based indexingelse:token = sentence[start:end + 1]tag_name = '[PAUSE]' if tag['entity_group'] == 'PP' else ''tagged_tokens.append(f"{token}{tag_name}")print(tagged_tokens)# Return the sentence with [PAUSE] replacement and spaces preservedmodified_words = []for i, word in enumerate(tagged_tokens):if word.startswith("'s"):modified_words[-1] = modified_words[-1] + wordelse:modified_words.append(word)output = " ".join(modified_words)return outputdef get_split_sentences(sentence, entity_tags):25 unmodified lines# If the sentence ends without a [PAUSE] token, add the final sentenceif current_sentence:split_sentences.append("".join(current_sentence))return split_sentences337 unmodified lines)# Additional styling# ax.set_title("Importance Score per Token", size=25)# ax.set_xlabel("Token")# ax.set_ylabel("Importance Value")ax.set_xticks(range(len(df["token"])))ax.set_xticklabels(df["token"], rotation=45)205 unmodified linesattribution_df1 = process_integrated_gradients(split_sentence, self._gpt2tokenizer, self.model)if i < len(self.split_sentences) - 1:# Add a row with [PAUSE] and value 0 at the end# pause_row = pd.DataFrame({'token': '[PAUSE]', 'importance_value': 0},index=[len(attribution_df1)])pause_row = pd.DataFrame({'', '': 0},index=[len(attribution_df1)])attribution_df1 = pd.concat([attribution_df1,pause_row], ignore_index=True)dataframes_list.append(attribution_df1)
app.py+21-40