|
| 1 | +''' |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2023 Ulster University (https://www.ulster.ac.uk). |
| 5 | +Project: Harmony (https://harmonydata.ac.uk) |
| 6 | +Maintainer: Thomas Wood (https://fastdatascience.com) |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +of this software and associated documentation files (the "Software"), to deal |
| 10 | +in the Software without restriction, including without limitation the rights |
| 11 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +copies of the Software, and to permit persons to whom the Software is |
| 13 | +furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | +The above copyright notice and this permission notice shall be included in all |
| 16 | +copies or substantial portions of the Software. |
| 17 | +
|
| 18 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +SOFTWARE. |
| 25 | +
|
| 26 | +''' |
| 27 | + |
| 28 | +import json |
| 29 | +import unittest |
| 30 | + |
| 31 | +import requests |
| 32 | + |
| 33 | +headers = { |
| 34 | + 'accept': 'application/json', |
| 35 | + 'Content-Type': 'application/json', |
| 36 | + 'Accept-Encoding': 'gzip, deflate, br', |
| 37 | +} |
| 38 | + |
| 39 | +json_data_to_match_gad_7 = { |
| 40 | + 'instruments': [ |
| 41 | + { |
| 42 | + 'file_id': 'fd60a9a64b1b4078a68f4bc06f20253c', |
| 43 | + 'instrument_id': '7829ba96f48e4848abd97884911b6795', |
| 44 | + 'instrument_name': 'GAD-7 English', |
| 45 | + 'file_name': 'GAD-7 EN.pdf', |
| 46 | + 'file_type': 'pdf', |
| 47 | + 'file_section': 'GAD-7 English', |
| 48 | + 'language': 'en', |
| 49 | + 'questions': [ |
| 50 | + { |
| 51 | + 'question_no': '1', |
| 52 | + 'question_intro': 'Over the last two weeks, how often have you been bothered by the following problems?', |
| 53 | + 'question_text': 'Feeling nervous, anxious, or on edge', |
| 54 | + 'options': [ |
| 55 | + 'Not at all', |
| 56 | + 'Several days', |
| 57 | + 'More than half the days', |
| 58 | + 'Nearly every day', |
| 59 | + ], |
| 60 | + 'source_page': 0, |
| 61 | + }, |
| 62 | + { |
| 63 | + 'question_no': '2', |
| 64 | + 'question_intro': 'Over the last two weeks, how often have you been bothered by the following problems?', |
| 65 | + 'question_text': 'Not being able to stop or control worrying', |
| 66 | + 'options': [ |
| 67 | + 'Not at all', |
| 68 | + 'Several days', |
| 69 | + 'More than half the days', |
| 70 | + 'Nearly every day', |
| 71 | + ], |
| 72 | + 'source_page': 0, |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + { |
| 77 | + 'file_id': 'fd60a9a64b1b4078a68f4bc06f20253c', |
| 78 | + 'instrument_id': '7829ba96f48e4848abd97884911b6795', |
| 79 | + 'instrument_name': 'GAD-7 Portuguese', |
| 80 | + 'file_name': 'GAD-7 PT.pdf', |
| 81 | + 'file_type': 'pdf', |
| 82 | + 'file_section': 'GAD-7 Portuguese', |
| 83 | + 'language': 'en', |
| 84 | + 'questions': [ |
| 85 | + { |
| 86 | + 'question_no': '1', |
| 87 | + 'question_intro': 'Durante as últimas 2 semanas, com que freqüência você foi incomodado/a pelos problemas abaixo?', |
| 88 | + 'question_text': 'Sentir-se nervoso/a, ansioso/a ou muito tenso/a', |
| 89 | + 'options': [ |
| 90 | + 'Nenhuma vez', |
| 91 | + 'Vários dias', |
| 92 | + 'Mais da metade dos dias', |
| 93 | + 'Quase todos os dias', |
| 94 | + ], |
| 95 | + 'source_page': 0, |
| 96 | + }, |
| 97 | + { |
| 98 | + 'question_no': '2', |
| 99 | + 'question_intro': 'Durante as últimas 2 semanas, com que freqüência você foi incomodado/a pelos problemas abaixo?', |
| 100 | + 'question_text': ' Não ser capaz de impedir ou de controlar as preocupações', |
| 101 | + 'options': [ |
| 102 | + 'Nenhuma vez', |
| 103 | + 'Vários dias', |
| 104 | + 'Mais da metade dos dias', |
| 105 | + 'Quase todos os dias', |
| 106 | + ], |
| 107 | + 'source_page': 0, |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + ], |
| 112 | + 'query': 'anxiety', |
| 113 | + 'parameters': { |
| 114 | + 'framework': 'huggingface', |
| 115 | + 'model': 'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2', |
| 116 | + }, |
| 117 | +} |
| 118 | + |
| 119 | +endpoint = 'https://harmonystagingtmp.azurewebsites.net/text/match' |
| 120 | + |
| 121 | + |
| 122 | +class TestDifferentFrameworks(unittest.TestCase): |
| 123 | + |
| 124 | + def test_huggingface(self): |
| 125 | + response = requests.post(endpoint, headers=headers, json=json_data_to_match_gad_7) |
| 126 | + |
| 127 | + print(response.json()["matches"][0][0]) |
| 128 | + |
| 129 | + self.assertLess(0.99, response.json()["matches"][0][0]) |
| 130 | + |
| 131 | + def test_google_nonexistent(self): |
| 132 | + payload_google = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 133 | + payload_google["parameters"]["framework"] = "google" |
| 134 | + |
| 135 | + response = requests.post(endpoint, headers=headers, json=payload_google) |
| 136 | + |
| 137 | + self.assertIn("model does not exist", response.text) |
| 138 | + |
| 139 | + def test_google_real(self): |
| 140 | + payload_google = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 141 | + payload_google["parameters"]["framework"] = "google" |
| 142 | + payload_google["parameters"]["model"] = "textembedding-gecko@003" |
| 143 | + |
| 144 | + response = requests.post(endpoint, headers=headers, json=payload_google) |
| 145 | + |
| 146 | + self.assertLess(0.99, response.json()["matches"][0][0]) |
| 147 | + |
| 148 | + def test_google_different_from_huggingface(self): |
| 149 | + response_huggingface = requests.post(endpoint, headers=headers, json=json_data_to_match_gad_7) |
| 150 | + |
| 151 | + payload_google = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 152 | + payload_google["parameters"]["framework"] = "google" |
| 153 | + payload_google["parameters"]["model"] = "textembedding-gecko@003" |
| 154 | + |
| 155 | + response_google = requests.post(endpoint, headers=headers, json=payload_google) |
| 156 | + |
| 157 | + abs_diff = abs(response_huggingface.json()["matches"][0][1] - response_google.json()["matches"][0][1]) |
| 158 | + |
| 159 | + print("Absolute difference", abs_diff) |
| 160 | + |
| 161 | + self.assertLess(0.1, abs_diff) |
| 162 | + |
| 163 | + def test_google_monolingual(self): |
| 164 | + payload_google = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 165 | + payload_google["parameters"]["framework"] = "google" |
| 166 | + payload_google["parameters"]["model"] = "textembedding-gecko@003" |
| 167 | + |
| 168 | + response = requests.post(endpoint, headers=headers, json=payload_google) |
| 169 | + |
| 170 | + self.assertGreater(0.92, response.json()["matches"][0][2]) |
| 171 | + |
| 172 | + def test_google_multilingual(self): |
| 173 | + payload_google = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 174 | + payload_google["parameters"]["framework"] = "google" |
| 175 | + payload_google["parameters"]["model"] = "textembedding-gecko-multilingual" |
| 176 | + |
| 177 | + response = requests.post(endpoint, headers=headers, json=payload_google) |
| 178 | + |
| 179 | + self.assertLess(0.92, response.json()["matches"][0][2]) |
| 180 | + |
| 181 | + def test_openai(self): |
| 182 | + payload_openai = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 183 | + payload_openai["parameters"]["framework"] = "openai" |
| 184 | + payload_openai["parameters"]["model"] = "text-embedding-3-large" |
| 185 | + |
| 186 | + response = requests.post(endpoint, headers=headers, json=payload_openai) |
| 187 | + |
| 188 | + self.assertLess(0.99, response.json()["matches"][0][0]) |
| 189 | + |
| 190 | + def test_azure_openai(self): |
| 191 | + payload_openai = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 192 | + payload_openai["parameters"]["framework"] = "azure_openai" |
| 193 | + payload_openai["parameters"]["model"] = "fds-text-embedding-3-large" |
| 194 | + |
| 195 | + response = requests.post(endpoint, headers=headers, json=payload_openai) |
| 196 | + |
| 197 | + self.assertLess(0.99, response.json()["matches"][0][0]) |
| 198 | + |
| 199 | + def test_azure_openai_same_as_vanilla_openai(self): |
| 200 | + payload_vanilla_openai = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 201 | + payload_vanilla_openai["parameters"]["framework"] = "openai" |
| 202 | + payload_vanilla_openai["parameters"]["model"] = "text-embedding-3-large" |
| 203 | + |
| 204 | + vanilla_openai_response = requests.post(endpoint, headers=headers, json=payload_vanilla_openai) |
| 205 | + |
| 206 | + payload_azure_openai = json.loads(json.dumps(json_data_to_match_gad_7)) |
| 207 | + payload_azure_openai["parameters"]["framework"] = "azure_openai" |
| 208 | + payload_azure_openai["parameters"]["model"] = "fds-text-embedding-3-large" |
| 209 | + |
| 210 | + azure_openai_response = requests.post(endpoint, headers=headers, json=payload_azure_openai) |
| 211 | + |
| 212 | + self.assertGreater(0.01, |
| 213 | + abs(vanilla_openai_response.json()["matches"][0][0] - |
| 214 | + azure_openai_response.json()["matches"][0][0])) |
| 215 | + self.assertGreater(0.01, |
| 216 | + abs(vanilla_openai_response.json()["matches"][0][1] - |
| 217 | + azure_openai_response.json()["matches"][0][1])) |
| 218 | + self.assertGreater(0.01, |
| 219 | + abs(vanilla_openai_response.json()["matches"][0][2] - |
| 220 | + azure_openai_response.json()["matches"][0][2])) |
| 221 | + |
| 222 | + |
| 223 | +if __name__ == '__main__': |
| 224 | + unittest.main() |
0 commit comments