Roland Ding commited on
Commit
38511f7
·
1 Parent(s): ecc12d7

11.10.26.76

Browse files

updated the backend_update.ipynb for including lab content
added the article.py for the article classes and instruction
for the instruction classes
Changes to be committed:
modified: article.py
modified: backend_update.ipynb
modified: instruction.py

Files changed (3) hide show
  1. article.py +28 -9
  2. backend_update.ipynb +0 -0
  3. instruction.py +78 -22
article.py CHANGED
@@ -2,13 +2,18 @@ from typing import Optional, List, Dict
2
  from enum import Enum
3
 
4
  from pydantic import BaseModel, Field
5
- # from langchain. import
6
-
7
 
8
  class Article(BaseModel):
9
- id: int
10
- title: str
11
- content: str
 
 
 
 
 
 
12
 
13
  class Table(BaseModel):
14
  title: str = Field(None, alias="title")
@@ -18,13 +23,28 @@ class Table(BaseModel):
18
  class Summary(BaseModel):
19
  tables: Optional[List[Table]] = Field(None, alias="tables")
20
 
 
 
 
 
 
21
  class Assessment(Enum):
22
  overview = "overview"
23
  clinical = "clinical"
24
- radiological = "radiological"
25
  safety = "safety"
26
  other = "other"
27
 
 
 
 
 
 
 
 
 
 
 
28
  class Parameter(BaseModel):
29
  name: str
30
  value: str
@@ -45,10 +65,9 @@ class ClinicalStudy(Article):
45
  tables: Optional[List[Table]] = Field(None, alias="tables")
46
  acceptance_year: Optional[int] = Field(None, alias="acceptance_year")
47
  acceptance_month: Optional[int] = Field(None, alias="acceptance_month")
48
- sections: Optional[Dict[str]] = Field(None, alias="sections")
49
- sectionCollection: Optional[Dict[str]] = Field(None, alias="sectionCollection")
50
 
51
- class DeviceIMF(Article):
52
  tables: Optional[List[Table]] = Field(None, alias="tables")
53
  sections: Optional[List[str]] = Field(None, alias="sections")
54
  sectionCollection: Optional[List[str]] = Field(None, alias="sectionCollection")
 
2
  from enum import Enum
3
 
4
  from pydantic import BaseModel, Field
5
+ from utility import read_pdf
 
6
 
7
  class Article(BaseModel):
8
+ file: str|object = Field(None, alias="file")
9
+ title: Optional[str] = Field(None, alias="title")
10
+ content: Optional[str]
11
+
12
+ def read_article(self):
13
+ if isinstance(self.file,str):
14
+ self.content = read_pdf(self.file)
15
+ else:
16
+ self.content = self.file.read()
17
 
18
  class Table(BaseModel):
19
  title: str = Field(None, alias="title")
 
23
  class Summary(BaseModel):
24
  tables: Optional[List[Table]] = Field(None, alias="tables")
25
 
26
+ class Region(Enum):
27
+ spine = "Spine"
28
+ extremity = "Extremity"
29
+ all = "All"
30
+
31
  class Assessment(Enum):
32
  overview = "overview"
33
  clinical = "clinical"
34
+ radiologic = "radiologic"
35
  safety = "safety"
36
  other = "other"
37
 
38
+ class Section(Enum):
39
+ abstract = "Abstract"
40
+ introduction = "Introduction"
41
+ methods = "Material and Methods"
42
+ results = "Results"
43
+ discussion = "Discussion"
44
+ conclusion = "Conclusion"
45
+ references = "References"
46
+ ifu = "IFU"
47
+
48
  class Parameter(BaseModel):
49
  name: str
50
  value: str
 
65
  tables: Optional[List[Table]] = Field(None, alias="tables")
66
  acceptance_year: Optional[int] = Field(None, alias="acceptance_year")
67
  acceptance_month: Optional[int] = Field(None, alias="acceptance_month")
68
+ sections: Optional[Dict[Section,str]] = Field(None, alias="sections")
 
69
 
70
+ class DeviceIFU(Article):
71
  tables: Optional[List[Table]] = Field(None, alias="tables")
72
  sections: Optional[List[str]] = Field(None, alias="sections")
73
  sectionCollection: Optional[List[str]] = Field(None, alias="sectionCollection")
