Spaces:
Sleeping
Sleeping
Jonathan Card commited on
Commit ·
5dc04ad
1
Parent(s): f49d5d2
Some initial hacking at the data service
Browse filesI had some free time and wanted the data service to at least use the out-of-the-box, uncleaned up dataset to ensure the API was properly defined.
- data_service.py +22 -2
- test/test_app.py +4 -4
- test/test_data_service.py +7 -2
data_service.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from contextlib import contextmanager
|
|
|
|
| 2 |
|
| 3 |
class DataService(object):
|
| 4 |
@contextmanager
|
|
@@ -17,8 +18,27 @@ class DataService(object):
|
|
| 17 |
raise Exception("Should not use the base class")
|
| 18 |
|
| 19 |
class DefaultDataService(DataService):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def close(self):
|
| 21 |
-
raise Exception("Not implemented")
|
|
|
|
| 22 |
|
|
|
|
| 23 |
def get_patients(self) -> list[str]:
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from contextlib import contextmanager
|
| 2 |
+
import pandas as pd
|
| 3 |
|
| 4 |
class DataService(object):
|
| 5 |
@contextmanager
|
|
|
|
| 18 |
raise Exception("Should not use the base class")
|
| 19 |
|
| 20 |
class DefaultDataService(DataService):
|
| 21 |
+
|
| 22 |
+
def __init__(self):
|
| 23 |
+
self._dataset_url = "https://huggingface.co/datasets/patjs/patient1/raw/main/patient_encounters1_notes.csv"
|
| 24 |
+
self._initialized = False
|
| 25 |
+
|
| 26 |
+
def initialize(func):
|
| 27 |
+
def wrapper(self):
|
| 28 |
+
if not self._initialized:
|
| 29 |
+
self._df = pd.read_csv(self._dataset_url)
|
| 30 |
+
self._initialized = True
|
| 31 |
+
return func(self)
|
| 32 |
+
|
| 33 |
+
return wrapper
|
| 34 |
+
|
| 35 |
def close(self):
|
| 36 |
+
#raise Exception("Not implemented")
|
| 37 |
+
pass
|
| 38 |
|
| 39 |
+
@initialize
|
| 40 |
def get_patients(self) -> list[str]:
|
| 41 |
+
col = self._df["FIRST"]
|
| 42 |
+
u = col.unique()
|
| 43 |
+
l = u.tolist()
|
| 44 |
+
return l
|
test/test_app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
import unittest
|
| 2 |
-
from unittest.mock import MagicMock
|
| 3 |
from data_service import DataService
|
| 4 |
from llm_service import LLMService
|
| 5 |
from g34_final_project import App
|
| 6 |
|
| 7 |
class TestApp(unittest.TestCase):
|
| 8 |
def test_app(self):
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
data_service.get_patients = MagicMock(return_value=["kareem", "abdul", "jabbar"])
|
| 12 |
-
data_service.close = MagicMock(return_value=None)
|
| 13 |
|
| 14 |
llm_service.get_summary = MagicMock(return_value="This is a summary")
|
| 15 |
llm_service.answer_query = MagicMock(return_value="This is the query answer")
|
|
|
|
| 1 |
import unittest
|
| 2 |
+
from unittest.mock import MagicMock, create_autospec
|
| 3 |
from data_service import DataService
|
| 4 |
from llm_service import LLMService
|
| 5 |
from g34_final_project import App
|
| 6 |
|
| 7 |
class TestApp(unittest.TestCase):
|
| 8 |
def test_app(self):
|
| 9 |
+
mock_tmp = MagicMock()
|
| 10 |
+
mock_tmp.return_value.__enter__.return_value = create_autospec(DataService)
|
| 11 |
+
with mock_tmp() as data_service, LLMService().with_key("").build() as llm_service:
|
| 12 |
data_service.get_patients = MagicMock(return_value=["kareem", "abdul", "jabbar"])
|
|
|
|
| 13 |
|
| 14 |
llm_service.get_summary = MagicMock(return_value="This is a summary")
|
| 15 |
llm_service.answer_query = MagicMock(return_value="This is the query answer")
|
test/test_data_service.py
CHANGED
|
@@ -1,7 +1,12 @@
|
|
| 1 |
import unittest
|
| 2 |
|
|
|
|
|
|
|
| 3 |
class TestDataService(unittest.TestCase):
|
| 4 |
def test_get_patients(self):
|
| 5 |
# TODO: I'm just stubbing out what I think the API could look like.
|
| 6 |
-
data_service = DataService().to("").withCreds("").build()
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import unittest
|
| 2 |
|
| 3 |
+
from data_service import DataService
|
| 4 |
+
|
| 5 |
class TestDataService(unittest.TestCase):
|
| 6 |
def test_get_patients(self):
|
| 7 |
# TODO: I'm just stubbing out what I think the API could look like.
|
| 8 |
+
#data_service = DataService().to("").withCreds("").build()
|
| 9 |
+
with DataService().build() as data_service:
|
| 10 |
+
patients = data_service.get_patients()
|
| 11 |
+
self.assertTrue(isinstance(patients, list))
|
| 12 |
+
self.assertTrue(len(patients) > 0)
|