Skip to content
This repository was archived by the owner on Oct 22, 2023. It is now read-only.

Commit 75c093f

Browse files
committed
Add option for local testing.
1 parent 1f8920c commit 75c093f

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ var/
4848
# pytest
4949
*pytest_cache
5050

51+
# Credentials
5152
key_openai.txt
53+
54+
# Models saved locally
55+
models/

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,50 @@ To run REMO, you will need the following:
3636
2. Interact with the API using a REST client or web browser: `http://localhost:8000`
3737

3838

39+
## Models
40+
41+
### Embedding Model
42+
43+
REMO currently uses the
44+
Universal Sentence Encoder v5 for generating embeddings.
45+
46+
#### Loading from TensorFlow Hub
47+
48+
This is the default option.
49+
50+
When
51+
52+
```python
53+
ARE_YOU_TESTING__LOAD_MODEL_LOCAL = False
54+
```
55+
56+
in file `utils.py`, the model is loaded from TensorFlow Hub.
57+
58+
#### Loading from a local file
59+
60+
Downloading the model from TensorFlow Hub every time you need to spin up
61+
the microservice would be expensive and time-consuming.
62+
63+
1. Download the `.tar.gz` file from
64+
TensorFlow Hub: https://tfhub.dev/google/universal-sentence-encoder-large/5
65+
66+
![img.png](docs/images/embedding_local_1.png)
67+
68+
2. Extract the file to the folder
69+
```
70+
models/universal-sentence-encoder-large_5/
71+
```
72+
with
73+
```shell
74+
tar -xvzf universal-sentence-encoder-large_5.tar.gz
75+
```
76+
77+
3. Set
78+
```python
79+
ARE_YOU_TESTING__LOAD_MODEL_LOCAL = True
80+
```
81+
in file `utils.py`.
82+
3983
## API Endpoints
4084

4185
- **POST /add_message**: Add a new message to REMO. Speaker, timestamp, and content required.

docs/images/embedding_local_1.png

57.9 KB
Loading

remo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fastapi import FastAPI
22
import utils
33
import os
4+
import uvicorn
45

56
app = FastAPI()
67
root_folder = os.getcwd()
@@ -48,3 +49,7 @@ async def maintain_tree():
4849
utils.maintain_tree(root_folder)
4950

5051
return {"detail": "Tree maintenance completed"}
52+
53+
54+
if __name__ == '__main__':
55+
uvicorn.run(app, host='0.0.0.0', port=8000)

utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
from pathlib import Path
3+
24
import yaml
35
import shutil
46
import openai
@@ -7,12 +9,24 @@
79
from typing import Dict, Any, List
810
from sklearn.metrics.pairwise import cosine_similarity
911
from sklearn.cluster import KMeans
12+
import tensorflow as tf
1013
import tensorflow_hub as hub
1114

1215

13-
embedding_model = hub.load(
14-
"https://tfhub.dev/google/universal-sentence-encoder-large/5"
15-
)
16+
ARE_YOU_TESTING__LOAD_MODEL_LOCAL = True
17+
18+
ROOT_REPO_PATH = Path().parent.absolute()
19+
20+
if ARE_YOU_TESTING__LOAD_MODEL_LOCAL:
21+
embedding_model = tf.saved_model.load(
22+
ROOT_REPO_PATH
23+
/ "models/universal-sentence-encoder-large_5"
24+
)
25+
26+
else:
27+
embedding_model = hub.load(
28+
"https://tfhub.dev/google/universal-sentence-encoder-large/5"
29+
)
1630

1731

1832
def open_file(filepath):

0 commit comments

Comments
 (0)