backend_update.ipynb CHANGED
The diff for this file is too large to render. See raw diff
 
instruction.py CHANGED
@@ -1,26 +1,82 @@
1
-
2
-
3
  from pydantic import BaseModel, Field
4
  from typing import Optional, List, Dict
5
  from enum import Enum
6
 
7
- class Term(BaseModel):
8
- id: int
9
- terms: List[str] = Field(None, alias="terms")
10
-
11
- class PostParser(BaseModel):
12
- id: int
13
- name: str
14
- terms: List[str] = Field(None, alias="terms")
15
-
16
- class PostParser(BaseModel):
17
- id: int
18
- name: str
19
- terms: List[str] = Field(None, alias="terms")
20
-
21
- class Instruction(BaseModel):
22
- name: str
23
-
24
- class InstructionClassifier(BaseModel):
25
- terms: List[Term] = Field(None, alias="terms")
26
- instruction: Instruction = Field(None, alias="instruction")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from article import Assessment, Region, Section
 
2
  from pydantic import BaseModel, Field
3
  from typing import Optional, List, Dict
4
  from enum import Enum
5
 
6
+ from langchain.prompts.chat import ChatPromptTemplate
7
+ from langchain.chat_models import ChatOpenAI
8
+ from langchain.schema.runnable.base import RunnableSequence
9
+
10
+ value_map = {
11
+ "overview": Assessment.overview,
12
+ "clinical": Assessment.clinical,
13
+ "radiologic": Assessment.radiologic,
14
+ "safety": Assessment.safety,
15
+ "other": Assessment.other,
16
+ "spine": Region.spine,
17
+ "extremity": Region.extremity,
18
+ "all": Region.all,
19
+ "abstract": Section.abstract,
20
+ "introduction": Section.introduction,
21
+ "material and methods": Section.methods,
22
+ "results": Section.results,
23
+ "discussion": Section.discussion,
24
+ "conclusion": Section.conclusion,
25
+ "references": Section.references,
26
+ }
27
+
28
+ class Parser(BaseModel):
29
+ term: str = Field("{term}", description="the pattern to be replaced with the output_term.")
30
+ region: Region = Field("cervical", alias="region")
31
+ assessment: Assessment = Field(None, alias="assessment")
32
+ replacement: str = Field(None, description="the term from last the input to be replaced with.")
33
+
34
+ def parse(self,content):
35
+ content.replace(self.term,self.replacement)
36
+ return content
37
+
38
+ # class Path(BaseModel): # maybe too early to generalize this. Lets walk through a normal one for the insturction classifier first.
39
+ # name: str
40
+ # inputs: List[str] | str = Field([""], alias="inputs")
41
+ # variables: Dict[str,str] = Field({"term":""}, alias="variables")
42
+ # assessment: Assessment = Field(None, description="The clinical trail assessment steps")
43
+ # chain: RunnableSequence = Field([""], description="The nodes in the path to be executed.")
44
+
45
+ # def run(self,article):
46
+ # content = " ".join([article[s] for s in self.inputs])
47
+ # self.varialbes.update(content=content)
48
+ # article[self.name] = self.chain.invoke(self.variables)
49
+
50
+ # async def arun(self,article):
51
+ # pass
52
+
53
+ class ChainClassifier(BaseModel):
54
+ terms: List[str] = Field([""], alias="terms")
55
+ region: Region = Field(Region.spine, alias="region")
56
+ sections: Section = Field(None, alias="sections")
57
+ path: List[object] = Field(..., description="the automation path to be executed.")
58
+ chain: RunnableSequence = Field(None, description="the nodes in the path to be executed.")
59
+
60
+ def classify(self,article):
61
+ content = "".join([article[s] for s in self.sections])
62
+ if not self.validate_region(article):
63
+ return
64
+ if all([t in content for t in self.terms]):
65
+ return self.instruction
66
+
67
+ def validate_region(self,article):
68
+ if self.region == Region.all:
69
+ return True
70
+ else:
71
+ return self.region == article.region
72
+
73
+ def parse_obj_mapped(self,obj,key_map):
74
+ for k,v in key_map.items():
75
+ if k in obj:
76
+ obj[v] = obj.pop(k)
77
+ self.parse_obj(obj)
78
+
79
+ def run(self,article):
80
+ self.chain.invoke(article)
81
+
82
+