-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (103 loc) · 3.93 KB
/
app.py
File metadata and controls
141 lines (103 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import pkg_resources
import uvicorn
from fastapi import FastAPI, Request
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from src import model_locator
from src.api.service import CiRAService, CiRAServiceImpl
cira_version = pkg_resources.require("cira")[0].version
description = """The CiRA API wraps the functionality of the Causality in Requirements Artifacts initiative and bundles it in one easy-to-use API.
## Functionality
At the time, the following features are supported:
* **classify** a single, natural language sentence as either causal or non-causal
* **label** each token in a sentence regarding its role within the causal relationship
* generate a cause-effect **graph** from a labeled sentence
* convert a cause-effect graph into a **test suite** containing the minimal number of test cases ensuring full requirements coverage
"""
tags_metadata = [
{
"name": "classify",
"description": "Classification of a single, natural language sentence as either causal or non-causal"
}, {
"name": "label",
"description": "Label each token in a sentence regarding its role within the causal relationship"
}, {
"name": "graph",
"description": "Generate a cause-effect graph from a labeled sentence"
}, {
"name": "testsuite",
"description": "Convert a cause-effect graph into a test suite"
},
]
app = FastAPI(
title="Causality in Requirements Artifacts - Pipeline",
version=cira_version,
description=description,
contact={
"name": "Julian Frattini",
"url": "http://www.cira.bth.se/",
"email": "julian.frattini@bth.se"
},
openapi_tags=tags_metadata
)
PREFIX = "/api"
# add CORS middleware allowing all requests from the same localhost
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
cira: CiRAService = None
def setup_cira():
global cira
print(f'Classification model path: {model_locator.CLASSIFICATION}')
print(f'Labeling model path: {model_locator.LABELING}')
# generate a CiRA service implementation
cira = CiRAServiceImpl(model_locator.CLASSIFICATION, model_locator.LABELING)
class SentenceRequest(BaseModel):
sentence: str
language: str = "en"
labels: list[dict] = []
graph: dict = None
class ClassificationResponse(BaseModel):
causal: bool
confidence: float
class LabelingResponse(BaseModel):
labels: list[dict]
class GraphResponse(BaseModel):
graph: dict
class TestsuiteResponse(BaseModel):
suite: dict
@app.get(PREFIX + "/")
def root(req: Request):
url_list = [
{"path": route.path, "name": route.name} for route in req.app.routes
]
return url_list
@app.get(PREFIX + "/health")
def health():
return {
"status": "up",
"cira-version": cira_version
}
@app.put(PREFIX + '/classify', response_model=ClassificationResponse, tags=['classify'])
async def create_classification(req: SentenceRequest):
causal, confidence = cira.classify(req.sentence)
return ClassificationResponse(causal=causal, confidence=confidence)
@app.put(PREFIX + '/label', response_model=LabelingResponse, tags=['label'])
async def create_labels(req: SentenceRequest):
labels = cira.sentence_to_labels(sentence=req.sentence)
return LabelingResponse(labels=labels)
@app.put(PREFIX + '/graph', response_model=GraphResponse, tags=['graph'])
async def create_graph(req: SentenceRequest):
graph = cira.sentence_to_graph(sentence=req.sentence, labels=req.labels)
return GraphResponse(graph=graph)
@app.put(PREFIX + '/testsuite', response_model=TestsuiteResponse, tags=['testsuite'])
async def create_testsuite(req: SentenceRequest):
testsuite = cira.graph_to_test(graph=req.graph, sentence=req.sentence)
return TestsuiteResponse(suite=testsuite)
if __name__ == '__main__':
setup_cira()
uvicorn.run(app, host='0.0.0.0')