cetinca commited on
Commit
f86237e
2 Parent(s): 5eceba8 4120946

Merge branch 'dev-test' into 'main'

Browse files

Update test file

See merge request tangibleai/community/mathtext-fastapi!2

app.py CHANGED
@@ -1,5 +1,5 @@
1
  """FastAPI endpoint
2
- To run locally use 'uvicorn modules.app:app --host localhost --port 7860'
3
  """
4
 
5
  from fastapi import FastAPI, Request
@@ -76,4 +76,5 @@ async def evaluate_user_message_with_nlu_api(request: Request):
76
  prepare_message_data_for_logging(message_data)
77
 
78
  int_data_dict = {'type': 'integer', 'data': int_api_resp}
79
- return JSONResponse(content=int_data_dict)
 
 
1
  """FastAPI endpoint
2
+ To run locally use 'uvicorn app:app --host localhost --port 7860'
3
  """
4
 
5
  from fastapi import FastAPI, Request
 
76
  prepare_message_data_for_logging(message_data)
77
 
78
  int_data_dict = {'type': 'integer', 'data': int_api_resp}
79
+ return JSONResponse(content=int_data_dict)
80
+
data/master_test_text2int.csv ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ input,output
2
+ fourteen,14
3
+ forteen,14
4
+ one thousand four hundred ninety two,1492
5
+ one thousand ninety two,1092
6
+ Fourteen Hundred Ninety-Two,1492
7
+ Fourteen Hundred,1400
8
+ Ninety nine,99
9
+ fifteen thousand five hundred-sixty,15560
10
+ three hundred fifty,350
11
+ one nine eight five,1985
12
+ nineteen eighty-five,1985
13
+ oh one,1
14
+ six oh 1,601
15
+ sex,6
16
+ six,6
17
+ eight oh,80
18
+ eighty,80
19
+ ate,8
20
+ double eight,88
21
+ eight three seven five three O nine,8375309
22
+ eight three seven five three oh nine,8375309
23
+ eight three seven five three zero nine,8375309
24
+ eight three seven five three oh ni-ee-ine,8375309
25
+ two eight,28
26
+ seven oh eleven,7011
27
+ seven elevens,77
28
+ seven eleven,711
29
+ ninety nine oh five,9905
30
+ seven 0 seven 0 seven 0 seven,7070707
31
+ 123 hundred,123000
32
+ 5 o 5,505
33
+ 15 o 5,1505
34
+ 15-o 5,1505
35
+ 15 o-5,1505
36
+ 911-thousand,911000
37
+ twenty-two twenty-two,2222
38
+ twenty-two twenty-twos,484
39
+ four eighty four,484
40
+ four eighties,320
41
+ four eighties and nine nineties,1130
42
+ ninety nine hundred and seventy seven,9977
43
+ seven thousands,7000
44
+ 2 hundreds,200
45
+ 99 thousands and one,99001
46
+ "forty-five thousand, seven hundred and nine",45709
47
+ eighty eight hundred eighty,8880
48
+ a hundred hundred,10000
49
+ a hundred thousand,100000
50
+ a hundred million,100000000
51
+ nineteen ninety nine,1999
52
+ forteen twenty seven,1427
53
+ seventeen-thousand and seventy two,17072
54
+ two hundred and nine,209
55
+ two thousand ten,2010
56
+ two thousand and ten,2010
57
+ twelve million,12000000
58
+ 8 billion,8000000000
59
+ twenty ten,2010
60
+ thirty-two hundred,3200
61
+ nine,9
62
+ forty two,42
63
+ 1 2 three,123
64
+ fourtean,14
65
+ one tousand four hundred ninty two,1492
66
+ Furteen Hundrd Ninety-Too,1492
67
+ forrteen,14
68
+ sevnteen-thosand and seventy two,17072
69
+ ninety nine hundred ad seventy seven,9977
70
+ seven thusands,7000
71
+ 2 hunreds,200
72
+ 99 tousands and one,99001
73
+ eighty ate hundred eighty,8880
74
+ fourteen Hundred,1400
75
+ 8 Bilion,8000000000
76
+ one million three thousand one,1003001
77
+ four million nine thousand seven,4009007
78
+ two million five hundred thousand,2500000
79
+ two tousand ten,2010
80
+ two thousand teen,2010
81
+ tvelve milion,12000000
82
+ tventy ten,2010
83
+ tirty-twoo hunred,3200
84
+ sevn thoosands,7000
85
+ five,5
86
+ ten,10
87
+ one two three and ten,12310
88
+ ONE MILLion three hunded and fiv,1000305
89
+ "50,500 and six",50506
90
+ one_million_and_five,1000005
data/test_data_text2int.csv DELETED
@@ -1,43 +0,0 @@
1
- input,output
2
- fourteen,14
3
- one thousand four hundred ninety two,1492
4
- Fourteen Hundred Ninety-Two,1492
5
- forteen,14
6
- seventeen-thousand and seventy two,17072
7
- two hundred and nine,209
8
- # Following are the ones that the current text2int.py fails on,-
9
- # ninety nine hundred and seventy seven,9977
10
- # seven thousands,7000
11
- # 2 hundreds,200
12
- # 99 thousands and one,9901
13
- # "forty-five thousand, seven hundred and nine",45709
14
- # eighty eight hundred eighty,8880
15
- # a hundred hundred,10000
16
- # a hundred thousand,100000
17
- # a hundred million,100000000
18
- # nineteen ninety nine,1999
19
- # forteen twenty seven,1427
20
- # one nine eight five,1985
21
- # nineteen eighty-five,1985
22
- # six,6
23
- # eighty,80
24
- # eight three seven five three zero nine,8375308
25
- # oh one,1
26
- # six oh 1,601
27
- # sex,6
28
- # eight oh,80
29
- # ate,8
30
- # double eight,88
31
- # eight three seven five three O nine,8375308
32
- # eight three seven five three oh nine,8375308
33
- # eight three seven five three 0 nine,8375308
34
- # eight three seven five three oh ni-ee-ine,8375308
35
- # two thousand ten,2010
36
- # two thousand and ten,2010
37
- # twelve million,12000000
38
- # 8 billion,8000000000
39
- # twenty ten,2010
40
- # thirty-two hundred,3200
41
- # nine,9
42
- # forty two,42
43
- # 1 2 three,123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tests/test_text2int.py CHANGED
@@ -1,10 +1,11 @@
1
  import unittest
