import numpy from transformers import TokenClassificationPipeline class BellmanFordTokenClassificationPipeline(TokenClassificationPipeline): def __init__(self,**kwargs): super().__init__(**kwargs) x=self.model.config.label2id y=[k for k in x if k.startswith("B-") or not (k.startswith("I-") or k.endswith("|root") or k.find("|l-")>0 or k.find("|r-")>0)] self.transition=numpy.full((len(x),len(x)),numpy.nan) for k,v in x.items(): for j in ["I-"+k[2:]] if k.startswith("B-") else [k]+y if k.startswith("I-") else y: self.transition[v,x[j]]=0 def check_model_type(self,supported_models): pass def postprocess(self,model_outputs,**kwargs): if "logits" not in model_outputs: return self.postprocess(model_outputs[0],**kwargs) m=model_outputs["logits"][0].numpy() e=numpy.exp(m-numpy.max(m,axis=-1,keepdims=True)) z=e/e.sum(axis=-1,keepdims=True) for i in range(m.shape[0]-1,0,-1): m[i-1]+=numpy.nanmax(m[i]+self.transition,axis=1) k=[numpy.nanargmax(m[0]+self.transition[0])] for i in range(1,m.shape[0]): k.append(numpy.nanargmax(m[i]+self.transition[k[-1]])) w=[{"entity":self.model.config.id2label[j],"start":s,"end":e,"score":z[i,j]} for i,((s,e),j) in enumerate(zip(model_outputs["offset_mapping"][0].tolist(),k)) if s0: self.left_arc[v]=0 elif k.find("|r-")>0: self.right_arc[v]=0 def postprocess(self,model_outputs,**kwargs): import torch if "logits" not in model_outputs: return self.postprocess(model_outputs[0],**kwargs) m=model_outputs["logits"][0].numpy() for i in range(m.shape[0]-1,0,-1): m[i-1]+=numpy.nanmax(m[i]+self.transition,axis=1) k=[numpy.nanargmax(m[0]+self.transition[0])] for i in range(1,m.shape[0]): k.append(numpy.nanargmax(m[i]+self.transition[k[-1]])) w=[{"entity":self.model.config.id2label[j],"start":s,"end":e} for i,((s,e),j) in enumerate(zip(model_outputs["offset_mapping"][0].tolist(),k)) if s1: k,h=z[numpy.nanargmax(m[z,z])],numpy.nanmin(m)-numpy.nanmax(m) m[:,z]+=[[0 if j in z and (i!=j or i==k) else h for i in z] for j in range(m.shape[0])] h=self.chu_liu_edmonds(m) q=[self.model.config.id2label[p[j,i]].split("|") for i,j in enumerate(h)] t=model_outputs["sentence"].replace("\n"," ") u="# text = "+t+"\n" for i,j in enumerate(d): u+="\t".join([str(i+1),j,"_",q[i][0],"_","_" if len(q[i])<3 else "|".join(q[i][1:-1]),str(0 if h[i]==i else h[i]+1),"root" if q[i][-1]=="root" else q[i][-1][2:],"_","_" if i+1