Modify API

This commit is contained in:
Ryszard Staruch 2024-01-12 10:20:16 +01:00
parent 99aacdc440
commit 07f2aa12e3

148
main.py
View File

@ -14,16 +14,10 @@ app = FastAPI()
class Data(BaseModel): class Data(BaseModel):
challenge: str metric: str
dev_expected: str = "" file_expected: str = ""
dev_out: str = "" file_out: str = ""
testA_expected: str = "" file_in: str = ""
testA_out: str = ""
testB_expected: str = ""
testB_out: str = ""
dev_in: str= ""
testA_in: str=""
testB_in: str=""
def preprocess_data(out, expected): def preprocess_data(out, expected):
@ -61,15 +55,11 @@ def get_answerability_f1(trues, preds):
return answerability_f1 return answerability_f1
def get_scores(trues, preds): def get_final_score(trues, preds):
scores = {} scores = {}
scores["Levenshtein"] = get_levenshtein_score(trues, preds) scores["Levenshtein"] = get_levenshtein_score(trues, preds)
scores["AnswerabilityF1"] = get_answerability_f1(trues, preds) scores["AnswerabilityF1"] = get_answerability_f1(trues, preds)
scores["Final"] = round((scores["Levenshtein"] + scores["AnswerabilityF1"]) / 2, 2) return round((scores["Levenshtein"] + scores["AnswerabilityF1"]) / 2, 2)
scores["Levenshtein"] = round(get_levenshtein_score(trues, preds), 2)
scores["AnswerabilityF1"] = round(get_answerability_f1(trues, preds), 2)
return scores
def get_emotion_recognition_scores(df_in, df_expected, df_predition): def get_emotion_recognition_scores(df_in, df_expected, df_predition):
@ -107,71 +97,79 @@ def get_emotion_recognition_scores(df_in, df_expected, df_predition):
@app.get("/") @app.get("/")
async def root(data: Data): async def root(data: Data):
challenge = data.challenge metric = data.metric
dev_expected = data.dev_expected file_expected = data.file_expected
dev_out = data.dev_out file_out = data.file_out
testA_expected = data.testA_expected file_in = data.file_in
testA_out = data.testA_out
testB_expected = data.testB_expected
testB_out = data.testB_out
dev_in = data.dev_in
testA_in = data.testA_in
testB_in = data.testB_in
if challenge == "QuestionAnswering": try:
results = {} if metric == "Final":
if len(dev_out) > 0: if len(file_out) > 0:
dev_out, dev_expected = preprocess_data(dev_out, dev_expected) file_out, file_expected = preprocess_data(file_out, file_expected)
if len(dev_out) != len(dev_expected): if len(file_out) != len(file_expected):
return ERROR_RESPONSE return ERROR_RESPONSE
results["dev-0"] = get_scores(dev_expected, dev_out)
if len(testA_out) > 0: return {
testA_out, testA_expected = preprocess_data(testA_out, testA_expected) "status": 200,
if len(testA_out) != len(testA_expected): "result": get_final_score(file_expected, file_out)
return ERROR_RESPONSE }
results["test-A"] = get_scores(testA_expected, testA_out) elif metric == "Levenshtein":
if len(file_out) > 0:
file_out, file_expected = preprocess_data(file_out, file_expected)
if len(file_out) != len(file_expected):
return ERROR_RESPONSE
if len(testB_out) > 0: return {
testB_out, testB_expected = preprocess_data(testB_out, testB_expected) "status": 200,
if len(testB_out) != len(testB_expected): "result": round(get_levenshtein_score(file_expected, file_out), 2)
return ERROR_RESPONSE }
results["test-B"] = get_scores(testB_expected, testB_out) elif metric == "AnswerabilityF1":
if len(file_out) > 0:
file_out, file_expected = preprocess_data(file_out, file_expected)
if len(file_out) != len(file_expected):
return ERROR_RESPONSE
if len(results) == 0: return {
return ERROR_RESPONSE "status": 200,
else: "result": round(get_answerability_f1(file_expected, file_out), 2)
return { }
"status": 200, elif metric == "FinalF1":
"results": results if len(file_out) > 0:
} df_in = pd.read_table(io.StringIO(file_in))
elif challenge == "EmotionRecognition": df_expected = pd.read_table(io.StringIO(file_expected))
results = {} df_predition = pd.read_table(io.StringIO(file_out))
if len(dev_out) > 0:
df_in = pd.read_table(io.StringIO(dev_in))
df_expected = pd.read_table(io.StringIO(dev_expected))
df_predition = pd.read_table(io.StringIO(dev_out))
results["dev-0"] = get_emotion_recognition_scores(df_in, df_expected, df_predition) results = get_emotion_recognition_scores(df_in, df_expected, df_predition)
if len(testA_out) > 0:
df_in = pd.read_table(io.StringIO(testA_in))
df_expected = pd.read_table(io.StringIO(testA_expected))
df_predition = pd.read_table(io.StringIO(testA_out))
results["test-A"] = get_emotion_recognition_scores(df_in, df_expected, df_predition) return {
if len(testB_out) > 0: "status": 200,
df_in = pd.read_table(io.StringIO(testB_in)) "result": results["FinalF1"]
df_expected = pd.read_table(io.StringIO(testB_expected)) }
df_predition = pd.read_table(io.StringIO(testB_out)) elif metric == "SentenceF1":
if len(file_out) > 0:
df_in = pd.read_table(io.StringIO(file_in))
df_expected = pd.read_table(io.StringIO(file_expected))
df_predition = pd.read_table(io.StringIO(file_out))
results["test-B"] = get_emotion_recognition_scores(df_in, df_expected, df_predition) results = get_emotion_recognition_scores(df_in, df_expected, df_predition)
if len(results) == 0: return {
return ERROR_RESPONSE "status": 200,
else: "result": results["SentenceF1"]
return { }
"status": 200, elif metric == "TextF1":
"results": results if len(file_out) > 0:
} df_in = pd.read_table(io.StringIO(file_in))
else: df_expected = pd.read_table(io.StringIO(file_expected))
df_predition = pd.read_table(io.StringIO(file_out))
results = get_emotion_recognition_scores(df_in, df_expected, df_predition)
return {
"status": 200,
"result": results["TextF1"]
}
except:
return ERROR_RESPONSE return ERROR_RESPONSE
return ERROR_RESPONSE