2
 
 
3
  from fastapi.testclient import TestClient
4
 
5
  from app import app
6
 
7
- TEST_DATA_FILE = "data/test_data_text2int.csv"
8
 
9
  client = TestClient(app)
10
 
@@ -12,49 +13,39 @@ client = TestClient(app)
12
  class TestStringMethods(unittest.TestCase):
13
 
14
  def setUp(self):
 
15
  self.client = TestClient(app)
16
 
17
- def test_1(self):
 
 
 
 
 
 
 
 
 
 
 
18
  response = self.client.post("/text2int",
19
  json={"content": "fourteen"}
20
  )
21
  self.assertEqual(response.status_code, 200)
22
- self.assertEqual(str(response.json()["message"]), "14")
23
 
24
- def test_2(self):
25
- response = self.client.post("/text2int",
26
- json={"content": "one thousand four hundred ninety two"}
27
- )
28
- self.assertEqual(response.status_code, 200)
29
- self.assertEqual(str(response.json()["message"]), "1492")
30
 
31
- def test_3(self):
32
- response = self.client.post("/text2int",
33
- json={"content": "Fourteen Hundred Ninety-Two"}
34
- )
35
- self.assertEqual(response.status_code, 200)
36
- self.assertEqual(str(response.json()["message"]), "1492")
 
37
 
38
- def test_4(self):
39
- response = client.post("/text2int",
40
- json={"content": "forteen"}
41
- )
42
- self.assertEqual(response.status_code, 200)
43
- self.assertEqual(str(response.json()["message"]), "14")
44
-
45
- def test_5(self):
46
- response = client.post("/text2int",
47
- json={"content": "seventeen-thousand and seventy two"}
48
- )
49
- self.assertEqual(response.status_code, 200)
50
- self.assertEqual(str(response.json()["message"]), "17072")
51
-
52
- def test_6(self):
53
- response = client.post("/text2int",
54
- json={"content": "two hundred and nine"}
55
- )
56
- self.assertEqual(response.status_code, 200)
57
- self.assertEqual(str(response.json()["message"]), "209")
58
 
59
 
60
  if __name__ == '__main__':
 
1
  import unittest
2
 
3
+ import pandas as pd
4
  from fastapi.testclient import TestClient
5
 
6
  from app import app
7
 
8
+ TEST_DATA_FILE = "data/master_test_text2int.csv"
9
 
10
  client = TestClient(app)
11
 
 
13
  class TestStringMethods(unittest.TestCase):
14
 
15
  def setUp(self):
16
+ """Creates a fastapi test client"""
17
  self.client = TestClient(app)
18
 
19
+ def get_response_text2int(self, text):
20
+ """Makes a post request to the endpoint"""
21
+ r = None
22
+ try:
23
+ r = self.client.post("/text2int", json={"content": text}) \
24
+ .json().get("message")
25
+ except:
26
+ pass
27
+ return r
28
+
29
+ def test_endpoint_text2int(self):
30
+ """Tests if endpoint is working"""
31
  response = self.client.post("/text2int",
32
  json={"content": "fourteen"}
33
  )
34
  self.assertEqual(response.status_code, 200)
 
35
 
36
+ def test_acc_score_text2int(self):
37
+ """Calculates accuracy score for endpoint"""
38
+ df = pd.read_csv(TEST_DATA_FILE)
 
 
 
39
 
40
+ df["text2int"] = df["input"].apply(func=self.get_response_text2int)
41
+ df["score"] = df[["output", "text2int"]].apply(
42
+ lambda row: row[0] == row[1],
43
+ axis=1
44
+ )
45
+ df.to_csv("data/text2int_results.csv", index=False)
46
+ acc_score = df["score"].mean().__round__(2)
47
 
48
+ self.assertGreaterEqual(acc_score, 0.8, f"Accuracy score: '{acc_score}'. Value is too low!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
 
51
  if __name__ == '__main__':