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