Spaces:
Build error
Build error
Commit
•
b1a499f
0
Parent(s):
Duplicate from thompsgj/mathtext-wormhole
Browse files- .gitattributes +34 -0
- .gitignore +163 -0
- .gitlab-ci.yml +16 -0
- Dockerfile +22 -0
- README.md +12 -0
- app.py +331 -0
- docs/transitions_math_quiz_example.ipynb +368 -0
- mathtext_fastapi/__init__.py +0 -0
- mathtext_fastapi/conversation_manager.py +437 -0
- mathtext_fastapi/curriculum_mapper.py +183 -0
- mathtext_fastapi/data/Rori_Framework_v1.xlsx +0 -0
- mathtext_fastapi/data/curriculum_framework_for_tests.xlsx +0 -0
- mathtext_fastapi/data/intent_classification_model.joblib +3 -0
- mathtext_fastapi/data/labeled_data.csv +144 -0
- mathtext_fastapi/data/text2int_results.csv +119 -0
- mathtext_fastapi/global_state_manager.py +23 -0
- mathtext_fastapi/intent_classification.py +52 -0
- mathtext_fastapi/logging.py +103 -0
- mathtext_fastapi/math_quiz_fsm.py +58 -0
- mathtext_fastapi/math_subtraction_fsm.py +58 -0
- mathtext_fastapi/nlu.py +174 -0
- mathtext_fastapi/v2_conversation_manager.py +266 -0
- pyproject.toml +46 -0
- requirements.txt +18 -0
- scripts/__init__.py +0 -0
- scripts/api_scaling.py +96 -0
- scripts/api_scaling.sh +83 -0
- scripts/build.sh +8 -0
- scripts/make_request.py +207 -0
- scripts/make_request.sh +46 -0
- scripts/plot_calls.py +116 -0
- scripts/quiz/__init__.py +0 -0
- scripts/quiz/data.csv +56 -0
- static/styles.css +8 -0
- templates/home.html +35 -0
- tests/__init__.py +0 -0
- tests/test_text2int.py +57 -0
- users.json +1 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
.idea/
|
161 |
+
|
162 |
+
*history_sentiment*
|
163 |
+
*history_text2int*
|
.gitlab-ci.yml
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Official Python language image.
|
2 |
+
test_py38:
|
3 |
+
image: python:3.8
|
4 |
+
before_script:
|
5 |
+
- python -v
|
6 |
+
- pip install -r requirements.txt
|
7 |
+
script:
|
8 |
+
- pytest --verbose
|
9 |
+
|
10 |
+
test_py39:
|
11 |
+
image: python:3.9
|
12 |
+
before_script:
|
13 |
+
- python -v
|
14 |
+
- pip install -r requirements.txt
|
15 |
+
script:
|
16 |
+
- pytest --verbose
|
Dockerfile
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# https://huggingface.co/docs/hub/spaces-sdks-docker-first-demo
|
2 |
+
|
3 |
+
FROM python:3.9
|
4 |
+
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
COPY ./requirements.txt /code/requirements.txt
|
8 |
+
|
9 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
10 |
+
|
11 |
+
RUN useradd -m -u 1000 user
|
12 |
+
|
13 |
+
USER user
|
14 |
+
|
15 |
+
ENV HOME=/home/user \
|
16 |
+
PATH=/home/user/.local/bin:$PATH
|
17 |
+
|
18 |
+
WORKDIR $HOME/app
|
19 |
+
|
20 |
+
COPY --chown=user . $HOME/app
|
21 |
+
|
22 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Mathtext Wormhole
|
3 |
+
emoji: 🐨
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
+
sdk: docker
|
7 |
+
pinned: false
|
8 |
+
license: agpl-3.0
|
9 |
+
duplicated_from: thompsgj/mathtext-wormhole
|
10 |
+
---
|
11 |
+
|
12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,331 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""FastAPI endpoint
|
2 |
+
To run locally use 'uvicorn app:app --host localhost --port 7860'
|
3 |
+
or
|
4 |
+
`python -m uvicorn app:app --reload --host localhost --port 7860`
|
5 |
+
"""
|
6 |
+
import ast
|
7 |
+
import mathactive.microlessons.num_one as num_one_quiz
|
8 |
+
from fastapi import FastAPI, Request
|
9 |
+
from fastapi.responses import JSONResponse
|
10 |
+
from fastapi.staticfiles import StaticFiles
|
11 |
+
from fastapi.templating import Jinja2Templates
|
12 |
+
from mathtext.sentiment import sentiment
|
13 |
+
from mathtext.text2int import text2int
|
14 |
+
from pydantic import BaseModel
|
15 |
+
|
16 |
+
from mathtext_fastapi.logging import prepare_message_data_for_logging
|
17 |
+
from mathtext_fastapi.conversation_manager import manage_conversation_response
|
18 |
+
from mathtext_fastapi.v2_conversation_manager import manage_conversation_response
|
19 |
+
from mathtext_fastapi.nlu import evaluate_message_with_nlu
|
20 |
+
from mathtext_fastapi.nlu import run_intent_classification
|
21 |
+
|
22 |
+
app = FastAPI()
|
23 |
+
|
24 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
25 |
+
|
26 |
+
templates = Jinja2Templates(directory="templates")
|
27 |
+
|
28 |
+
|
29 |
+
class Text(BaseModel):
|
30 |
+
content: str = ""
|
31 |
+
|
32 |
+
|
33 |
+
@app.get("/")
|
34 |
+
def home(request: Request):
|
35 |
+
return templates.TemplateResponse("home.html", {"request": request})
|
36 |
+
|
37 |
+
|
38 |
+
@app.post("/hello")
|
39 |
+
def hello(content: Text = None):
|
40 |
+
content = {"message": f"Hello {content.content}!"}
|
41 |
+
return JSONResponse(content=content)
|
42 |
+
|
43 |
+
|
44 |
+
@app.post("/sentiment-analysis")
|
45 |
+
def sentiment_analysis_ep(content: Text = None):
|
46 |
+
ml_response = sentiment(content.content)
|
47 |
+
content = {"message": ml_response}
|
48 |
+
return JSONResponse(content=content)
|
49 |
+
|
50 |
+
|
51 |
+
@app.post("/text2int")
|
52 |
+
def text2int_ep(content: Text = None):
|
53 |
+
ml_response = text2int(content.content)
|
54 |
+
content = {"message": ml_response}
|
55 |
+
return JSONResponse(content=content)
|
56 |
+
|
57 |
+
|
58 |
+
@app.post("/v1/manager")
|
59 |
+
async def programmatic_message_manager(request: Request):
|
60 |
+
"""
|
61 |
+
Calls conversation management function to determine the next state
|
62 |
+
|
63 |
+
Input
|
64 |
+
request.body: dict - message data for the most recent user response
|
65 |
+
{
|
66 |
+
"author_id": "+47897891",
|
67 |
+
"contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09",
|
68 |
+
"author_type": "OWNER",
|
69 |
+
"message_body": "a test message",
|
70 |
+
"message_direction": "inbound",
|
71 |
+
"message_id": "ABJAK64jlk3-agjkl2QHFAFH",
|
72 |
+
"message_inserted_at": "2022-07-05T04:00:34.03352Z",
|
73 |
+
"message_updated_at": "2023-02-14T03:54:19.342950Z",
|
74 |
+
}
|
75 |
+
|
76 |
+
Output
|
77 |
+
context: dict - the information for the current state
|
78 |
+
{
|
79 |
+
"user": "47897891",
|
80 |
+
"state": "welcome-message-state",
|
81 |
+
"bot_message": "Welcome to Rori!",
|
82 |
+
"user_message": "",
|
83 |
+
"type": "ask"
|
84 |
+
}
|
85 |
+
"""
|
86 |
+
data_dict = await request.json()
|
87 |
+
context = manage_conversation_response(data_dict)
|
88 |
+
return JSONResponse(context)
|
89 |
+
|
90 |
+
|
91 |
+
@app.post("/v2/manager")
|
92 |
+
async def programmatic_message_manager(request: Request):
|
93 |
+
"""
|
94 |
+
Calls conversation management function to determine the next state
|
95 |
+
|
96 |
+
Input
|
97 |
+
request.body: dict - message data for the most recent user response
|
98 |
+
{
|
99 |
+
"author_id": "+47897891",
|
100 |
+
"contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09",
|
101 |
+
"author_type": "OWNER",
|
102 |
+
"message_body": "a test message",
|
103 |
+
"message_direction": "inbound",
|
104 |
+
"message_id": "ABJAK64jlk3-agjkl2QHFAFH",
|
105 |
+
"message_inserted_at": "2022-07-05T04:00:34.03352Z",
|
106 |
+
"message_updated_at": "2023-02-14T03:54:19.342950Z",
|
107 |
+
}
|
108 |
+
|
109 |
+
Output
|
110 |
+
context: dict - the information for the current state
|
111 |
+
{
|
112 |
+
"user": "47897891",
|
113 |
+
"state": "welcome-message-state",
|
114 |
+
"bot_message": "Welcome to Rori!",
|
115 |
+
"user_message": "",
|
116 |
+
"type": "ask"
|
117 |
+
}
|
118 |
+
"""
|
119 |
+
data_dict = await request.json()
|
120 |
+
context = manage_conversation_response(data_dict)
|
121 |
+
return JSONResponse(context)
|
122 |
+
|
123 |
+
|
124 |
+
@app.post("/intent-classification")
|
125 |
+
def intent_classification_ep(content: Text = None):
|
126 |
+
ml_response = run_intent_classification(content.content)
|
127 |
+
content = {"message": ml_response}
|
128 |
+
return JSONResponse(content=content)
|
129 |
+
|
130 |
+
|
131 |
+
@app.post("/nlu")
|
132 |
+
async def evaluate_user_message_with_nlu_api(request: Request):
|
133 |
+
""" Calls nlu evaluation and returns the nlu_response
|
134 |
+
|
135 |
+
Input
|
136 |
+
- request.body: json - message data for the most recent user response
|
137 |
+
|
138 |
+
Output
|
139 |
+
- int_data_dict or sent_data_dict: dict - the type of NLU run and result
|
140 |
+
{'type':'integer', 'data': '8', 'confidence': 0}
|
141 |
+
{'type':'sentiment', 'data': 'negative', 'confidence': 0.99}
|
142 |
+
"""
|
143 |
+
data_dict = await request.json()
|
144 |
+
message_data = data_dict.get('message_data', '')
|
145 |
+
nlu_response = evaluate_message_with_nlu(message_data)
|
146 |
+
return JSONResponse(content=nlu_response)
|
147 |
+
|
148 |
+
|
149 |
+
@app.post("/num_one")
|
150 |
+
async def num_one(request: Request):
|
151 |
+
"""
|
152 |
+
Input:
|
153 |
+
{
|
154 |
+
"user_id": 1,
|
155 |
+
"message_text": 5,
|
156 |
+
}
|
157 |
+
Output:
|
158 |
+
{
|
159 |
+
'messages':
|
160 |
+
["Let's", 'practice', 'counting', '', '', '46...', '47...', '48...', '49', '', '', 'After', '49,', 'what', 'is', 'the', 'next', 'number', 'you', 'will', 'count?\n46,', '47,', '48,', '49'],
|
161 |
+
'input_prompt': '50',
|
162 |
+
'state': 'question'
|
163 |
+
}
|
164 |
+
"""
|
165 |
+
print("STEP 1")
|
166 |
+
data_dict = await request.json()
|
167 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
168 |
+
user_id = message_data['user_id']
|
169 |
+
message_text = message_data['message_text']
|
170 |
+
print("STEP 2")
|
171 |
+
return num_one_quiz.process_user_message(user_id, message_text)
|
172 |
+
|
173 |
+
|
174 |
+
@app.post("/start")
|
175 |
+
async def ask_math_question(request: Request):
|
176 |
+
"""Generate a question data
|
177 |
+
|
178 |
+
Input
|
179 |
+
{
|
180 |
+
'difficulty': 0.1,
|
181 |
+
'do_increase': True | False
|
182 |
+
}
|
183 |
+
|
184 |
+
Output
|
185 |
+
{
|
186 |
+
'text': 'What is 1+2?',
|
187 |
+
'difficulty': 0.2,
|
188 |
+
'question_numbers': [3, 1, 4]
|
189 |
+
}
|
190 |
+
"""
|
191 |
+
data_dict = await request.json()
|
192 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
193 |
+
difficulty = message_data['difficulty']
|
194 |
+
do_increase = message_data['do_increase']
|
195 |
+
|
196 |
+
return JSONResponse(generators.start_interactive_math(difficulty, do_increase))
|
197 |
+
|
198 |
+
|
199 |
+
@app.post("/hint")
|
200 |
+
async def get_hint(request: Request):
|
201 |
+
"""Generate a hint data
|
202 |
+
|
203 |
+
Input
|
204 |
+
{
|
205 |
+
'start': 5,
|
206 |
+
'step': 1,
|
207 |
+
'difficulty': 0.1
|
208 |
+
}
|
209 |
+
|
210 |
+
Output
|
211 |
+
{
|
212 |
+
'text': 'What number is greater than 4 and less than 6?',
|
213 |
+
'difficulty': 0.1,
|
214 |
+
'question_numbers': [5, 1, 6]
|
215 |
+
}
|
216 |
+
"""
|
217 |
+
data_dict = await request.json()
|
218 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
219 |
+
start = message_data['start']
|
220 |
+
step = message_data['step']
|
221 |
+
difficulty = message_data['difficulty']
|
222 |
+
|
223 |
+
return JSONResponse(hints.generate_hint(start, step, difficulty))
|
224 |
+
|
225 |
+
|
226 |
+
@app.post("/question")
|
227 |
+
async def ask_math_question(request: Request):
|
228 |
+
"""Generate a question data
|
229 |
+
|
230 |
+
Input
|
231 |
+
{
|
232 |
+
'start': 5,
|
233 |
+
'step': 1,
|
234 |
+
'question_num': 1 # optional
|
235 |
+
}
|
236 |
+
|
237 |
+
Output
|
238 |
+
{
|
239 |
+
'question': 'What is 1+2?',
|
240 |
+
'start': 5,
|
241 |
+
'step': 1,
|
242 |
+
'answer': 6
|
243 |
+
}
|
244 |
+
"""
|
245 |
+
data_dict = await request.json()
|
246 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
247 |
+
start = message_data['start']
|
248 |
+
step = message_data['step']
|
249 |
+
arg_tuple = (start, step)
|
250 |
+
try:
|
251 |
+
question_num = message_data['question_num']
|
252 |
+
arg_tuple += (question_num,)
|
253 |
+
except KeyError:
|
254 |
+
pass
|
255 |
+
|
256 |
+
return JSONResponse(questions.generate_question_data(*arg_tuple))
|
257 |
+
|
258 |
+
|
259 |
+
@app.post("/difficulty")
|
260 |
+
async def get_hint(request: Request):
|
261 |
+
"""Generate a number matching difficulty
|
262 |
+
|
263 |
+
Input
|
264 |
+
{
|
265 |
+
'difficulty': 0.01,
|
266 |
+
'do_increase': True
|
267 |
+
}
|
268 |
+
|
269 |
+
Output - value from 0.01 to 0.99 inclusively:
|
270 |
+
0.09
|
271 |
+
"""
|
272 |
+
data_dict = await request.json()
|
273 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
274 |
+
difficulty = message_data['difficulty']
|
275 |
+
do_increase = message_data['do_increase']
|
276 |
+
|
277 |
+
return JSONResponse(utils.get_next_difficulty(difficulty, do_increase))
|
278 |
+
|
279 |
+
|
280 |
+
@app.post("/start_step")
|
281 |
+
async def get_hint(request: Request):
|
282 |
+
"""Generate a start and step values
|
283 |
+
|
284 |
+
Input
|
285 |
+
{
|
286 |
+
'difficulty': 0.01,
|
287 |
+
'path_to_csv_file': 'scripts/quiz/data.csv' # optional
|
288 |
+
}
|
289 |
+
|
290 |
+
Output - tuple (start, step):
|
291 |
+
(5, 1)
|
292 |
+
"""
|
293 |
+
data_dict = await request.json()
|
294 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
295 |
+
difficulty = message_data['difficulty']
|
296 |
+
arg_tuple = (difficulty,)
|
297 |
+
try:
|
298 |
+
path_to_csv_file = message_data['path_to_csv_file']
|
299 |
+
arg_tuple += (path_to_csv_file,)
|
300 |
+
except KeyError:
|
301 |
+
pass
|
302 |
+
|
303 |
+
return JSONResponse(utils.get_next_difficulty(*arg_tuple))
|
304 |
+
|
305 |
+
|
306 |
+
@app.post("/sequence")
|
307 |
+
async def generate_question(request: Request):
|
308 |
+
"""Generate a sequence from start, step and optional separator parameter
|
309 |
+
|
310 |
+
Input
|
311 |
+
{
|
312 |
+
'start': 5,
|
313 |
+
'step': 1,
|
314 |
+
'sep': ', ' # optional
|
315 |
+
}
|
316 |
+
|
317 |
+
Output
|
318 |
+
5, 6, 7
|
319 |
+
"""
|
320 |
+
data_dict = await request.json()
|
321 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
322 |
+
start = message_data['start']
|
323 |
+
step = message_data['step']
|
324 |
+
arg_tuple = (start, step)
|
325 |
+
try:
|
326 |
+
sep = message_data['sep']
|
327 |
+
arg_tuple += (sep,)
|
328 |
+
except KeyError:
|
329 |
+
pass
|
330 |
+
|
331 |
+
return JSONResponse(utils.convert_sequence_to_string(*arg_tuple))
|
docs/transitions_math_quiz_example.ipynb
ADDED
@@ -0,0 +1,368 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 19,
|
6 |
+
"id": "d3da0422",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import random\n",
|
11 |
+
"\n",
|
12 |
+
"from transitions import State, Machine"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"cell_type": "code",
|
17 |
+
"execution_count": 20,
|
18 |
+
"id": "07cfb740",
|
19 |
+
"metadata": {},
|
20 |
+
"outputs": [],
|
21 |
+
"source": [
|
22 |
+
"class MathQuizFSM(object):\n",
|
23 |
+
" states = [\n",
|
24 |
+
" 'quiz_start', \n",
|
25 |
+
" 'quiz_question', \n",
|
26 |
+
" 'quiz_end'\n",
|
27 |
+
" ]\n",
|
28 |
+
"\n",
|
29 |
+
" transitions = [\n",
|
30 |
+
" ['ask_second_question', 'quiz_start', 'quiz_question'],\n",
|
31 |
+
" ['ask_next_question', 'quiz_question', 'quiz_question'],\n",
|
32 |
+
" ['exit', 'quiz_start', 'quiz_end'],\n",
|
33 |
+
" ['exit', 'quiz_question', 'quiz_end'],\n",
|
34 |
+
" ]\n",
|
35 |
+
" \n",
|
36 |
+
" \n",
|
37 |
+
" def __init__(self):\n",
|
38 |
+
" # Instantiate the FSM\n",
|
39 |
+
" self.machine = Machine(model=self, states=MathQuizFSM.states, transitions=MathQuizFSM.transitions,initial='quiz_start')\n",
|
40 |
+
"\n",
|
41 |
+
" # Instantiate variables necessary for tracking activity\n",
|
42 |
+
" self.question_nums = [2, 3]\n",
|
43 |
+
" self.correct_answer = 5\n",
|
44 |
+
" self.student_answer = 0\n",
|
45 |
+
" self.is_correct_answer = False\n",
|
46 |
+
" self.response_text = \"What is 2 + 3?\"\n",
|
47 |
+
"\n",
|
48 |
+
" # Define transitions\n",
|
49 |
+
"# self.machine.add_transition('ask_second_question', 'quiz_start', 'quiz_question')\n",
|
50 |
+
"# self.machine.add_transition('ask_next_question', 'quiz_question', 'quiz_question')\n",
|
51 |
+
"# self.machine.add_transition('exit', 'quiz_start', 'quiz_end')\n",
|
52 |
+
"# self.machine.add_transition('exit', 'quiz_question', 'quiz_end')\n",
|
53 |
+
"\n",
|
54 |
+
" # Define functions to run on transitions\n",
|
55 |
+
" self.machine.on_enter_quiz_question('generate_math_problem')\n",
|
56 |
+
" self.machine.on_exit_quiz_question('validate_answer')\n",
|
57 |
+
"\n",
|
58 |
+
" def validate_answer(self):\n",
|
59 |
+
" if self.student_answer == 'exit':\n",
|
60 |
+
" self.machine.set_state('quiz_end')\n",
|
61 |
+
" return [\"Come back any time!\"]\n",
|
62 |
+
" elif self.correct_answer == self.student_answer:\n",
|
63 |
+
" self.machine.set_state('quiz_question')\n",
|
64 |
+
" self.generate_math_problem()\n",
|
65 |
+
" return ['Great job!', self.response_text]\n",
|
66 |
+
" else:\n",
|
67 |
+
" return [\"That's not quite right. Try again.\",self.response_text]\n",
|
68 |
+
" \n",
|
69 |
+
" def generate_math_problem(self):\n",
|
70 |
+
" self.question_nums = random.sample(range(1,100),2)\n",
|
71 |
+
" self.response_text = f\"What is {self.question_nums[0]} + {self.question_nums[1]}\"\n",
|
72 |
+
" self.correct_answer = self.question_nums[0] + self.question_nums[1]\n"
|
73 |
+
]
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"cell_type": "code",
|
77 |
+
"execution_count": 21,
|
78 |
+
"id": "ebdf92ae",
|
79 |
+
"metadata": {},
|
80 |
+
"outputs": [],
|
81 |
+
"source": [
|
82 |
+
"test = MathQuizFSM()"
|
83 |
+
]
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"cell_type": "code",
|
87 |
+
"execution_count": 22,
|
88 |
+
"id": "92024fcc",
|
89 |
+
"metadata": {},
|
90 |
+
"outputs": [
|
91 |
+
{
|
92 |
+
"data": {
|
93 |
+
"text/plain": [
|
94 |
+
"'quiz_start'"
|
95 |
+
]
|
96 |
+
},
|
97 |
+
"execution_count": 22,
|
98 |
+
"metadata": {},
|
99 |
+
"output_type": "execute_result"
|
100 |
+
}
|
101 |
+
],
|
102 |
+
"source": [
|
103 |
+
"# Set as `quiz_start` due to the initial setting in Line 10\n",
|
104 |
+
"test.state"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"cell_type": "code",
|
109 |
+
"execution_count": 23,
|
110 |
+
"id": "fd1ba433",
|
111 |
+
"metadata": {},
|
112 |
+
"outputs": [
|
113 |
+
{
|
114 |
+
"data": {
|
115 |
+
"text/plain": [
|
116 |
+
"['quiz_start', 'quiz_question', 'quiz_end']"
|
117 |
+
]
|
118 |
+
},
|
119 |
+
"execution_count": 23,
|
120 |
+
"metadata": {},
|
121 |
+
"output_type": "execute_result"
|
122 |
+
}
|
123 |
+
],
|
124 |
+
"source": [
|
125 |
+
"# Available states for the quiz module\n",
|
126 |
+
"test.states"
|
127 |
+
]
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"cell_type": "code",
|
131 |
+
"execution_count": 24,
|
132 |
+
"id": "bb190089",
|
133 |
+
"metadata": {},
|
134 |
+
"outputs": [
|
135 |
+
{
|
136 |
+
"name": "stdout",
|
137 |
+
"output_type": "stream",
|
138 |
+
"text": [
|
139 |
+
"What is 2 + 3?\n",
|
140 |
+
"Initial Correct Answer: 5\n",
|
141 |
+
"Initial Student Answer: 0\n"
|
142 |
+
]
|
143 |
+
}
|
144 |
+
],
|
145 |
+
"source": [
|
146 |
+
"# When the FSM is created, it comes with a default question/answer pair loaded\n",
|
147 |
+
"print(test.response_text)\n",
|
148 |
+
"print(f\"Initial Correct Answer: {test.correct_answer}\")\n",
|
149 |
+
"print(f\"Initial Student Answer: {test.student_answer}\")"
|
150 |
+
]
|
151 |
+
},
|
152 |
+
{
|
153 |
+
"cell_type": "code",
|
154 |
+
"execution_count": 25,
|
155 |
+
"id": "3de7c4e0",
|
156 |
+
"metadata": {},
|
157 |
+
"outputs": [
|
158 |
+
{
|
159 |
+
"data": {
|
160 |
+
"text/plain": [
|
161 |
+
"[\"That's not quite right. Try again.\", 'What is 2 + 3?']"
|
162 |
+
]
|
163 |
+
},
|
164 |
+
"execution_count": 25,
|
165 |
+
"metadata": {},
|
166 |
+
"output_type": "execute_result"
|
167 |
+
}
|
168 |
+
],
|
169 |
+
"source": [
|
170 |
+
"# Calling the validation fails because the answer is wrong. The state remains the same.\n",
|
171 |
+
"test.validate_answer()"
|
172 |
+
]
|
173 |
+
},
|
174 |
+
{
|
175 |
+
"cell_type": "code",
|
176 |
+
"execution_count": 26,
|
177 |
+
"id": "4935b470",
|
178 |
+
"metadata": {},
|
179 |
+
"outputs": [],
|
180 |
+
"source": [
|
181 |
+
"# The student tries again\n",
|
182 |
+
"test.student_answer = 5"
|
183 |
+
]
|
184 |
+
},
|
185 |
+
{
|
186 |
+
"cell_type": "code",
|
187 |
+
"execution_count": 27,
|
188 |
+
"id": "03722434",
|
189 |
+
"metadata": {},
|
190 |
+
"outputs": [
|
191 |
+
{
|
192 |
+
"data": {
|
193 |
+
"text/plain": [
|
194 |
+
"['Great job!', 'What is 58 + 89']"
|
195 |
+
]
|
196 |
+
},
|
197 |
+
"execution_count": 27,
|
198 |
+
"metadata": {},
|
199 |
+
"output_type": "execute_result"
|
200 |
+
}
|
201 |
+
],
|
202 |
+
"source": [
|
203 |
+
"# Since the student answered correctly, MathQuizFSM generates a new math problem\n",
|
204 |
+
"test.validate_answer()"
|
205 |
+
]
|
206 |
+
},
|
207 |
+
{
|
208 |
+
"cell_type": "code",
|
209 |
+
"execution_count": 28,
|
210 |
+
"id": "d98a4d5b",
|
211 |
+
"metadata": {},
|
212 |
+
"outputs": [
|
213 |
+
{
|
214 |
+
"data": {
|
215 |
+
"text/plain": [
|
216 |
+
"'quiz_question'"
|
217 |
+
]
|
218 |
+
},
|
219 |
+
"execution_count": 28,
|
220 |
+
"metadata": {},
|
221 |
+
"output_type": "execute_result"
|
222 |
+
}
|
223 |
+
],
|
224 |
+
"source": [
|
225 |
+
"# It will repeatedly re-activate the same state\n",
|
226 |
+
"test.state"
|
227 |
+
]
|
228 |
+
},
|
229 |
+
{
|
230 |
+
"cell_type": "code",
|
231 |
+
"execution_count": 29,
|
232 |
+
"id": "76c8a5b2",
|
233 |
+
"metadata": {},
|
234 |
+
"outputs": [
|
235 |
+
{
|
236 |
+
"data": {
|
237 |
+
"text/plain": [
|
238 |
+
"[\"That's not quite right. Try again.\", 'What is 58 + 89']"
|
239 |
+
]
|
240 |
+
},
|
241 |
+
"execution_count": 29,
|
242 |
+
"metadata": {},
|
243 |
+
"output_type": "execute_result"
|
244 |
+
}
|
245 |
+
],
|
246 |
+
"source": [
|
247 |
+
"test.validate_answer()"
|
248 |
+
]
|
249 |
+
},
|
250 |
+
{
|
251 |
+
"cell_type": "code",
|
252 |
+
"execution_count": 30,
|
253 |
+
"id": "ec0a7e6a",
|
254 |
+
"metadata": {},
|
255 |
+
"outputs": [],
|
256 |
+
"source": [
|
257 |
+
"test.student_answer = 128"
|
258 |
+
]
|
259 |
+
},
|
260 |
+
{
|
261 |
+
"cell_type": "code",
|
262 |
+
"execution_count": 31,
|
263 |
+
"id": "a093ff27",
|
264 |
+
"metadata": {},
|
265 |
+
"outputs": [
|
266 |
+
{
|
267 |
+
"data": {
|
268 |
+
"text/plain": [
|
269 |
+
"[\"That's not quite right. Try again.\", 'What is 58 + 89']"
|
270 |
+
]
|
271 |
+
},
|
272 |
+
"execution_count": 31,
|
273 |
+
"metadata": {},
|
274 |
+
"output_type": "execute_result"
|
275 |
+
}
|
276 |
+
],
|
277 |
+
"source": [
|
278 |
+
"test.validate_answer()"
|
279 |
+
]
|
280 |
+
},
|
281 |
+
{
|
282 |
+
"cell_type": "code",
|
283 |
+
"execution_count": 32,
|
284 |
+
"id": "f992d34d",
|
285 |
+
"metadata": {},
|
286 |
+
"outputs": [],
|
287 |
+
"source": [
|
288 |
+
"test.student_answer = 'exit'"
|
289 |
+
]
|
290 |
+
},
|
291 |
+
{
|
292 |
+
"cell_type": "code",
|
293 |
+
"execution_count": 33,
|
294 |
+
"id": "28800a2b",
|
295 |
+
"metadata": {},
|
296 |
+
"outputs": [
|
297 |
+
{
|
298 |
+
"data": {
|
299 |
+
"text/plain": [
|
300 |
+
"['Come back any time!']"
|
301 |
+
]
|
302 |
+
},
|
303 |
+
"execution_count": 33,
|
304 |
+
"metadata": {},
|
305 |
+
"output_type": "execute_result"
|
306 |
+
}
|
307 |
+
],
|
308 |
+
"source": [
|
309 |
+
"test.validate_answer()"
|
310 |
+
]
|
311 |
+
},
|
312 |
+
{
|
313 |
+
"cell_type": "code",
|
314 |
+
"execution_count": 34,
|
315 |
+
"id": "360ef774",
|
316 |
+
"metadata": {},
|
317 |
+
"outputs": [
|
318 |
+
{
|
319 |
+
"data": {
|
320 |
+
"text/plain": [
|
321 |
+
"'quiz_end'"
|
322 |
+
]
|
323 |
+
},
|
324 |
+
"execution_count": 34,
|
325 |
+
"metadata": {},
|
326 |
+
"output_type": "execute_result"
|
327 |
+
}
|
328 |
+
],
|
329 |
+
"source": [
|
330 |
+
"test.state"
|
331 |
+
]
|
332 |
+
},
|
333 |
+
{
|
334 |
+
"cell_type": "code",
|
335 |
+
"execution_count": null,
|
336 |
+
"id": "3f0392ae",
|
337 |
+
"metadata": {},
|
338 |
+
"outputs": [],
|
339 |
+
"source": []
|
340 |
+
}
|
341 |
+
],
|
342 |
+
"metadata": {
|
343 |
+
"kernelspec": {
|
344 |
+
"display_name": "base",
|
345 |
+
"language": "python",
|
346 |
+
"name": "python3"
|
347 |
+
},
|
348 |
+
"language_info": {
|
349 |
+
"codemirror_mode": {
|
350 |
+
"name": "ipython",
|
351 |
+
"version": 3
|
352 |
+
},
|
353 |
+
"file_extension": ".py",
|
354 |
+
"mimetype": "text/x-python",
|
355 |
+
"name": "python",
|
356 |
+
"nbconvert_exporter": "python",
|
357 |
+
"pygments_lexer": "ipython3",
|
358 |
+
"version": "3.9.7"
|
359 |
+
},
|
360 |
+
"vscode": {
|
361 |
+
"interpreter": {
|
362 |
+
"hash": "32cf04bfac80a5e1e74e86fca42ae7f3079b15fa61041a60732bc19e88699268"
|
363 |
+
}
|
364 |
+
}
|
365 |
+
},
|
366 |
+
"nbformat": 4,
|
367 |
+
"nbformat_minor": 5
|
368 |
+
}
|
mathtext_fastapi/__init__.py
ADDED
File without changes
|
mathtext_fastapi/conversation_manager.py
ADDED
@@ -0,0 +1,437 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import dill
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import jsonpickle
|
6 |
+
import pickle
|
7 |
+
import random
|
8 |
+
import requests
|
9 |
+
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
from mathtext_fastapi.nlu import evaluate_message_with_nlu
|
12 |
+
from mathtext_fastapi.math_quiz_fsm import MathQuizFSM
|
13 |
+
from mathtext_fastapi.math_subtraction_fsm import MathSubtractionFSM
|
14 |
+
from supabase import create_client
|
15 |
+
from transitions import Machine
|
16 |
+
|
17 |
+
from mathactive.generators import start_interactive_math
|
18 |
+
from mathactive.hints import generate_hint
|
19 |
+
|
20 |
+
load_dotenv()
|
21 |
+
|
22 |
+
SUPA = create_client(
|
23 |
+
os.environ.get('SUPABASE_URL'),
|
24 |
+
os.environ.get('SUPABASE_KEY')
|
25 |
+
)
|
26 |
+
|
27 |
+
|
28 |
+
def create_text_message(message_text, whatsapp_id):
|
29 |
+
""" Fills a template with input values to send a text message to Whatsapp
|
30 |
+
|
31 |
+
Inputs
|
32 |
+
- message_text: str - the content that the message should display
|
33 |
+
- whatsapp_id: str - the message recipient's phone number
|
34 |
+
|
35 |
+
Outputs
|
36 |
+
- message_data: dict - a preformatted template filled with inputs
|
37 |
+
"""
|
38 |
+
message_data = {
|
39 |
+
"preview_url": False,
|
40 |
+
"recipient_type": "individual",
|
41 |
+
"to": whatsapp_id,
|
42 |
+
"type": "text",
|
43 |
+
"text": {
|
44 |
+
"body": message_text
|
45 |
+
}
|
46 |
+
}
|
47 |
+
return message_data
|
48 |
+
|
49 |
+
|
50 |
+
def create_button_objects(button_options):
|
51 |
+
""" Creates a list of button objects using the input values
|
52 |
+
Input
|
53 |
+
- button_options: list - a list of text to be displayed in buttons
|
54 |
+
|
55 |
+
Output
|
56 |
+
- button_arr: list - preformatted button objects filled with the inputs
|
57 |
+
|
58 |
+
NOTE: Not fully implemented and tested
|
59 |
+
"""
|
60 |
+
button_arr = []
|
61 |
+
for option in button_options:
|
62 |
+
button_choice = {
|
63 |
+
"type": "reply",
|
64 |
+
"reply": {
|
65 |
+
"id": "inquiry-yes",
|
66 |
+
"title": option['text']
|
67 |
+
}
|
68 |
+
}
|
69 |
+
button_arr.append(button_choice)
|
70 |
+
return button_arr
|
71 |
+
|
72 |
+
|
73 |
+
def create_interactive_message(message_text, button_options, whatsapp_id):
|
74 |
+
""" Fills a template to create a button message for Whatsapp
|
75 |
+
|
76 |
+
* NOTE: Not fully implemented and tested
|
77 |
+
* NOTE/TODO: It is possible to create other kinds of messages
|
78 |
+
with the 'interactive message' template
|
79 |
+
* Documentation:
|
80 |
+
https://whatsapp.turn.io/docs/api/messages#interactive-messages
|
81 |
+
|
82 |
+
Inputs
|
83 |
+
- message_text: str - the content that the message should display
|
84 |
+
- button_options: list - what each button option should display
|
85 |
+
- whatsapp_id: str - the message recipient's phone number
|
86 |
+
"""
|
87 |
+
button_arr = create_button_objects(button_options)
|
88 |
+
|
89 |
+
data = {
|
90 |
+
"to": whatsapp_id,
|
91 |
+
"type": "interactive",
|
92 |
+
"interactive": {
|
93 |
+
"type": "button",
|
94 |
+
# "header": { },
|
95 |
+
"body": {
|
96 |
+
"text": message_text
|
97 |
+
},
|
98 |
+
# "footer": { },
|
99 |
+
"action": {
|
100 |
+
"buttons": button_arr
|
101 |
+
}
|
102 |
+
}
|
103 |
+
}
|
104 |
+
return data
|
105 |
+
|
106 |
+
|
107 |
+
def pickle_and_encode_state_machine(state_machine):
|
108 |
+
dump = pickle.dumps(state_machine)
|
109 |
+
dump_encoded = base64.b64encode(dump).decode('utf-8')
|
110 |
+
return dump_encoded
|
111 |
+
|
112 |
+
|
113 |
+
def manage_math_quiz_fsm(user_message, contact_uuid, type):
|
114 |
+
fsm_check = SUPA.table('state_machines').select("*").eq(
|
115 |
+
"contact_uuid",
|
116 |
+
contact_uuid
|
117 |
+
).execute()
|
118 |
+
|
119 |
+
# This doesn't allow for when one FSM is present and the other is empty
|
120 |
+
"""
|
121 |
+
1
|
122 |
+
data=[] count=None
|
123 |
+
|
124 |
+
2
|
125 |
+
data=[{'id': 29, 'contact_uuid': 'j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09', 'addition3': None, 'subtraction': None, 'addition':
|
126 |
+
|
127 |
+
- but problem is there is no subtraction , but it's assuming there's a subtration
|
128 |
+
|
129 |
+
|
130 |
+
Cases
|
131 |
+
- make a completely new record
|
132 |
+
- update an existing record with an existing FSM
|
133 |
+
- update an existing record without an existing FSM
|
134 |
+
"""
|
135 |
+
|
136 |
+
|
137 |
+
# Make a completely new entry
|
138 |
+
if fsm_check.data == []:
|
139 |
+
if type == 'addition':
|
140 |
+
math_quiz_state_machine = MathQuizFSM()
|
141 |
+
else:
|
142 |
+
math_quiz_state_machine = MathSubtractionFSM()
|
143 |
+
messages = [math_quiz_state_machine.response_text]
|
144 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
145 |
+
|
146 |
+
SUPA.table('state_machines').insert({
|
147 |
+
'contact_uuid': contact_uuid,
|
148 |
+
f'{type}': dump_encoded
|
149 |
+
}).execute()
|
150 |
+
# Update an existing record with a new state machine
|
151 |
+
elif not fsm_check.data[0][type]:
|
152 |
+
if type == 'addition':
|
153 |
+
math_quiz_state_machine = MathQuizFSM()
|
154 |
+
else:
|
155 |
+
math_quiz_state_machine = MathSubtractionFSM()
|
156 |
+
messages = [math_quiz_state_machine.response_text]
|
157 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
158 |
+
|
159 |
+
SUPA.table('state_machines').update({
|
160 |
+
f'{type}': dump_encoded
|
161 |
+
}).eq(
|
162 |
+
"contact_uuid", contact_uuid
|
163 |
+
).execute()
|
164 |
+
# Update an existing record with an existing state machine
|
165 |
+
elif fsm_check.data[0][type]:
|
166 |
+
undump_encoded = base64.b64decode(
|
167 |
+
fsm_check.data[0][type].encode('utf-8')
|
168 |
+
)
|
169 |
+
math_quiz_state_machine = pickle.loads(undump_encoded)
|
170 |
+
|
171 |
+
math_quiz_state_machine.student_answer = user_message
|
172 |
+
math_quiz_state_machine.correct_answer = str(math_quiz_state_machine.correct_answer)
|
173 |
+
messages = math_quiz_state_machine.validate_answer()
|
174 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
175 |
+
SUPA.table('state_machines').update({
|
176 |
+
f'{type}': dump_encoded
|
177 |
+
}).eq(
|
178 |
+
"contact_uuid", contact_uuid
|
179 |
+
).execute()
|
180 |
+
return messages
|
181 |
+
|
182 |
+
|
183 |
+
def use_quiz_module_approach(user_message, context_data):
|
184 |
+
print("USER MESSAGE")
|
185 |
+
print(user_message)
|
186 |
+
print("=======================")
|
187 |
+
if user_message == 'add':
|
188 |
+
context_result = start_interactive_math()
|
189 |
+
message_package = {
|
190 |
+
'messages': [
|
191 |
+
"Great, let's do some addition",
|
192 |
+
"First, we'll start with single digits.",
|
193 |
+
"Type your response as a number. For example, for '1 + 1', you'd write 2."
|
194 |
+
],
|
195 |
+
'input_prompt': context_result['text'],
|
196 |
+
'state': "addition-question-sequence"
|
197 |
+
}
|
198 |
+
|
199 |
+
elif user_message == context_data.get('right_answer'):
|
200 |
+
context_result = start_interactive_math(
|
201 |
+
context_data['number_correct'],
|
202 |
+
context_data['number_incorrect'],
|
203 |
+
context_data['level']
|
204 |
+
)
|
205 |
+
message_package = {
|
206 |
+
'messages': [
|
207 |
+
"That's right, great!",
|
208 |
+
],
|
209 |
+
'input_prompt': context_result['text'],
|
210 |
+
'state': "addition-question-sequence"
|
211 |
+
}
|
212 |
+
else:
|
213 |
+
context_result = generate_hint(
|
214 |
+
context_data['question_numbers'],
|
215 |
+
context_data['right_answer'],
|
216 |
+
context_data['number_correct'],
|
217 |
+
context_data['number_incorrect'],
|
218 |
+
context_data['level'],
|
219 |
+
context_data['hints_used']
|
220 |
+
)
|
221 |
+
message_package = {
|
222 |
+
'messages': [
|
223 |
+
context_result['text'],
|
224 |
+
],
|
225 |
+
'input_prompt': context_data['text'],
|
226 |
+
'state': "addition-question-sequence"
|
227 |
+
}
|
228 |
+
return message_package, context_result
|
229 |
+
|
230 |
+
|
231 |
+
def return_next_conversational_state(context_data, user_message, contact_uuid):
|
232 |
+
""" Evaluates the conversation's current state to determine the next state
|
233 |
+
|
234 |
+
Input
|
235 |
+
- context_data: dict - data about the conversation's current state
|
236 |
+
- user_message: str - the message the user sent in response to the state
|
237 |
+
|
238 |
+
Output
|
239 |
+
- message_package: dict - a series of messages and prompt to send
|
240 |
+
"""
|
241 |
+
if context_data['user_message'] == '' and \
|
242 |
+
context_data['state'] == 'start-conversation':
|
243 |
+
message_package = {
|
244 |
+
'messages': [],
|
245 |
+
'input_prompt': "Welcome to our math practice. What would you like to try? Type add or subtract.",
|
246 |
+
'state': "welcome-sequence"
|
247 |
+
}
|
248 |
+
elif context_data['state'] == 'addition-question-sequence' or \
|
249 |
+
user_message == 'add':
|
250 |
+
|
251 |
+
# Used in FSM
|
252 |
+
# messages = manage_math_quiz_fsm(user_message, contact_uuid)
|
253 |
+
|
254 |
+
# message_package, context_result = use_quiz_module_approach(user_message, context_data)
|
255 |
+
messages = manage_math_quiz_fsm(user_message, contact_uuid, 'addition')
|
256 |
+
|
257 |
+
if user_message == 'exit':
|
258 |
+
state_label = 'exit'
|
259 |
+
else:
|
260 |
+
state_label = 'addition-question-sequence'
|
261 |
+
# Used in FSM
|
262 |
+
input_prompt = messages.pop()
|
263 |
+
message_package = {
|
264 |
+
'messages': messages,
|
265 |
+
'input_prompt': input_prompt,
|
266 |
+
'state': state_label
|
267 |
+
}
|
268 |
+
|
269 |
+
# Used in quiz w/ hints
|
270 |
+
# context_data = context_result
|
271 |
+
# message_package['state'] = state_label
|
272 |
+
|
273 |
+
elif context_data['state'] == 'subtraction-question-sequence' or \
|
274 |
+
user_message == 'subtract':
|
275 |
+
messages = manage_math_quiz_fsm(user_message, contact_uuid, 'subtraction')
|
276 |
+
|
277 |
+
if user_message == 'exit':
|
278 |
+
state_label = 'exit'
|
279 |
+
else:
|
280 |
+
state_label = 'subtraction-question-sequence'
|
281 |
+
|
282 |
+
input_prompt = messages.pop()
|
283 |
+
|
284 |
+
message_package = {
|
285 |
+
'messages': messages,
|
286 |
+
'input_prompt': input_prompt,
|
287 |
+
'state': state_label
|
288 |
+
}
|
289 |
+
|
290 |
+
# message_package = {
|
291 |
+
# 'messages': [
|
292 |
+
# "Time for some subtraction!",
|
293 |
+
# "Type your response as a number. For example, for '1 - 1', you'd write 0."
|
294 |
+
# ],
|
295 |
+
# 'input_prompt': "Here's the first one... What's 3-1?",
|
296 |
+
# 'state': "subtract-question-sequence"
|
297 |
+
# }
|
298 |
+
elif context_data['state'] == 'exit' or user_message == 'exit':
|
299 |
+
message_package = {
|
300 |
+
'messages': [
|
301 |
+
"Great, thanks for practicing math today. Come back any time."
|
302 |
+
],
|
303 |
+
'input_prompt': "",
|
304 |
+
'state': "exit"
|
305 |
+
}
|
306 |
+
else:
|
307 |
+
message_package = {
|
308 |
+
'messages': [
|
309 |
+
"Hmmm...sorry friend. I'm not really sure what to do."
|
310 |
+
],
|
311 |
+
'input_prompt': "Please type add or subtract to start a math activity.",
|
312 |
+
'state': "reprompt-menu-options"
|
313 |
+
}
|
314 |
+
# Used in FSM
|
315 |
+
return message_package
|
316 |
+
|
317 |
+
# Used in quiz folder approach
|
318 |
+
# return context_result, message_package
|
319 |
+
|
320 |
+
|
321 |
+
def manage_conversation_response(data_json):
|
322 |
+
""" Calls functions necessary to determine message and context data to send
|
323 |
+
|
324 |
+
Input
|
325 |
+
- data_json: dict - message data from Turn.io/Whatsapp
|
326 |
+
|
327 |
+
Output
|
328 |
+
- context: dict - a record of the state at a given point a conversation
|
329 |
+
|
330 |
+
TODOs
|
331 |
+
- implement logging of message
|
332 |
+
- test interactive messages
|
333 |
+
- review context object and re-work to use a standardized format
|
334 |
+
- review ways for more robust error handling
|
335 |
+
- need to make util functions that apply to both /nlu and /conversation_manager
|
336 |
+
"""
|
337 |
+
message_data = data_json.get('message_data', '')
|
338 |
+
context_data = data_json.get('context_data', '')
|
339 |
+
|
340 |
+
whatsapp_id = message_data['author_id']
|
341 |
+
user_message = message_data['message_body']
|
342 |
+
contact_uuid = message_data['contact_uuid']
|
343 |
+
|
344 |
+
# TODO: Need to incorporate nlu_response into wormhole by checking answers against database (spreadsheet?)
|
345 |
+
nlu_response = evaluate_message_with_nlu(message_data)
|
346 |
+
|
347 |
+
if context_data['state'] == 'addition':
|
348 |
+
context_result, message_package = return_next_conversational_state(
|
349 |
+
context_data,
|
350 |
+
user_message,
|
351 |
+
contact_uuid
|
352 |
+
)
|
353 |
+
else:
|
354 |
+
message_package = return_next_conversational_state(
|
355 |
+
context_data,
|
356 |
+
user_message,
|
357 |
+
contact_uuid
|
358 |
+
)
|
359 |
+
|
360 |
+
headers = {
|
361 |
+
'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
|
362 |
+
'Content-Type': 'application/json'
|
363 |
+
}
|
364 |
+
|
365 |
+
# Send all messages for the current state before a user input prompt (text/button input request)
|
366 |
+
for message in message_package['messages']:
|
367 |
+
data = create_text_message(message, whatsapp_id)
|
368 |
+
|
369 |
+
print("data")
|
370 |
+
print(data)
|
371 |
+
|
372 |
+
r = requests.post(
|
373 |
+
f'https://whatsapp.turn.io/v1/messages',
|
374 |
+
data=json.dumps(data),
|
375 |
+
headers=headers
|
376 |
+
)
|
377 |
+
|
378 |
+
# Update the context object with the new state of the conversation
|
379 |
+
if context_data['state'] == 'addition':
|
380 |
+
context = {
|
381 |
+
"context": {
|
382 |
+
"user": whatsapp_id,
|
383 |
+
"state": message_package['state'],
|
384 |
+
"bot_message": message_package['input_prompt'],
|
385 |
+
"user_message": user_message,
|
386 |
+
"type": 'ask',
|
387 |
+
# Necessary for quiz folder approach
|
388 |
+
"text": context_result.get('text'),
|
389 |
+
"question_numbers": context_result.get('question_numbers'),
|
390 |
+
"right_answer": context_result.get('right_answer'),
|
391 |
+
"number_correct": context_result.get('number_correct'),
|
392 |
+
"hints_used": context_result.get('hints_used'),
|
393 |
+
}
|
394 |
+
}
|
395 |
+
else:
|
396 |
+
context = {
|
397 |
+
"context": {
|
398 |
+
"user": whatsapp_id,
|
399 |
+
"state": message_package['state'],
|
400 |
+
"bot_message": message_package['input_prompt'],
|
401 |
+
"user_message": user_message,
|
402 |
+
"type": 'ask',
|
403 |
+
}
|
404 |
+
}
|
405 |
+
|
406 |
+
return context
|
407 |
+
|
408 |
+
# data = {
|
409 |
+
# "to": whatsapp_id,
|
410 |
+
# "type": "interactive",
|
411 |
+
# "interactive": {
|
412 |
+
# "type": "button",
|
413 |
+
# # "header": { },
|
414 |
+
# "body": {
|
415 |
+
# "text": "Did I answer your question?"
|
416 |
+
# },
|
417 |
+
# # "footer": { },
|
418 |
+
# "action": {
|
419 |
+
# "buttons": [
|
420 |
+
# {
|
421 |
+
# "type": "reply",
|
422 |
+
# "reply": {
|
423 |
+
# "id": "inquiry-yes",
|
424 |
+
# "title": "Yes"
|
425 |
+
# }
|
426 |
+
# },
|
427 |
+
# {
|
428 |
+
# "type": "reply",
|
429 |
+
# "reply": {
|
430 |
+
# "id": "inquiry-no",
|
431 |
+
# "title": "No"
|
432 |
+
# }
|
433 |
+
# }
|
434 |
+
# ]
|
435 |
+
# }
|
436 |
+
# }
|
437 |
+
# }
|
mathtext_fastapi/curriculum_mapper.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
|
8 |
+
def read_and_preprocess_spreadsheet(file_name):
|
9 |
+
""" Creates a pandas dataframe from the curriculum overview spreadsheet """
|
10 |
+
DATA_DIR = Path(__file__).parent.parent / "mathtext_fastapi" / "data" / file_name
|
11 |
+
script_df = pd.read_excel(DATA_DIR, engine='openpyxl')
|
12 |
+
# Ensures the grade level columns are integers instead of floats
|
13 |
+
script_df.columns = script_df.columns[:2].tolist() + script_df.columns[2:11].astype(int).astype(str).tolist() + script_df.columns[11:].tolist()
|
14 |
+
script_df.fillna('', inplace=True)
|
15 |
+
return script_df
|
16 |
+
|
17 |
+
|
18 |
+
def extract_skill_code(skill):
|
19 |
+
""" Looks within a curricular skill description for its descriptive code
|
20 |
+
|
21 |
+
Input
|
22 |
+
- skill: str - a brief description of a curricular skill
|
23 |
+
|
24 |
+
>>> extract_skill_code('A3.3.4 - Solve inequalities')
|
25 |
+
'A3.3.4'
|
26 |
+
>>> extract_skill_code('A3.3.2 - Graph linear equations, and identify the x- and y-intercepts or the slope of a line')
|
27 |
+
'A3.3.2'
|
28 |
+
"""
|
29 |
+
pattern = r'[A-Z][0-9]\.\d+\.\d+'
|
30 |
+
result = re.search(pattern, skill)
|
31 |
+
return result.group()
|
32 |
+
|
33 |
+
|
34 |
+
def build_horizontal_transitions(script_df):
|
35 |
+
""" Build a list of transitional relationships within a curricular skill
|
36 |
+
|
37 |
+
Inputs
|
38 |
+
- script_df: pandas dataframe - an overview of the curriculum skills by grade level
|
39 |
+
|
40 |
+
Output
|
41 |
+
- horizontal_transitions: array of arrays - transition data with label, from state, and to state
|
42 |
+
|
43 |
+
>>> script_df = read_and_preprocess_spreadsheet('curriculum_framework_for_tests.xlsx')
|
44 |
+
>>> build_horizontal_transitions(script_df)
|
45 |
+
[['right', 'N1.1.1_G1', 'N1.1.1_G2'], ['right', 'N1.1.1_G2', 'N1.1.1_G3'], ['right', 'N1.1.1_G3', 'N1.1.1_G4'], ['right', 'N1.1.1_G4', 'N1.1.1_G5'], ['right', 'N1.1.1_G5', 'N1.1.1_G6'], ['left', 'N1.1.1_G6', 'N1.1.1_G5'], ['left', 'N1.1.1_G5', 'N1.1.1_G4'], ['left', 'N1.1.1_G4', 'N1.1.1_G3'], ['left', 'N1.1.1_G3', 'N1.1.1_G2'], ['left', 'N1.1.1_G2', 'N1.1.1_G1'], ['right', 'N1.1.2_G1', 'N1.1.2_G2'], ['right', 'N1.1.2_G2', 'N1.1.2_G3'], ['right', 'N1.1.2_G3', 'N1.1.2_G4'], ['right', 'N1.1.2_G4', 'N1.1.2_G5'], ['right', 'N1.1.2_G5', 'N1.1.2_G6'], ['left', 'N1.1.2_G6', 'N1.1.2_G5'], ['left', 'N1.1.2_G5', 'N1.1.2_G4'], ['left', 'N1.1.2_G4', 'N1.1.2_G3'], ['left', 'N1.1.2_G3', 'N1.1.2_G2'], ['left', 'N1.1.2_G2', 'N1.1.2_G1']]
|
46 |
+
"""
|
47 |
+
horizontal_transitions = []
|
48 |
+
for index, row in script_df.iterrows():
|
49 |
+
skill_code = extract_skill_code(row['Knowledge or Skill'])
|
50 |
+
|
51 |
+
rightward_matches = []
|
52 |
+
for i in range(9):
|
53 |
+
# Grade column
|
54 |
+
current_grade = i+1
|
55 |
+
if row[current_grade].lower().strip() == 'x':
|
56 |
+
rightward_matches.append(i)
|
57 |
+
|
58 |
+
for match in rightward_matches:
|
59 |
+
if rightward_matches[-1] != match:
|
60 |
+
horizontal_transitions.append([
|
61 |
+
"right",
|
62 |
+
f"{skill_code}_G{match}",
|
63 |
+
f"{skill_code}_G{match+1}"
|
64 |
+
])
|
65 |
+
|
66 |
+
leftward_matches = []
|
67 |
+
for i in reversed(range(9)):
|
68 |
+
current_grade = i
|
69 |
+
if row[current_grade].lower().strip() == 'x':
|
70 |
+
leftward_matches.append(i)
|
71 |
+
|
72 |
+
for match in leftward_matches:
|
73 |
+
if leftward_matches[0] != match:
|
74 |
+
horizontal_transitions.append([
|
75 |
+
"left",
|
76 |
+
f"{skill_code}_G{match}",
|
77 |
+
f"{skill_code}_G{match-1}"
|
78 |
+
])
|
79 |
+
|
80 |
+
return horizontal_transitions
|
81 |
+
|
82 |
+
|
83 |
+
def gather_all_vertical_matches(script_df):
|
84 |
+
""" Build a list of transitional relationships within a grade level across skills
|
85 |
+
|
86 |
+
Inputs
|
87 |
+
- script_df: pandas dataframe - an overview of the curriculum skills by grade level
|
88 |
+
|
89 |
+
Output
|
90 |
+
- all_matches: array of arrays - represents skills at each grade level
|
91 |
+
|
92 |
+
>>> script_df = read_and_preprocess_spreadsheet('curriculum_framework_for_tests.xlsx')
|
93 |
+
>>> gather_all_vertical_matches(script_df)
|
94 |
+
[['N1.1.1', '1'], ['N1.1.2', '1'], ['N1.1.1', '2'], ['N1.1.2', '2'], ['N1.1.1', '3'], ['N1.1.2', '3'], ['N1.1.1', '4'], ['N1.1.2', '4'], ['N1.1.1', '5'], ['N1.1.2', '5'], ['N1.1.1', '6'], ['N1.1.2', '6']]
|
95 |
+
"""
|
96 |
+
all_matches = []
|
97 |
+
columns = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
|
98 |
+
|
99 |
+
for column in columns:
|
100 |
+
for index, value in script_df[column].iteritems():
|
101 |
+
row_num = index + 1
|
102 |
+
if value == 'x':
|
103 |
+
# Extract skill code
|
104 |
+
skill_code = extract_skill_code(
|
105 |
+
script_df['Knowledge or Skill'][row_num-1]
|
106 |
+
)
|
107 |
+
|
108 |
+
all_matches.append([skill_code, column])
|
109 |
+
return all_matches
|
110 |
+
|
111 |
+
|
112 |
+
def build_vertical_transitions(script_df):
|
113 |
+
""" Build a list of transitional relationships within a grade level across skills
|
114 |
+
|
115 |
+
Inputs
|
116 |
+
- script_df: pandas dataframe - an overview of the curriculum skills by grade level
|
117 |
+
|
118 |
+
Output
|
119 |
+
- vertical_transitions: array of arrays - transition data with label, from state, and to state
|
120 |
+
|
121 |
+
>>> script_df = read_and_preprocess_spreadsheet('curriculum_framework_for_tests.xlsx')
|
122 |
+
>>> build_vertical_transitions(script_df)
|
123 |
+
[['down', 'N1.1.1_G1', 'N1.1.2_G1'], ['down', 'N1.1.2_G1', 'N1.1.1_G1'], ['down', 'N1.1.1_G2', 'N1.1.2_G2'], ['down', 'N1.1.2_G2', 'N1.1.1_G2'], ['down', 'N1.1.1_G3', 'N1.1.2_G3'], ['down', 'N1.1.2_G3', 'N1.1.1_G3'], ['down', 'N1.1.1_G4', 'N1.1.2_G4'], ['down', 'N1.1.2_G4', 'N1.1.1_G4'], ['down', 'N1.1.1_G5', 'N1.1.2_G5'], ['down', 'N1.1.2_G5', 'N1.1.1_G5'], ['down', 'N1.1.1_G6', 'N1.1.2_G6'], ['up', 'N1.1.2_G6', 'N1.1.1_G6'], ['up', 'N1.1.1_G6', 'N1.1.2_G6'], ['up', 'N1.1.2_G5', 'N1.1.1_G5'], ['up', 'N1.1.1_G5', 'N1.1.2_G5'], ['up', 'N1.1.2_G4', 'N1.1.1_G4'], ['up', 'N1.1.1_G4', 'N1.1.2_G4'], ['up', 'N1.1.2_G3', 'N1.1.1_G3'], ['up', 'N1.1.1_G3', 'N1.1.2_G3'], ['up', 'N1.1.2_G2', 'N1.1.1_G2'], ['up', 'N1.1.1_G2', 'N1.1.2_G2'], ['up', 'N1.1.2_G1', 'N1.1.1_G1']]
|
124 |
+
"""
|
125 |
+
vertical_transitions = []
|
126 |
+
|
127 |
+
all_matches = gather_all_vertical_matches(script_df)
|
128 |
+
|
129 |
+
# Downward
|
130 |
+
for index, match in enumerate(all_matches):
|
131 |
+
skill = match[0]
|
132 |
+
row_num = match[1]
|
133 |
+
if all_matches[-1] != match:
|
134 |
+
vertical_transitions.append([
|
135 |
+
"down",
|
136 |
+
f"{skill}_G{row_num}",
|
137 |
+
f"{all_matches[index+1][0]}_G{row_num}"
|
138 |
+
])
|
139 |
+
|
140 |
+
# Upward
|
141 |
+
for index, match in reversed(list(enumerate(all_matches))):
|
142 |
+
skill = match[0]
|
143 |
+
row_num = match[1]
|
144 |
+
if all_matches[0] != match:
|
145 |
+
vertical_transitions.append([
|
146 |
+
"up",
|
147 |
+
f"{skill}_G{row_num}",
|
148 |
+
f"{all_matches[index-1][0]}_G{row_num}"
|
149 |
+
])
|
150 |
+
|
151 |
+
return vertical_transitions
|
152 |
+
|
153 |
+
|
154 |
+
def build_all_states(all_transitions):
|
155 |
+
""" Creates an array with all state labels for the curriculum
|
156 |
+
|
157 |
+
Input
|
158 |
+
- all_transitions: list of lists - all possible up, down, left, or right transitions in curriculum
|
159 |
+
|
160 |
+
Output
|
161 |
+
- all_states: list - a collection of state labels (skill code and grade number)
|
162 |
+
|
163 |
+
>>> all_transitions = [['right', 'N1.1.1_G1', 'N1.1.1_G2'], ['right', 'N1.1.1_G2', 'N1.1.1_G3'], ['right', 'N1.1.1_G3', 'N1.1.1_G4'], ['right', 'N1.1.1_G4', 'N1.1.1_G5'], ['right', 'N1.1.1_G5', 'N1.1.1_G6'], ['left', 'N1.1.1_G6', 'N1.1.1_G5'], ['left', 'N1.1.1_G5', 'N1.1.1_G4'], ['left', 'N1.1.1_G4', 'N1.1.1_G3'], ['left', 'N1.1.1_G3', 'N1.1.1_G2'], ['left', 'N1.1.1_G2', 'N1.1.1_G1'], ['right', 'N1.1.2_G1', 'N1.1.2_G2'], ['right', 'N1.1.2_G2', 'N1.1.2_G3'], ['right', 'N1.1.2_G3', 'N1.1.2_G4'], ['right', 'N1.1.2_G4', 'N1.1.2_G5'], ['right', 'N1.1.2_G5', 'N1.1.2_G6'], ['left', 'N1.1.2_G6', 'N1.1.2_G5'], ['left', 'N1.1.2_G5', 'N1.1.2_G4'], ['left', 'N1.1.2_G4', 'N1.1.2_G3'], ['left', 'N1.1.2_G3', 'N1.1.2_G2'], ['left', 'N1.1.2_G2', 'N1.1.2_G1'], ['down', 'N1.1.1_G1', 'N1.1.2_G1'], ['down', 'N1.1.2_G1', 'N1.1.1_G1'], ['down', 'N1.1.1_G2', 'N1.1.2_G2'], ['down', 'N1.1.2_G2', 'N1.1.1_G2'], ['down', 'N1.1.1_G3', 'N1.1.2_G3'], ['down', 'N1.1.2_G3', 'N1.1.1_G3'], ['down', 'N1.1.1_G4', 'N1.1.2_G4'], ['down', 'N1.1.2_G4', 'N1.1.1_G4'], ['down', 'N1.1.1_G5', 'N1.1.2_G5'], ['down', 'N1.1.2_G5', 'N1.1.1_G5'], ['down', 'N1.1.1_G6', 'N1.1.2_G6'], ['up', 'N1.1.2_G6', 'N1.1.1_G6'], ['up', 'N1.1.1_G6', 'N1.1.2_G6'], ['up', 'N1.1.2_G5', 'N1.1.1_G5'], ['up', 'N1.1.1_G5', 'N1.1.2_G5'], ['up', 'N1.1.2_G4', 'N1.1.1_G4'], ['up', 'N1.1.1_G4', 'N1.1.2_G4'], ['up', 'N1.1.2_G3', 'N1.1.1_G3'], ['up', 'N1.1.1_G3', 'N1.1.2_G3'], ['up', 'N1.1.2_G2', 'N1.1.1_G2'], ['up', 'N1.1.1_G2', 'N1.1.2_G2'], ['up', 'N1.1.2_G1', 'N1.1.1_G1']]
|
164 |
+
>>> build_all_states(all_transitions)
|
165 |
+
['N1.1.1_G1', 'N1.1.1_G2', 'N1.1.1_G3', 'N1.1.1_G4', 'N1.1.1_G5', 'N1.1.1_G6', 'N1.1.2_G1', 'N1.1.2_G2', 'N1.1.2_G3', 'N1.1.2_G4', 'N1.1.2_G5', 'N1.1.2_G6']
|
166 |
+
"""
|
167 |
+
all_states = []
|
168 |
+
for transition in all_transitions:
|
169 |
+
for index, state in enumerate(transition):
|
170 |
+
if index == 0:
|
171 |
+
continue
|
172 |
+
if state not in all_states:
|
173 |
+
all_states.append(state)
|
174 |
+
return all_states
|
175 |
+
|
176 |
+
|
177 |
+
def build_curriculum_logic():
|
178 |
+
script_df = read_and_preprocess_spreadsheet('Rori_Framework_v1.xlsx')
|
179 |
+
horizontal_transitions = build_horizontal_transitions(script_df)
|
180 |
+
vertical_transitions = build_vertical_transitions(script_df)
|
181 |
+
all_transitions = horizontal_transitions + vertical_transitions
|
182 |
+
all_states = build_all_states(all_transitions)
|
183 |
+
return all_states, all_transitions
|
mathtext_fastapi/data/Rori_Framework_v1.xlsx
ADDED
Binary file (420 kB). View file
|
|
mathtext_fastapi/data/curriculum_framework_for_tests.xlsx
ADDED
Binary file (510 kB). View file
|
|
mathtext_fastapi/data/intent_classification_model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ea4954368c3b95673167ce347f2962b5508c4af295b6af58b6c11b3c1075b42e
|
3 |
+
size 127903
|
mathtext_fastapi/data/labeled_data.csv
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Utterance,Label
|
2 |
+
skip this,skip
|
3 |
+
this is stupid,skip
|
4 |
+
this is stupid,harder
|
5 |
+
this is stupid,feedback
|
6 |
+
I'm done,exit
|
7 |
+
quit,exit
|
8 |
+
I don't know,hint
|
9 |
+
help,hint
|
10 |
+
can I do something else?,main menu
|
11 |
+
what's going on,rapport
|
12 |
+
what's going on,main menu
|
13 |
+
tell me a joke,rapport
|
14 |
+
tell me a joke,main menu
|
15 |
+
Sorry I don't understand,do not know
|
16 |
+
Ten thousand,number
|
17 |
+
1.234,number
|
18 |
+
"10,000",number
|
19 |
+
"123, 456",numbers
|
20 |
+
"11, 12, 13",numbers
|
21 |
+
"100, 200, 300",numbers
|
22 |
+
"100, 200",numbers
|
23 |
+
Stop for a minute,wait
|
24 |
+
Bye bye,exit
|
25 |
+
Good night,exit
|
26 |
+
Am done,exit
|
27 |
+
Yes,yes
|
28 |
+
Help,help
|
29 |
+
Idiot,harder
|
30 |
+
Stop,exit
|
31 |
+
I don't get it,hint
|
32 |
+
Math,main menu
|
33 |
+
Math,math topic
|
34 |
+
Tomorrow let do math,wait
|
35 |
+
Later,wait
|
36 |
+
Pls i will continue pls,skip
|
37 |
+
Rori tell me now,help
|
38 |
+
harder,skip
|
39 |
+
Stop for now i wont to go to School,exit
|
40 |
+
Next,next
|
41 |
+
Okay,okay
|
42 |
+
Great,affirmation
|
43 |
+
Give me for example,example
|
44 |
+
No I want to learn algebraic expressions,algebra
|
45 |
+
Hi rori,greeting
|
46 |
+
*help*,help
|
47 |
+
*Next*,next
|
48 |
+
Okay nice,okay
|
49 |
+
I don't know it,hint
|
50 |
+
Nex,next
|
51 |
+
I need a help,hint
|
52 |
+
Please can I ask your any math questions?,faq
|
53 |
+
The answer is 1,answer
|
54 |
+
The answer is 1,number
|
55 |
+
But 0.8 is also same as . 8 so I was actually right,I'm right
|
56 |
+
What is the number system?,faq
|
57 |
+
Ok thanks,thanks
|
58 |
+
I'm going to school now,exit
|
59 |
+
Let's move to another topic,main menu
|
60 |
+
"Ummanni saba
|
61 |
+
Kebena bara kana galmi keenya inni guddaan bilisummaa qofa #Gabrummaan_ammaan booda_gaha namni hundi bakka jiru irraa kutatee ka,ee jira obboleewwan goototni keenya jiran haqa Kebenaaf jechaa jiru Guraandhala 29 booda walabummaa keenya labsina Dhugaa qabna Ni injifanna *** . Naannoo giddu galeessa Itoophiyaatti #Kebenaan aanaa addaati Kun murtoo ummata Kebenaa hundaati",spam
|
62 |
+
Yes it,yes
|
63 |
+
U type fast,too fast
|
64 |
+
I mean your typing is fast,too fast
|
65 |
+
Why do u type so fast,too fast
|
66 |
+
Ur typing is fast,too fast
|
67 |
+
Can we go to a real work,harder
|
68 |
+
I know all this,harder
|
69 |
+
Answer this,preamble
|
70 |
+
Am tired,exit
|
71 |
+
This is not what I asked for,main menu
|
72 |
+
Bye,exit
|
73 |
+
😱😱😂😂😂😡😰😰😰😒,spam
|
74 |
+
Gbxbxbcbcbbcbchcbchc,spam
|
75 |
+
I want to solve math,math topic
|
76 |
+
Pleas let start with the fraction,fractions topic
|
77 |
+
Okey,okay
|
78 |
+
i need substraction,subtraction topic
|
79 |
+
Can you please stop with me,exit
|
80 |
+
Another one,next
|
81 |
+
Harder or easy,main menu
|
82 |
+
Hard or easier,main menu
|
83 |
+
Jump topic,menu
|
84 |
+
Got it,okay
|
85 |
+
I didn't understand,don't know
|
86 |
+
Don't understand,don't know
|
87 |
+
Excuse me pls,hint
|
88 |
+
Let stop for today,exit
|
89 |
+
Help and stop asking me stupid questions,
|
90 |
+
Ykay,okay
|
91 |
+
Not interested in solving this,menu
|
92 |
+
Stpo,exit
|
93 |
+
Hiiiiiii,greeting
|
94 |
+
Hi rori,greeting
|
95 |
+
I've done this things before,harder
|
96 |
+
Which number my phone number,
|
97 |
+
Unit,main menu
|
98 |
+
No ide,don't know
|
99 |
+
No ide,hint
|
100 |
+
No idea,don't know
|
101 |
+
🙈🤩😇🙏,spam
|
102 |
+
Thank u,thanks
|
103 |
+
Do you know programming,faq
|
104 |
+
Delete my number,unsubscribe
|
105 |
+
See u,exit
|
106 |
+
Can I go for break ??,wait
|
107 |
+
I wanna fuck,profanity
|
108 |
+
Enough of this nw,exit
|
109 |
+
Can we move to equations,equations
|
110 |
+
Do you know you are an idiot,insult
|
111 |
+
3 digit number,number
|
112 |
+
3 digit number,answer
|
113 |
+
Three digit number,confident answer
|
114 |
+
Three digit number,number
|
115 |
+
Good evening Rori,greeting
|
116 |
+
89 Next,answer
|
117 |
+
89 Next,number
|
118 |
+
3 digit number,answer
|
119 |
+
Three digit number,answer
|
120 |
+
This is too simple,harder
|
121 |
+
Am not a kid,harder
|
122 |
+
Hey Miss Roribcan you ask me some question from Secondary 2,greeting
|
123 |
+
Hey Miss Roribcan you ask me some question from Secondary 2,faq
|
124 |
+
Hey Miss Roribcan you ask me some question from Secondary 2,main menu
|
125 |
+
don't know,hint
|
126 |
+
don't know,easier
|
127 |
+
𝑴𝒂𝒕𝒉,math
|
128 |
+
Rori can you help me to gat value,
|
129 |
+
I called but u are not picking up,
|
130 |
+
0.3 answer,answer
|
131 |
+
Sorry rori was101,answer
|
132 |
+
Y is it 6,answer
|
133 |
+
Y is it 6,number
|
134 |
+
0.3 answer,number
|
135 |
+
Why 0.5,more explanation
|
136 |
+
Why 0.5,number
|
137 |
+
6\nNext,Next
|
138 |
+
How is the answer is 11,more explanation
|
139 |
+
How comes we have 11,more explanation
|
140 |
+
Yes 6,answer
|
141 |
+
Yes 6,number
|
142 |
+
6\nNext,number
|
143 |
+
How is the answer is 11,number
|
144 |
+
How comes we have 11,number
|
mathtext_fastapi/data/text2int_results.csv
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
input,output,text2int,score
|
2 |
+
notanumber,32202.0,32202.0,True
|
3 |
+
this is not a number,32202.0,32202.0,True
|
4 |
+
fourteen,14.0,14.0,True
|
5 |
+
forteen,14.0,14.0,True
|
6 |
+
one thousand four hundred ninety two,1492.0,1492.0,True
|
7 |
+
one thousand ninety two,1092.0,1092.0,True
|
8 |
+
Fourteen Hundred Ninety-Two,1492.0,1492.0,True
|
9 |
+
Fourteen Hundred,1400.0,1400.0,True
|
10 |
+
Ninety nine,99.0,99.0,True
|
11 |
+
fifteen thousand five hundred-sixty,15560.0,15560.0,True
|
12 |
+
three hundred fifty,350.0,350.0,True
|
13 |
+
one nine eight five,1985.0,1985.0,True
|
14 |
+
nineteen eighty-five,1985.0,1605.0,False
|
15 |
+
oh one,1.0,1.0,True
|
16 |
+
six oh 1,601.0,601.0,True
|
17 |
+
sex,6.0,6.0,True
|
18 |
+
six,6.0,6.0,True
|
19 |
+
eight oh,80.0,8.0,False
|
20 |
+
eighty,80.0,80.0,True
|
21 |
+
ate,8.0,1.0,False
|
22 |
+
double eight,88.0,8.0,False
|
23 |
+
<<<<<<< HEAD
|
24 |
+
eight three seven five three O nine,8375309.0,8375329.0,False
|
25 |
+
eight three seven five three oh nine,8375309.0,8375309.0,True
|
26 |
+
eight three seven five three zero nine,8375309.0,8375309.0,True
|
27 |
+
eight three seven five three oh ni-ee-ine,8375309.0,837530619.0,False
|
28 |
+
=======
|
29 |
+
eight three seven five three O nine,8375309.0,8375319.0,False
|
30 |
+
eight three seven five three oh nine,8375309.0,8375309.0,True
|
31 |
+
eight three seven five three zero nine,8375309.0,8375309.0,True
|
32 |
+
eight three seven five three oh ni-ee-ine,8375309.0,837530111.0,False
|
33 |
+
>>>>>>> feature-wormhole
|
34 |
+
two eight,28.0,16.0,False
|
35 |
+
seven oh eleven,7011.0,77.0,False
|
36 |
+
seven elevens,77.0,77.0,True
|
37 |
+
seven eleven,711.0,77.0,False
|
38 |
+
ninety nine oh five,9905.0,149.0,False
|
39 |
+
seven 0 seven 0 seven 0 seven,7070707.0,7070707.0,True
|
40 |
+
123 hundred,123000.0,223.0,False
|
41 |
+
<<<<<<< HEAD
|
42 |
+
5 o 5,505.0,525.0,False
|
43 |
+
15 o 5,1505.0,22.0,False
|
44 |
+
15-o 5,1505.0,22.0,False
|
45 |
+
15 o-5,1505.0,22.0,False
|
46 |
+
=======
|
47 |
+
5 o 5,505.0,515.0,False
|
48 |
+
15 o 5,1505.0,21.0,False
|
49 |
+
15-o 5,1505.0,21.0,False
|
50 |
+
15 o-5,1505.0,21.0,False
|
51 |
+
>>>>>>> feature-wormhole
|
52 |
+
911-thousand,911000.0,911000.0,True
|
53 |
+
twenty-two twenty-two,2222.0,44.0,False
|
54 |
+
twenty-two twenty-twos,484.0,44.0,False
|
55 |
+
four eighty four,484.0,404.0,False
|
56 |
+
four eighties,320.0,72.0,False
|
57 |
+
four eighties and nine nineties,1130.0,243.0,False
|
58 |
+
ninety nine hundred and seventy seven,9977.0,276.0,False
|
59 |
+
seven thousands,7000.0,7000.0,True
|
60 |
+
2 hundreds,200.0,200.0,True
|
61 |
+
99 thousands and one,99001.0,99001.0,True
|
62 |
+
"forty-five thousand, seven hundred and nine",45709.0,1161.0,False
|
63 |
+
eighty eight hundred eighty,8880.0,268.0,False
|
64 |
+
a hundred hundred,10000.0,100.0,False
|
65 |
+
a hundred thousand,100000.0,100.0,False
|
66 |
+
a hundred million,100000000.0,100.0,False
|
67 |
+
nineteen ninety nine,1999.0,1809.0,False
|
68 |
+
forteen twenty seven,1427.0,307.0,False
|
69 |
+
seventeen-thousand and seventy two,17072.0,17072.0,True
|
70 |
+
two hundred and nine,209.0,209.0,True
|
71 |
+
two thousand ten,2010.0,2010.0,True
|
72 |
+
two thousand and ten,2010.0,2010.0,True
|
73 |
+
twelve million,12000000.0,12000000.0,True
|
74 |
+
8 billion,8000000000.0,8000000000.0,True
|
75 |
+
twenty ten,2010.0,2010.0,True
|
76 |
+
thirty-two hundred,3200.0,3200.0,True
|
77 |
+
nine,9.0,9.0,True
|
78 |
+
forty two,42.0,42.0,True
|
79 |
+
1 2 three,123.0,123.0,True
|
80 |
+
fourtean,14.0,14.0,True
|
81 |
+
one tousand four hundred ninty two,1492.0,1492.0,True
|
82 |
+
Furteen Hundrd Ninety-Too,1492.0,1492.0,True
|
83 |
+
forrteen,14.0,14.0,True
|
84 |
+
sevnteen-thosand and seventy two,17072.0,17072.0,True
|
85 |
+
ninety nine hundred ad seventy seven,9977.0,90.0,False
|
86 |
+
seven thusands,7000.0,7000.0,True
|
87 |
+
2 hunreds,200.0,200.0,True
|
88 |
+
99 tousands and one,99001.0,99001.0,True
|
89 |
+
eighty ate hundred eighty,8880.0,261.0,False
|
90 |
+
fourteen Hundred,1400.0,1400.0,True
|
91 |
+
8 Bilion,8000000000.0,8000000.0,False
|
92 |
+
one million three thousand one,1003001.0,1003001.0,True
|
93 |
+
four million nine thousand seven,4009007.0,4009007.0,True
|
94 |
+
two million five hundred thousand,2500000.0,2001500.0,False
|
95 |
+
two tousand ten,2010.0,2010.0,True
|
96 |
+
two thousand teen,2010.0,2007.0,False
|
97 |
+
tvelve milion,12000000.0,12000000.0,True
|
98 |
+
tventy ten,2010.0,2010.0,True
|
99 |
+
tirty-twoo hunred,3200.0,3200.0,True
|
100 |
+
sevn thoosands,7000.0,7000.0,True
|
101 |
+
five,5.0,5.0,True
|
102 |
+
ten,10.0,10.0,True
|
103 |
+
one two three and ten,12310.0,51.0,False
|
104 |
+
ONE MILLion three hunded and fiv,1000305.0,1000305.0,True
|
105 |
+
"50,500 and six",50506.0,50506.0,True
|
106 |
+
one_million_and_five,1000005.0,1000005.0,True
|
107 |
+
2.0,2.0,2.0,True
|
108 |
+
4.5,4.5,4.5,True
|
109 |
+
12345.001,12345.001,12345.001,True
|
110 |
+
7..0,7.0,7.0,True
|
111 |
+
0.06,0.06,0.06,True
|
112 |
+
"0,25",0.25,25.0,False
|
113 |
+
o.45,0.45,32202.0,False
|
114 |
+
0.1.2,0.12,32202.0,False
|
115 |
+
0.00009,9e-05,9e-05,True
|
116 |
+
0.01.,0.01,0.01,True
|
117 |
+
I don't know 8,8.0,8.0,True
|
118 |
+
"You're wrong it's not 20, it's 45",45.0,20.0,False
|
119 |
+
I don't understand why it's 19,19.0,19.0,True
|
mathtext_fastapi/global_state_manager.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transitions import Machine
|
2 |
+
from mathtext_fastapi.curriculum_mapper import build_curriculum_logic
|
3 |
+
|
4 |
+
all_states, all_transitions = build_curriculum_logic()
|
5 |
+
|
6 |
+
class GlobalStateManager(object):
|
7 |
+
states = all_states
|
8 |
+
|
9 |
+
transitions = all_transitions
|
10 |
+
|
11 |
+
def __init__(
|
12 |
+
self,
|
13 |
+
initial_state='N1.1.1_G1',
|
14 |
+
):
|
15 |
+
self.machine = Machine(
|
16 |
+
model=self,
|
17 |
+
states=GlobalStateManager.states,
|
18 |
+
transitions=GlobalStateManager.transitions,
|
19 |
+
initial=initial_state
|
20 |
+
)
|
21 |
+
|
22 |
+
|
23 |
+
curriculum = GlobalStateManager()
|
mathtext_fastapi/intent_classification.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
from pathlib import Path
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
from sklearn.linear_model import LogisticRegression
|
7 |
+
from joblib import dump, load
|
8 |
+
|
9 |
+
def pickle_model(model):
|
10 |
+
DATA_DIR = Path(__file__).parent.parent / "mathtext_fastapi" / "data" / "intent_classification_model.joblib"
|
11 |
+
dump(model, DATA_DIR)
|
12 |
+
|
13 |
+
|
14 |
+
def create_intent_classification_model():
|
15 |
+
encoder = SentenceTransformer('all-MiniLM-L6-v2')
|
16 |
+
# path = list(Path.cwd().glob('*.csv'))
|
17 |
+
DATA_DIR = Path(__file__).parent.parent / "mathtext_fastapi" / "data" / "labeled_data.csv"
|
18 |
+
|
19 |
+
print("DATA_DIR")
|
20 |
+
print(f"{DATA_DIR}")
|
21 |
+
|
22 |
+
with open(f"{DATA_DIR}",'r', newline='', encoding='utf-8') as f:
|
23 |
+
df = pd.read_csv(f)
|
24 |
+
df = df[df.columns[:2]]
|
25 |
+
df = df.dropna()
|
26 |
+
X_explore = np.array([list(encoder.encode(x)) for x in df['Utterance']])
|
27 |
+
X = np.array([list(encoder.encode(x)) for x in df['Utterance']])
|
28 |
+
y = df['Label']
|
29 |
+
model = LogisticRegression(class_weight='balanced')
|
30 |
+
model.fit(X, y, sample_weight=None)
|
31 |
+
|
32 |
+
print("MODEL")
|
33 |
+
print(model)
|
34 |
+
|
35 |
+
pickle_model(model)
|
36 |
+
|
37 |
+
|
38 |
+
def retrieve_intent_classification_model():
|
39 |
+
DATA_DIR = Path(__file__).parent.parent / "mathtext_fastapi" / "data" / "intent_classification_model.joblib"
|
40 |
+
model = load(DATA_DIR)
|
41 |
+
return model
|
42 |
+
|
43 |
+
|
44 |
+
def predict_message_intent(message):
|
45 |
+
encoder = SentenceTransformer('all-MiniLM-L6-v2')
|
46 |
+
model = retrieve_intent_classification_model()
|
47 |
+
tokenized_utterance = np.array([list(encoder.encode(message))])
|
48 |
+
predicted_label = model.predict(tokenized_utterance)
|
49 |
+
predicted_probabilities = model.predict_proba(tokenized_utterance)
|
50 |
+
confidence_score = predicted_probabilities.max()
|
51 |
+
|
52 |
+
return {"type": "intent", "data": predicted_label[0], "confidence": confidence_score}
|
mathtext_fastapi/logging.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from datetime import datetime
|
3 |
+
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from supabase import create_client
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
SUPA = create_client(
|
10 |
+
os.environ.get('SUPABASE_URL'),
|
11 |
+
os.environ.get('SUPABASE_KEY')
|
12 |
+
)
|
13 |
+
|
14 |
+
|
15 |
+
def log_message_data_through_supabase_api(table_name, log_data):
|
16 |
+
return SUPA.table(table_name).insert(log_data).execute()
|
17 |
+
|
18 |
+
|
19 |
+
def format_datetime_in_isoformat(dt):
|
20 |
+
return getattr(dt.now(), 'isoformat', lambda x: None)()
|
21 |
+
|
22 |
+
|
23 |
+
def get_or_create_supabase_entry(table_name, insert_data, check_variable=None):
|
24 |
+
""" Checks if project or contact exists and adds entry if not found
|
25 |
+
|
26 |
+
Input:
|
27 |
+
- table_name: str- the name of the table in Supabase that is being examined
|
28 |
+
- insert_data: json - the data to insert
|
29 |
+
- check_variable: str/None - the specific field to check for existing match
|
30 |
+
|
31 |
+
Result
|
32 |
+
- logged_data - an object with the Supabase data
|
33 |
+
"""
|
34 |
+
if table_name == 'contact':
|
35 |
+
resp = SUPA.table('contact').select("*").eq(
|
36 |
+
"original_contact_id",
|
37 |
+
insert_data['original_contact_id']
|
38 |
+
).eq(
|
39 |
+
"project",
|
40 |
+
insert_data['project']
|
41 |
+
).execute()
|
42 |
+
else:
|
43 |
+
resp = SUPA.table(table_name).select("*").eq(
|
44 |
+
check_variable,
|
45 |
+
insert_data[check_variable]
|
46 |
+
).execute()
|
47 |
+
|
48 |
+
if len(resp.data) == 0:
|
49 |
+
logged_data = log_message_data_through_supabase_api(
|
50 |
+
table_name,
|
51 |
+
insert_data
|
52 |
+
)
|
53 |
+
else:
|
54 |
+
logged_data = resp
|
55 |
+
return logged_data
|
56 |
+
|
57 |
+
|
58 |
+
def prepare_message_data_for_logging(message_data, nlu_response):
|
59 |
+
""" Builds objects for each table and logs them to the database
|
60 |
+
|
61 |
+
Input:
|
62 |
+
- message_data: an object with the full message data from Turn.io/Whatsapp
|
63 |
+
"""
|
64 |
+
project_data = {
|
65 |
+
'name': "Rori",
|
66 |
+
# Autogenerated fields: id, created_at, modified_at
|
67 |
+
}
|
68 |
+
project_data_log = get_or_create_supabase_entry(
|
69 |
+
'project',
|
70 |
+
project_data,
|
71 |
+
'name'
|
72 |
+
)
|
73 |
+
|
74 |
+
contact_data = {
|
75 |
+
'project': project_data_log.data[0]['id'], # FK
|
76 |
+
'original_contact_id': message_data['contact_uuid'],
|
77 |
+
'urn': "",
|
78 |
+
'language_code': "en",
|
79 |
+
'contact_inserted_at': format_datetime_in_isoformat(datetime.now())
|
80 |
+
# Autogenerated fields: id, created_at, modified_at
|
81 |
+
}
|
82 |
+
contact_data_log = get_or_create_supabase_entry('contact', contact_data)
|
83 |
+
|
84 |
+
del message_data['author_id']
|
85 |
+
|
86 |
+
message_data = {
|
87 |
+
'contact': contact_data_log.data[0]['id'], # FK
|
88 |
+
'original_message_id': message_data['message_id'],
|
89 |
+
'text': message_data['message_body'],
|
90 |
+
'direction': message_data['message_direction'],
|
91 |
+
'sender_type': message_data['author_type'],
|
92 |
+
'channel_type': "whatsapp / turn.io",
|
93 |
+
'message_inserted_at': message_data['message_inserted_at'],
|
94 |
+
'message_modified_at': message_data['message_updated_at'],
|
95 |
+
'message_sent_at': format_datetime_in_isoformat(datetime.now()),
|
96 |
+
'nlu_response': nlu_response,
|
97 |
+
'request_object': message_data
|
98 |
+
# Autogenerated fields: created_at, modified_at
|
99 |
+
}
|
100 |
+
message_data_log = log_message_data_through_supabase_api(
|
101 |
+
'message',
|
102 |
+
message_data
|
103 |
+
)
|
mathtext_fastapi/math_quiz_fsm.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
from transitions import Machine
|
3 |
+
|
4 |
+
|
5 |
+
class MathQuizFSM(object):
|
6 |
+
states = [
|
7 |
+
'quiz_start',
|
8 |
+
'quiz_question',
|
9 |
+
'quiz_end'
|
10 |
+
]
|
11 |
+
|
12 |
+
transitions = [
|
13 |
+
['ask_second_question', 'quiz_start', 'quiz_question'],
|
14 |
+
['ask_next_question', 'quiz_question', 'quiz_question'],
|
15 |
+
['exit', 'quiz_start', 'quiz_end'],
|
16 |
+
['exit', 'quiz_question', 'quiz_end'],
|
17 |
+
]
|
18 |
+
|
19 |
+
def __init__(
|
20 |
+
self,
|
21 |
+
initial_state='quiz_start',
|
22 |
+
question_nums=[2, 3],
|
23 |
+
initial_student_answer=0,
|
24 |
+
):
|
25 |
+
# Instantiate the FSM
|
26 |
+
self.machine = Machine(
|
27 |
+
model=self,
|
28 |
+
states=MathQuizFSM.states,
|
29 |
+
transitions=MathQuizFSM.transitions,
|
30 |
+
initial=initial_state
|
31 |
+
)
|
32 |
+
|
33 |
+
# Instantiate variables necessary for tracking activity
|
34 |
+
self.question_nums = question_nums
|
35 |
+
self.correct_answer = self.question_nums[0] + self.question_nums[1]
|
36 |
+
self.student_answer = initial_student_answer
|
37 |
+
self.is_correct_answer = False
|
38 |
+
self.response_text = f"What is {self.question_nums[0]} + {self.question_nums[1]}?"
|
39 |
+
|
40 |
+
# Define functions to run on transitions
|
41 |
+
self.machine.on_enter_quiz_question('generate_math_problem')
|
42 |
+
self.machine.on_exit_quiz_question('validate_answer')
|
43 |
+
|
44 |
+
def validate_answer(self):
|
45 |
+
if self.student_answer == 'exit':
|
46 |
+
self.machine.set_state('quiz_end')
|
47 |
+
return ["Come back any time!"]
|
48 |
+
elif self.correct_answer == self.student_answer:
|
49 |
+
self.machine.set_state('quiz_question')
|
50 |
+
self.generate_math_problem()
|
51 |
+
return ['Great job!', self.response_text]
|
52 |
+
else:
|
53 |
+
return ["That's not quite right. Try again.", self.response_text]
|
54 |
+
|
55 |
+
def generate_math_problem(self):
|
56 |
+
self.question_nums = random.sample(range(1,100),2)
|
57 |
+
self.response_text = f"What is {self.question_nums[0]} + {self.question_nums[1]}"
|
58 |
+
self.correct_answer = self.question_nums[0] + self.question_nums[1]
|
mathtext_fastapi/math_subtraction_fsm.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
from transitions import Machine
|
3 |
+
|
4 |
+
|
5 |
+
class MathSubtractionFSM(object):
|
6 |
+
states = [
|
7 |
+
'quiz_start',
|
8 |
+
'quiz_question',
|
9 |
+
'quiz_end'
|
10 |
+
]
|
11 |
+
|
12 |
+
transitions = [
|
13 |
+
['ask_second_question', 'quiz_start', 'quiz_question'],
|
14 |
+
['ask_next_question', 'quiz_question', 'quiz_question'],
|
15 |
+
['exit', 'quiz_start', 'quiz_end'],
|
16 |
+
['exit', 'quiz_question', 'quiz_end'],
|
17 |
+
]
|
18 |
+
|
19 |
+
def __init__(
|
20 |
+
self,
|
21 |
+
initial_state='quiz_start',
|
22 |
+
question_nums=[4, 3],
|
23 |
+
initial_student_answer=0,
|
24 |
+
):
|
25 |
+
# Instantiate the FSM
|
26 |
+
self.machine = Machine(
|
27 |
+
model=self,
|
28 |
+
states=MathSubtractionFSM.states,
|
29 |
+
transitions=MathSubtractionFSM.transitions,
|
30 |
+
initial=initial_state
|
31 |
+
)
|
32 |
+
|
33 |
+
# Instantiate variables necessary for tracking activity
|
34 |
+
self.question_nums = question_nums
|
35 |
+
self.correct_answer = self.question_nums[0] - self.question_nums[1]
|
36 |
+
self.student_answer = initial_student_answer
|
37 |
+
self.is_correct_answer = False
|
38 |
+
self.response_text = f"What is {self.question_nums[0]} - {self.question_nums[1]}?"
|
39 |
+
|
40 |
+
# Define functions to run on transitions
|
41 |
+
self.machine.on_enter_quiz_question('generate_math_problem')
|
42 |
+
self.machine.on_exit_quiz_question('validate_answer')
|
43 |
+
|
44 |
+
def validate_answer(self):
|
45 |
+
if self.student_answer == 'exit':
|
46 |
+
self.machine.set_state('quiz_end')
|
47 |
+
return ["Come back any time!"]
|
48 |
+
elif self.correct_answer == self.student_answer:
|
49 |
+
self.machine.set_state('quiz_question')
|
50 |
+
self.generate_math_problem()
|
51 |
+
return ['Great job!', self.response_text]
|
52 |
+
else:
|
53 |
+
return ["That's not quite right. Try again.", self.response_text]
|
54 |
+
|
55 |
+
def generate_math_problem(self):
|
56 |
+
self.question_nums = random.sample(range(1, 100), 2)
|
57 |
+
self.response_text = f"What is {self.question_nums[0]} - {self.question_nums[1]}"
|
58 |
+
self.correct_answer = self.question_nums[0] - self.question_nums[1]
|
mathtext_fastapi/nlu.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fuzzywuzzy import fuzz
|
2 |
+
from mathtext_fastapi.logging import prepare_message_data_for_logging
|
3 |
+
from mathtext.sentiment import sentiment
|
4 |
+
from mathtext.text2int import text2int
|
5 |
+
from mathtext_fastapi.intent_classification import create_intent_classification_model, retrieve_intent_classification_model, predict_message_intent
|
6 |
+
import re
|
7 |
+
|
8 |
+
|
9 |
+
def build_nlu_response_object(type, data, confidence):
|
10 |
+
""" Turns nlu results into an object to send back to Turn.io
|
11 |
+
Inputs
|
12 |
+
- type: str - the type of nlu run (integer or sentiment-analysis)
|
13 |
+
- data: str/int - the student message
|
14 |
+
- confidence: - the nlu confidence score (sentiment) or '' (integer)
|
15 |
+
|
16 |
+
>>> build_nlu_response_object('integer', 8, 0)
|
17 |
+
{'type': 'integer', 'data': 8, 'confidence': 0}
|
18 |
+
|
19 |
+
>>> build_nlu_response_object('sentiment', 'POSITIVE', 0.99)
|
20 |
+
{'type': 'sentiment', 'data': 'POSITIVE', 'confidence': 0.99}
|
21 |
+
"""
|
22 |
+
return {'type': type, 'data': data, 'confidence': confidence}
|
23 |
+
|
24 |
+
|
25 |
+
# def test_for_float_or_int(message_data, message_text):
|
26 |
+
# nlu_response = {}
|
27 |
+
# if type(message_text) == int or type(message_text) == float:
|
28 |
+
# nlu_response = build_nlu_response_object('integer', message_text, '')
|
29 |
+
# prepare_message_data_for_logging(message_data, nlu_response)
|
30 |
+
# return nlu_response
|
31 |
+
|
32 |
+
|
33 |
+
def test_for_number_sequence(message_text_arr, message_data, message_text):
|
34 |
+
""" Determines if the student's message is a sequence of numbers
|
35 |
+
|
36 |
+
>>> test_for_number_sequence(['1','2','3'], {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "I am tired", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}, '1, 2, 3')
|
37 |
+
{'type': 'integer', 'data': '1,2,3', 'confidence': 0}
|
38 |
+
|
39 |
+
>>> test_for_number_sequence(['a','b','c'], {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "I am tired", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}, 'a, b, c')
|
40 |
+
{}
|
41 |
+
"""
|
42 |
+
nlu_response = {}
|
43 |
+
if all(ele.isdigit() for ele in message_text_arr):
|
44 |
+
nlu_response = build_nlu_response_object(
|
45 |
+
'integer',
|
46 |
+
','.join(message_text_arr),
|
47 |
+
0
|
48 |
+
)
|
49 |
+
prepare_message_data_for_logging(message_data, nlu_response)
|
50 |
+
return nlu_response
|
51 |
+
|
52 |
+
|
53 |
+
def run_text2int_on_each_list_item(message_text_arr):
|
54 |
+
""" Attempts to convert each list item to an integer
|
55 |
+
|
56 |
+
Input
|
57 |
+
- message_text_arr: list - a set of text extracted from the student message
|
58 |
+
|
59 |
+
Output
|
60 |
+
- student_response_arr: list - a set of integers (32202 for error code)
|
61 |
+
|
62 |
+
>>> run_text2int_on_each_list_item(['1','2','3'])
|
63 |
+
[1, 2, 3]
|
64 |
+
"""
|
65 |
+
student_response_arr = []
|
66 |
+
for student_response in message_text_arr:
|
67 |
+
int_api_resp = text2int(student_response.lower())
|
68 |
+
student_response_arr.append(int_api_resp)
|
69 |
+
return student_response_arr
|
70 |
+
|
71 |
+
|
72 |
+
def run_sentiment_analysis(message_text):
|
73 |
+
""" Evaluates the sentiment of a student message
|
74 |
+
|
75 |
+
>>> run_sentiment_analysis("I am tired")
|
76 |
+
[{'label': 'NEGATIVE', 'score': 0.9997807145118713}]
|
77 |
+
|
78 |
+
>>> run_sentiment_analysis("I am full of joy")
|
79 |
+
[{'label': 'POSITIVE', 'score': 0.999882698059082}]
|
80 |
+
"""
|
81 |
+
# TODO: Add intent labelling here
|
82 |
+
# TODO: Add logic to determine whether intent labeling or sentiment analysis is more appropriate (probably default to intent labeling)
|
83 |
+
return sentiment(message_text)
|
84 |
+
|
85 |
+
|
86 |
+
def run_intent_classification(message_text):
|
87 |
+
""" Process a student's message using basic fuzzy text comparison
|
88 |
+
|
89 |
+
>>> run_intent_classification("exit")
|
90 |
+
{'type': 'intent', 'data': 'exit', 'confidence': 1.0}
|
91 |
+
>>> run_intent_classification("exi")
|
92 |
+
{'type': 'intent', 'data': 'exit', 'confidence': 0.86}
|
93 |
+
>>> run_intent_classification("eas")
|
94 |
+
{'type': 'intent', 'data': '', 'confidence': 0}
|
95 |
+
>>> run_intent_classification("hard")
|
96 |
+
{'type': 'intent', 'data': '', 'confidence': 0}
|
97 |
+
>>> run_intent_classification("hardier")
|
98 |
+
{'type': 'intent', 'data': 'harder', 'confidence': 0.92}
|
99 |
+
"""
|
100 |
+
label = ''
|
101 |
+
ratio = 0
|
102 |
+
nlu_response = {'type': 'intent', 'data': label, 'confidence': ratio}
|
103 |
+
commands = [
|
104 |
+
'easier',
|
105 |
+
'exit',
|
106 |
+
'harder',
|
107 |
+
'hint',
|
108 |
+
'next',
|
109 |
+
'stop',
|
110 |
+
]
|
111 |
+
|
112 |
+
for command in commands:
|
113 |
+
ratio = fuzz.ratio(command, message_text.lower())
|
114 |
+
if ratio > 80:
|
115 |
+
nlu_response['data'] = command
|
116 |
+
nlu_response['confidence'] = ratio / 100
|
117 |
+
|
118 |
+
return nlu_response
|
119 |
+
|
120 |
+
|
121 |
+
def evaluate_message_with_nlu(message_data):
|
122 |
+
""" Process a student's message using NLU functions and send the result
|
123 |
+
|
124 |
+
>>> evaluate_message_with_nlu({"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "8", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"})
|
125 |
+
{'type': 'integer', 'data': 8, 'confidence': 0}
|
126 |
+
|
127 |
+
>>> evaluate_message_with_nlu({"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "I am tired", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"})
|
128 |
+
{'type': 'sentiment', 'data': 'NEGATIVE', 'confidence': 0.9997807145118713}
|
129 |
+
"""
|
130 |
+
# Keeps system working with two different inputs - full and filtered @event object
|
131 |
+
try:
|
132 |
+
message_text = message_data['message_body']
|
133 |
+
except KeyError:
|
134 |
+
message_data = {
|
135 |
+
'author_id': message_data['message']['_vnd']['v1']['chat']['owner'],
|
136 |
+
'author_type': message_data['message']['_vnd']['v1']['author']['type'],
|
137 |
+
'contact_uuid': message_data['message']['_vnd']['v1']['chat']['contact_uuid'],
|
138 |
+
'message_body': message_data['message']['text']['body'],
|
139 |
+
'message_direction': message_data['message']['_vnd']['v1']['direction'],
|
140 |
+
'message_id': message_data['message']['id'],
|
141 |
+
'message_inserted_at': message_data['message']['_vnd']['v1']['chat']['inserted_at'],
|
142 |
+
'message_updated_at': message_data['message']['_vnd']['v1']['chat']['updated_at'],
|
143 |
+
}
|
144 |
+
message_text = message_data['message_body']
|
145 |
+
|
146 |
+
# Run intent classification only for keywords
|
147 |
+
intent_api_response = run_intent_classification(message_text)
|
148 |
+
if intent_api_response['data']:
|
149 |
+
return intent_api_response
|
150 |
+
|
151 |
+
number_api_resp = text2int(message_text.lower())
|
152 |
+
|
153 |
+
if number_api_resp == 32202:
|
154 |
+
# Run intent classification with logistic regression model
|
155 |
+
predicted_label = predict_message_intent(message_text)
|
156 |
+
if predicted_label['confidence'] > 0.01:
|
157 |
+
nlu_response = predicted_label
|
158 |
+
else:
|
159 |
+
# Run sentiment analysis
|
160 |
+
sentiment_api_resp = sentiment(message_text)
|
161 |
+
nlu_response = build_nlu_response_object(
|
162 |
+
'sentiment',
|
163 |
+
sentiment_api_resp[0]['label'],
|
164 |
+
sentiment_api_resp[0]['score']
|
165 |
+
)
|
166 |
+
else:
|
167 |
+
nlu_response = build_nlu_response_object(
|
168 |
+
'integer',
|
169 |
+
number_api_resp,
|
170 |
+
0
|
171 |
+
)
|
172 |
+
|
173 |
+
prepare_message_data_for_logging(message_data, nlu_response)
|
174 |
+
return nlu_response
|
mathtext_fastapi/v2_conversation_manager.py
ADDED
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import copy
|
3 |
+
import dill
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
import jsonpickle
|
7 |
+
import pickle
|
8 |
+
import random
|
9 |
+
import requests
|
10 |
+
import mathtext_fastapi.global_state_manager as gsm
|
11 |
+
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
from mathtext_fastapi.nlu import evaluate_message_with_nlu
|
14 |
+
from mathtext_fastapi.math_quiz_fsm import MathQuizFSM
|
15 |
+
from mathtext_fastapi.math_subtraction_fsm import MathSubtractionFSM
|
16 |
+
from supabase import create_client
|
17 |
+
from transitions import Machine
|
18 |
+
|
19 |
+
from mathactive.generators import start_interactive_math
|
20 |
+
from mathactive.hints import generate_hint
|
21 |
+
from mathactive.microlessons import num_one
|
22 |
+
|
23 |
+
load_dotenv()
|
24 |
+
|
25 |
+
SUPA = create_client(
|
26 |
+
os.environ.get('SUPABASE_URL'),
|
27 |
+
os.environ.get('SUPABASE_KEY')
|
28 |
+
)
|
29 |
+
|
30 |
+
|
31 |
+
def pickle_and_encode_state_machine(state_machine):
|
32 |
+
dump = pickle.dumps(state_machine)
|
33 |
+
dump_encoded = base64.b64encode(dump).decode('utf-8')
|
34 |
+
return dump_encoded
|
35 |
+
|
36 |
+
|
37 |
+
def manage_math_quiz_fsm(user_message, contact_uuid, type):
|
38 |
+
fsm_check = SUPA.table('state_machines').select("*").eq(
|
39 |
+
"contact_uuid",
|
40 |
+
contact_uuid
|
41 |
+
).execute()
|
42 |
+
|
43 |
+
# This doesn't allow for when one FSM is present and the other is empty
|
44 |
+
"""
|
45 |
+
1
|
46 |
+
data=[] count=None
|
47 |
+
|
48 |
+
2
|
49 |
+
data=[{'id': 29, 'contact_uuid': 'j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09', 'addition3': None, 'subtraction': None, 'addition':
|
50 |
+
|
51 |
+
- but problem is there is no subtraction , but it's assuming there's a subtration
|
52 |
+
|
53 |
+
Cases
|
54 |
+
- make a completely new record
|
55 |
+
- update an existing record with an existing FSM
|
56 |
+
- update an existing record without an existing FSM
|
57 |
+
"""
|
58 |
+
print("MATH QUIZ FSM ACTIVITY")
|
59 |
+
print("user_message")
|
60 |
+
print(user_message)
|
61 |
+
# Make a completely new entry
|
62 |
+
if fsm_check.data == []:
|
63 |
+
if type == 'addition':
|
64 |
+
math_quiz_state_machine = MathQuizFSM()
|
65 |
+
else:
|
66 |
+
math_quiz_state_machine = MathSubtractionFSM()
|
67 |
+
messages = [math_quiz_state_machine.response_text]
|
68 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
69 |
+
|
70 |
+
SUPA.table('state_machines').insert({
|
71 |
+
'contact_uuid': contact_uuid,
|
72 |
+
f'{type}': dump_encoded
|
73 |
+
}).execute()
|
74 |
+
# Update an existing record with a new state machine
|
75 |
+
elif not fsm_check.data[0][type]:
|
76 |
+
if type == 'addition':
|
77 |
+
math_quiz_state_machine = MathQuizFSM()
|
78 |
+
else:
|
79 |
+
math_quiz_state_machine = MathSubtractionFSM()
|
80 |
+
messages = [math_quiz_state_machine.response_text]
|
81 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
82 |
+
|
83 |
+
SUPA.table('state_machines').update({
|
84 |
+
f'{type}': dump_encoded
|
85 |
+
}).eq(
|
86 |
+
"contact_uuid", contact_uuid
|
87 |
+
).execute()
|
88 |
+
# Update an existing record with an existing state machine
|
89 |
+
elif fsm_check.data[0][type]:
|
90 |
+
undump_encoded = base64.b64decode(
|
91 |
+
fsm_check.data[0][type].encode('utf-8')
|
92 |
+
)
|
93 |
+
math_quiz_state_machine = pickle.loads(undump_encoded)
|
94 |
+
|
95 |
+
math_quiz_state_machine.student_answer = user_message
|
96 |
+
math_quiz_state_machine.correct_answer = str(math_quiz_state_machine.correct_answer)
|
97 |
+
messages = math_quiz_state_machine.validate_answer()
|
98 |
+
dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)
|
99 |
+
SUPA.table('state_machines').update({
|
100 |
+
f'{type}': dump_encoded
|
101 |
+
}).eq(
|
102 |
+
"contact_uuid", contact_uuid
|
103 |
+
).execute()
|
104 |
+
return messages
|
105 |
+
|
106 |
+
|
107 |
+
def retrieve_microlesson_content(context_data, user_message, microlesson, contact_uuid):
|
108 |
+
# TODO: This is being filtered by both the local and global states, so not changing
|
109 |
+
if microlesson == 'addition':
|
110 |
+
messages = manage_math_quiz_fsm(user_message, contact_uuid, 'addition')
|
111 |
+
|
112 |
+
if user_message == 'exit':
|
113 |
+
state_label = 'exit'
|
114 |
+
else:
|
115 |
+
state_label = 'addition-question-sequence'
|
116 |
+
|
117 |
+
input_prompt = messages.pop()
|
118 |
+
message_package = {
|
119 |
+
'messages': messages,
|
120 |
+
'input_prompt': input_prompt,
|
121 |
+
'state': state_label
|
122 |
+
}
|
123 |
+
elif microlesson == 'addition2':
|
124 |
+
message_package = num_one.process_user_message(contact_uuid, user_message)
|
125 |
+
elif context_data['local_state'] == 'subtraction-question-sequence' or \
|
126 |
+
user_message == 'subtract' or \
|
127 |
+
microlesson == 'subtraction':
|
128 |
+
messages = manage_math_quiz_fsm(user_message, contact_uuid, 'subtraction')
|
129 |
+
|
130 |
+
if user_message == 'exit':
|
131 |
+
state_label = 'exit'
|
132 |
+
else:
|
133 |
+
state_label = 'subtraction-question-sequence'
|
134 |
+
|
135 |
+
input_prompt = messages.pop()
|
136 |
+
|
137 |
+
message_package = {
|
138 |
+
'messages': messages,
|
139 |
+
'input_prompt': input_prompt,
|
140 |
+
'state': state_label
|
141 |
+
}
|
142 |
+
print("MICROLESSON CONTENT RESPONSE")
|
143 |
+
print(message_package)
|
144 |
+
return message_package
|
145 |
+
|
146 |
+
|
147 |
+
curriculum_lookup_table = {
|
148 |
+
'N1.1.1_G1': 'addition',
|
149 |
+
'N1.1.1_G2': 'addition2',
|
150 |
+
'N1.1.2_G1': 'subtraction'
|
151 |
+
}
|
152 |
+
|
153 |
+
|
154 |
+
def lookup_local_state(next_state):
|
155 |
+
microlesson = curriculum_lookup_table[next_state]
|
156 |
+
return microlesson
|
157 |
+
|
158 |
+
|
159 |
+
def create_text_message(message_text, whatsapp_id):
|
160 |
+
""" Fills a template with input values to send a text message to Whatsapp
|
161 |
+
|
162 |
+
Inputs
|
163 |
+
- message_text: str - the content that the message should display
|
164 |
+
- whatsapp_id: str - the message recipient's phone number
|
165 |
+
|
166 |
+
Outputs
|
167 |
+
- message_data: dict - a preformatted template filled with inputs
|
168 |
+
"""
|
169 |
+
message_data = {
|
170 |
+
"preview_url": False,
|
171 |
+
"recipient_type": "individual",
|
172 |
+
"to": whatsapp_id,
|
173 |
+
"type": "text",
|
174 |
+
"text": {
|
175 |
+
"body": message_text
|
176 |
+
}
|
177 |
+
}
|
178 |
+
return message_data
|
179 |
+
|
180 |
+
|
181 |
+
def manage_conversation_response(data_json):
|
182 |
+
""" Calls functions necessary to determine message and context data """
|
183 |
+
print("V2 ENDPOINT")
|
184 |
+
|
185 |
+
# whatsapp_id = data_json['author_id']
|
186 |
+
message_data = data_json['message_data']
|
187 |
+
context_data = data_json['context_data']
|
188 |
+
whatsapp_id = message_data['author_id']
|
189 |
+
user_message = message_data['message_body']
|
190 |
+
print("MESSAGE DATA")
|
191 |
+
print(message_data)
|
192 |
+
print("CONTEXT DATA")
|
193 |
+
print(context_data)
|
194 |
+
print("=================")
|
195 |
+
|
196 |
+
# nlu_response = evaluate_message_with_nlu(message_data)
|
197 |
+
|
198 |
+
# context_data = {
|
199 |
+
# 'contact_uuid': 'abcdefg',
|
200 |
+
# 'current_state': 'N1.1.1_G2',
|
201 |
+
# 'user_message': '1',
|
202 |
+
# 'local_state': ''
|
203 |
+
# }
|
204 |
+
print("STEP 1")
|
205 |
+
print(data_json)
|
206 |
+
print(f"1: {context_data['current_state']}")
|
207 |
+
if not context_data['current_state']:
|
208 |
+
context_data['current_state'] = 'N1.1.1_G1'
|
209 |
+
print(f"2: {context_data['current_state']}")
|
210 |
+
|
211 |
+
curriculum_copy = copy.deepcopy(gsm.curriculum)
|
212 |
+
curriculum_copy.state = context_data['current_state']
|
213 |
+
print("STEP 2")
|
214 |
+
if user_message == 'easier':
|
215 |
+
curriculum_copy.left()
|
216 |
+
next_state = curriculum_copy.state
|
217 |
+
elif user_message == 'harder':
|
218 |
+
curriculum_copy.right()
|
219 |
+
next_state = curriculum_copy.state
|
220 |
+
else:
|
221 |
+
next_state = context_data['current_state']
|
222 |
+
print("next_state")
|
223 |
+
print(next_state)
|
224 |
+
|
225 |
+
print("STEP 3")
|
226 |
+
microlesson = lookup_local_state(next_state)
|
227 |
+
|
228 |
+
print("microlesson")
|
229 |
+
print(microlesson)
|
230 |
+
|
231 |
+
microlesson_content = retrieve_microlesson_content(context_data, user_message, microlesson, context_data['contact_uuid'])
|
232 |
+
|
233 |
+
headers = {
|
234 |
+
'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
|
235 |
+
'Content-Type': 'application/json'
|
236 |
+
}
|
237 |
+
|
238 |
+
# Send all messages for the current state before a user input prompt (text/button input request)
|
239 |
+
for message in microlesson_content['messages']:
|
240 |
+
data = create_text_message(message, whatsapp_id)
|
241 |
+
|
242 |
+
print("data")
|
243 |
+
print(data)
|
244 |
+
|
245 |
+
r = requests.post(
|
246 |
+
f'https://whatsapp.turn.io/v1/messages',
|
247 |
+
data=json.dumps(data),
|
248 |
+
headers=headers
|
249 |
+
)
|
250 |
+
|
251 |
+
print("STEP 4")
|
252 |
+
# combine microlesson content and context_data object
|
253 |
+
|
254 |
+
updated_context = {
|
255 |
+
"context": {
|
256 |
+
"contact_id": whatsapp_id,
|
257 |
+
"contact_uuid": context_data['contact_uuid'],
|
258 |
+
"current_state": next_state,
|
259 |
+
"local_state": microlesson_content['state'],
|
260 |
+
"bot_message": microlesson_content['input_prompt'],
|
261 |
+
"user_message": user_message,
|
262 |
+
"type": 'ask'
|
263 |
+
}
|
264 |
+
}
|
265 |
+
print(updated_context)
|
266 |
+
return updated_context
|
pyproject.toml
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[tool.poetry]
|
2 |
+
name = "MathText_FastAPI"
|
3 |
+
version = "0.0.1"
|
4 |
+
authors = [
|
5 |
+
"Sebastian Larsen <sebastianlarson22@gmail.com>",
|
6 |
+
"Çetin ÇAKIR <cetincakirtr@gmail.com>",
|
7 |
+
"Hobson Lane <gitlab@totalgood.com>",
|
8 |
+
]
|
9 |
+
description = "Natural Language Understanding (text processing) for math symbols, digits, and words with a Gradio user interface and REST API."
|
10 |
+
readme = "README.md"
|
11 |
+
# requires-python = ">=3.8"
|
12 |
+
license = "AGPL-3.0-or-later"
|
13 |
+
classifiers = [
|
14 |
+
"Programming Language :: Python :: 3",
|
15 |
+
"Programming Language :: Python :: 3.8",
|
16 |
+
"Programming Language :: Python :: 3.9",
|
17 |
+
"License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
|
18 |
+
"Operating System :: OS Independent",
|
19 |
+
]
|
20 |
+
|
21 |
+
|
22 |
+
[tool.poetry.dependencies]
|
23 |
+
mathactive = {git = "git@gitlab.com:tangibleai/community/mathactive.git", rev = "vlad"}
|
24 |
+
mathtext = {git = "https://gitlab.com/tangibleai/community/mathtext", rev = "main"}
|
25 |
+
fastapi = "^0.90.0"
|
26 |
+
pydantic = "*"
|
27 |
+
python = "^3.8"
|
28 |
+
requests = "2.27.*"
|
29 |
+
sentencepiece = "0.1.*"
|
30 |
+
supabase = "*"
|
31 |
+
uvicorn = "0.17.*"
|
32 |
+
pandas = "^1.5.3"
|
33 |
+
scipy = "^1.10.1"
|
34 |
+
|
35 |
+
[tool.poetry.group.dev.dependencies]
|
36 |
+
pytest = "^7.2"
|
37 |
+
|
38 |
+
[build-system]
|
39 |
+
requires = ["poetry-core"]
|
40 |
+
build-backend = "poetry.core.masonry.api"
|
41 |
+
|
42 |
+
# [build-system]
|
43 |
+
# requires = ["hatchling"]
|
44 |
+
# build-backend = "hatchling.build"
|
45 |
+
|
46 |
+
# repository = "https://gitlab.com/tangibleai/community/mathtext-fastapi"
|
requirements.txt
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
dill
|
2 |
+
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.4.1/en_core_web_sm-3.4.1-py3-none-any.whl
|
3 |
+
fuzzywuzzy
|
4 |
+
jsonpickle
|
5 |
+
mathtext @ git+https://gitlab.com/tangibleai/community/mathtext@main
|
6 |
+
mathactive @ git+https://gitlab.com/tangibleai/community/mathactive@main
|
7 |
+
fastapi
|
8 |
+
pydantic
|
9 |
+
requests
|
10 |
+
sentencepiece
|
11 |
+
openpyxl
|
12 |
+
python-Levenshtein
|
13 |
+
sentence-transformers
|
14 |
+
supabase
|
15 |
+
transitions
|
16 |
+
uvicorn
|
17 |
+
pandas
|
18 |
+
scipy
|
scripts/__init__.py
ADDED
File without changes
|
scripts/api_scaling.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""https://zetcode.com/python/concurrent-http-requests/"""
|
2 |
+
|
3 |
+
import asyncio
|
4 |
+
import random
|
5 |
+
import time
|
6 |
+
import pandas as pd
|
7 |
+
import httpx
|
8 |
+
from os.path import exists
|
9 |
+
|
10 |
+
NUMBER_OF_CALLS = 1
|
11 |
+
|
12 |
+
headers = {"Content-Type": "application/json; charset=utf-8"}
|
13 |
+
|
14 |
+
# base_url = "https://tangibleai-mathtext-fastapi.hf.space/{endpoint}"
|
15 |
+
base_url = "http://localhost:7860/run/{endpoint}"
|
16 |
+
|
17 |
+
data_list_1 = {
|
18 |
+
"endpoint": "text2int",
|
19 |
+
"test_data": [
|
20 |
+
"one hundred forty five",
|
21 |
+
"twenty thousand nine hundred fifty",
|
22 |
+
"one hundred forty five",
|
23 |
+
"nine hundred eighty three",
|
24 |
+
"five million",
|
25 |
+
]
|
26 |
+
}
|
27 |
+
|
28 |
+
data_list_2 = {
|
29 |
+
"endpoint": "text2int-preprocessed",
|
30 |
+
"test_data": [
|
31 |
+
"one hundred forty five",
|
32 |
+
"twenty thousand nine hundred fifty",
|
33 |
+
"one hundred forty five",
|
34 |
+
"nine hundred eighty three",
|
35 |
+
"five million",
|
36 |
+
]
|
37 |
+
}
|
38 |
+
data_list_3 = {
|
39 |
+
"endpoint": "sentiment-analysis",
|
40 |
+
"test_data": [
|
41 |
+
"Totally agree",
|
42 |
+
"I like it",
|
43 |
+
"No more",
|
44 |
+
"I am not sure",
|
45 |
+
"Never",
|
46 |
+
]
|
47 |
+
}
|
48 |
+
|
49 |
+
|
50 |
+
# async call to endpoint
|
51 |
+
async def call_api(url, data, call_number, number_of_calls):
|
52 |
+
json = {"data": [data]}
|
53 |
+
async with httpx.AsyncClient() as client:
|
54 |
+
start = time.perf_counter() # Used perf_counter for more precise result.
|
55 |
+
response = await client.post(url=url, headers=headers, json=json, timeout=30)
|
56 |
+
end = time.perf_counter()
|
57 |
+
return {
|
58 |
+
"endpoint": url.split("/")[-1],
|
59 |
+
"test data": data,
|
60 |
+
"status code": response.status_code,
|
61 |
+
"response": response.json().get("data"),
|
62 |
+
"call number": call_number,
|
63 |
+
"number of calls": number_of_calls,
|
64 |
+
"start": start.__round__(4),
|
65 |
+
"end": end.__round__(4),
|
66 |
+
"delay": (end - start).__round__(4)
|
67 |
+
}
|
68 |
+
|
69 |
+
|
70 |
+
data_lists = [data_list_1, data_list_2, data_list_3]
|
71 |
+
|
72 |
+
results = []
|
73 |
+
|
74 |
+
|
75 |
+
async def main(number_of_calls):
|
76 |
+
for data_list in data_lists:
|
77 |
+
calls = []
|
78 |
+
for call_number in range(1, number_of_calls + 1):
|
79 |
+
url = base_url.format(endpoint=data_list["endpoint"])
|
80 |
+
data = random.choice(data_list["test_data"])
|
81 |
+
calls.append(call_api(url, data, call_number, number_of_calls))
|
82 |
+
r = await asyncio.gather(*calls)
|
83 |
+
results.extend(r)
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
start = time.perf_counter()
|
88 |
+
asyncio.run(main(NUMBER_OF_CALLS))
|
89 |
+
end = time.perf_counter()
|
90 |
+
print(end-start)
|
91 |
+
df = pd.DataFrame(results)
|
92 |
+
|
93 |
+
if exists("call_history.csv"):
|
94 |
+
df.to_csv(path_or_buf="call_history.csv", mode="a", header=False, index=False)
|
95 |
+
else:
|
96 |
+
df.to_csv(path_or_buf="call_history.csv", mode="w", header=True, index=False)
|
scripts/api_scaling.sh
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#! /bin/env bash
|
2 |
+
|
3 |
+
LOG_FILE_NAME="call_history_bash.csv"
|
4 |
+
|
5 |
+
if [[ ! -f "$LOG_FILE_NAME" ]]; then
|
6 |
+
# Creation of column names if the file does not exits
|
7 |
+
echo "student_id;active_students;endpoint;inputs;outputs;started;finished" >$LOG_FILE_NAME
|
8 |
+
fi
|
9 |
+
|
10 |
+
data_list_1() {
|
11 |
+
responses=(
|
12 |
+
"one hundred forty five"
|
13 |
+
"twenty thousand nine hundred fifty"
|
14 |
+
"one hundred forty five"
|
15 |
+
"nine hundred eighty three"
|
16 |
+
"five million"
|
17 |
+
)
|
18 |
+
echo "${responses[$1]}"
|
19 |
+
}
|
20 |
+
|
21 |
+
data_list_2() {
|
22 |
+
responses=(
|
23 |
+
"Totally agree"
|
24 |
+
"I like it"
|
25 |
+
"No more"
|
26 |
+
"I am not sure"
|
27 |
+
"Never"
|
28 |
+
)
|
29 |
+
echo "${responses[$1]}"
|
30 |
+
}
|
31 |
+
|
32 |
+
# endpoints: "text2int" "sentiment-analysis"
|
33 |
+
# selected endpoint to test
|
34 |
+
endpoint="sentiment-analysis"
|
35 |
+
|
36 |
+
create_random_delay() {
|
37 |
+
# creates a random delay for given arguments
|
38 |
+
echo "scale=8; $RANDOM/32768*$1" | bc
|
39 |
+
}
|
40 |
+
|
41 |
+
simulate_student() {
|
42 |
+
# Student simulator waits randomly between 0-10s after an interaction.
|
43 |
+
# Based on 100 interactions per student
|
44 |
+
for i in {1..100}; do
|
45 |
+
|
46 |
+
random_value=$((RANDOM % 5))
|
47 |
+
text=$(data_list_2 $random_value)
|
48 |
+
data='{"data": ["'$text'"]}'
|
49 |
+
|
50 |
+
start_=$(date +"%F %T.%6N")
|
51 |
+
|
52 |
+
url="https://tangibleai-mathtext-fastapi.hf.space/$3"
|
53 |
+
response=$(curl --silent --connect-timeout 30 --max-time 30 -X POST "$url" -H 'Content-Type: application/json' -d "$data")
|
54 |
+
|
55 |
+
if [[ "$response" == *"Time-out"* ]]; then
|
56 |
+
echo "$response" >>bad_response.txt
|
57 |
+
response="504 Gateway Time-out"
|
58 |
+
elif [[ -z "$response" ]]; then
|
59 |
+
echo "No response" >>bad_response.txt
|
60 |
+
response="504 Gateway Time-out"
|
61 |
+
fi
|
62 |
+
|
63 |
+
end_=$(date +"%F %T.%6N")
|
64 |
+
|
65 |
+
printf "%s;%s;%s;%s;%s;%s;%s\n" "$1" "$2" "$3" "$data" "$response" "$start_" "$end_" >>$LOG_FILE_NAME
|
66 |
+
sleep "$(create_random_delay 10)"
|
67 |
+
|
68 |
+
done
|
69 |
+
}
|
70 |
+
|
71 |
+
echo "start: $(date)"
|
72 |
+
|
73 |
+
active_students=250 # the number of students using the system at the same time
|
74 |
+
|
75 |
+
i=1
|
76 |
+
while [[ "$i" -le "$active_students" ]]; do
|
77 |
+
simulate_student "student$i" "$active_students" "$endpoint" &
|
78 |
+
sleep "$(create_random_delay 1)" # adding a random delay between students
|
79 |
+
i=$(("$i" + 1))
|
80 |
+
done
|
81 |
+
|
82 |
+
wait
|
83 |
+
echo "end: $(date)"
|
scripts/build.sh
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
deactivate
|
2 |
+
pip install virtualenv
|
3 |
+
rm -rf .venv
|
4 |
+
python3.9 -m virtualenv --python 3.9 .venv
|
5 |
+
# pip install --upgrade scikit-learn
|
6 |
+
# pip install --upgrade transformers
|
7 |
+
# pip install --upgrade pandas
|
8 |
+
pip install --upgrade -e .
|
scripts/make_request.py
ADDED
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
|
4 |
+
|
5 |
+
def add_message_text_to_sample_object(message_text):
|
6 |
+
"""
|
7 |
+
Builds a sample request object using an example of a student answer
|
8 |
+
|
9 |
+
Input
|
10 |
+
- message_text: str - an example of user input to test
|
11 |
+
|
12 |
+
Example Input
|
13 |
+
"test message"
|
14 |
+
|
15 |
+
Output
|
16 |
+
- b_string: json b-string - simulated Turn.io message data
|
17 |
+
|
18 |
+
Example Output
|
19 |
+
b'{"context": "hi", "message_data": {"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "test message", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}}'
|
20 |
+
|
21 |
+
"""
|
22 |
+
message_data = '{' + f'"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "{message_text}", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"' + '}'
|
23 |
+
# context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}"' + '}'
|
24 |
+
|
25 |
+
# V1
|
26 |
+
# context_data = '{' + '"user":"", "state":"start-conversation", "bot_message":"", "user_message":"{message_text}"' + '}'
|
27 |
+
|
28 |
+
#V2
|
29 |
+
context_data = '{' + '"contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "current_state":"", "local_state": "", "user_message":""' + '}'
|
30 |
+
|
31 |
+
# context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}","text": "What is 2+3?","question_numbers": [4,3],"right_answer": 7,"number_correct": 2, "number_incorrect": 0, "hints_used": 0, "level": "easy"' + '}'
|
32 |
+
|
33 |
+
json_string = '{' + f'"context_data": {context_data}, "message_data": {message_data}' + '}'
|
34 |
+
b_string = json_string.encode("utf-8")
|
35 |
+
|
36 |
+
return b_string
|
37 |
+
|
38 |
+
# """
|
39 |
+
# "text": "What is 2+3?",
|
40 |
+
# "question_numbers": [2,3],
|
41 |
+
# "right_answer": 5,
|
42 |
+
# "number_correct": 2,
|
43 |
+
# "hints_used": 0,
|
44 |
+
# """
|
45 |
+
|
46 |
+
|
47 |
+
def run_simulated_request(endpoint, sample_answer, context=None):
|
48 |
+
print(f"Case: {sample_answer}")
|
49 |
+
b_string = add_message_text_to_sample_object(sample_answer)
|
50 |
+
|
51 |
+
if endpoint == 'sentiment-analysis' or endpoint == 'text2int' or endpoint =='intent-classification':
|
52 |
+
request = requests.post(
|
53 |
+
url=f'http://localhost:7860/{endpoint}',
|
54 |
+
json={'content': sample_answer}
|
55 |
+
).json()
|
56 |
+
else:
|
57 |
+
request = requests.post(
|
58 |
+
url=f'http://localhost:7860/{endpoint}',
|
59 |
+
data=b_string
|
60 |
+
).json()
|
61 |
+
|
62 |
+
print(request)
|
63 |
+
|
64 |
+
|
65 |
+
# run_simulated_request('intent-classification', 'exit')
|
66 |
+
# run_simulated_request('intent-classification', "I'm not sure")
|
67 |
+
# run_simulated_request('sentiment-analysis', 'I reject it')
|
68 |
+
# run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')
|
69 |
+
# run_simulated_request('nlu', 'test message')
|
70 |
+
# run_simulated_request('nlu', 'eight')
|
71 |
+
# run_simulated_request('nlu', 'is it 8')
|
72 |
+
# run_simulated_request('nlu', 'can I know how its 0.5')
|
73 |
+
# run_simulated_request('nlu', 'eight, nine, ten')
|
74 |
+
# run_simulated_request('nlu', '8, 9, 10')
|
75 |
+
# run_simulated_request('nlu', '8')
|
76 |
+
# run_simulated_request('nlu', "I don't know")
|
77 |
+
# run_simulated_request('nlu', "I don't know eight")
|
78 |
+
# run_simulated_request('nlu', "I don't 9")
|
79 |
+
# run_simulated_request('nlu', "0.2")
|
80 |
+
# run_simulated_request('nlu', 'Today is a wonderful day')
|
81 |
+
# run_simulated_request('nlu', 'IDK 5?')
|
82 |
+
# run_simulated_request('v2/manager', '')
|
83 |
+
# run_simulated_request('v2/manager', '5')
|
84 |
+
# run_simulated_request('manager', '')
|
85 |
+
# run_simulated_request('manager', 'add')
|
86 |
+
# run_simulated_request('manager', 'subtract')
|
87 |
+
|
88 |
+
# run_simulated_request("start", {
|
89 |
+
# 'difficulty': 0.04,
|
90 |
+
# 'do_increase': True
|
91 |
+
# })
|
92 |
+
# run_simulated_request("hint", {
|
93 |
+
# 'start': 5,
|
94 |
+
# 'step': 1,
|
95 |
+
# 'difficulty': 0.56 # optional
|
96 |
+
# })
|
97 |
+
# run_simulated_request("question", {
|
98 |
+
# 'start': 2,
|
99 |
+
# 'step': 1,
|
100 |
+
# 'question_num': 2 # optional
|
101 |
+
# })
|
102 |
+
# run_simulated_request("difficulty", {
|
103 |
+
# 'difficulty': 0.01,
|
104 |
+
# 'do_increase': False # True | False
|
105 |
+
# })
|
106 |
+
# Need to start with this command to populate users.json
|
107 |
+
# If users.json is not already made
|
108 |
+
# run_simulated_request("num_one", {
|
109 |
+
# "user_id": "1",
|
110 |
+
# "message_text": "",
|
111 |
+
# })
|
112 |
+
run_simulated_request("num_one", {
|
113 |
+
"user_id": "1",
|
114 |
+
"message_text": "61",
|
115 |
+
})
|
116 |
+
# run_simulated_request("sequence", {
|
117 |
+
# 'start': 2,
|
118 |
+
# 'step': 1,
|
119 |
+
# 'sep': '... '
|
120 |
+
# })
|
121 |
+
|
122 |
+
# run_simulated_request('manager', 'exit')
|
123 |
+
|
124 |
+
|
125 |
+
# Example of simplified object received from Turn.io stacks
|
126 |
+
# This is a contrived example to show the structure, not an actual state
|
127 |
+
# NOTE: This is actually a bstring, not a dict
|
128 |
+
simplified_json = {
|
129 |
+
"context": {
|
130 |
+
"user": "+57787919091",
|
131 |
+
"state": "answer-addition-problem",
|
132 |
+
"bot_message": "What is 2+2?",
|
133 |
+
"user_message": "eight",
|
134 |
+
"type": "ask"
|
135 |
+
},
|
136 |
+
"message_data": {
|
137 |
+
"author_id": "+57787919091",
|
138 |
+
"author_type": "OWNER",
|
139 |
+
"contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09",
|
140 |
+
"message_body": "eight",
|
141 |
+
"message_direction": "inbound",
|
142 |
+
"message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32",
|
143 |
+
"message_inserted_at": "2023-01-10T02:37:28.477940Z",
|
144 |
+
"message_updated_at": "2023-01-10T02:37:28.487319Z"
|
145 |
+
}
|
146 |
+
}
|
147 |
+
|
148 |
+
|
149 |
+
# Full example of event data from Turn.io
|
150 |
+
# simplified_json is built from this in Turn.io
|
151 |
+
# full_json = {
|
152 |
+
# 'message': {
|
153 |
+
# '_vnd': {
|
154 |
+
# 'v1': {
|
155 |
+
# 'author': {
|
156 |
+
# 'id': 57787919091,
|
157 |
+
# 'name': 'GT',
|
158 |
+
# 'type': 'OWNER'
|
159 |
+
# },
|
160 |
+
# 'card_uuid': None,
|
161 |
+
# 'chat': {
|
162 |
+
# 'assigned_to': {
|
163 |
+
# 'id': 'jhk151kl-hj42-3752-3hjk-h4jk6hjkk2',
|
164 |
+
# 'name': 'Greg Thompson',
|
165 |
+
# 'type': 'OPERATOR'
|
166 |
+
# },
|
167 |
+
# 'contact_uuid': 'j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09',
|
168 |
+
# 'inserted_at': '2022-07-05T04:00:34.033522Z',
|
169 |
+
# 'owner': '+57787919091',
|
170 |
+
# 'permalink': 'https://app.turn.io/c/4kl209sd0-a7b8-2hj3-8563-3hu4a89b32',
|
171 |
+
# 'state': 'OPEN',
|
172 |
+
# 'state_reason': 'Re-opened by inbound message.',
|
173 |
+
# 'unread_count': 19,
|
174 |
+
# 'updated_at': '2023-01-10T02:37:28.487319Z',
|
175 |
+
# 'uuid': '4kl209sd0-a7b8-2hj3-8563-3hu4a89b32'
|
176 |
+
# },
|
177 |
+
# 'direction': 'inbound',
|
178 |
+
# 'faq_uuid': None,
|
179 |
+
# 'in_reply_to': None,
|
180 |
+
# 'inserted_at': '2023-01-10T02:37:28.477940Z',
|
181 |
+
# 'labels': [{
|
182 |
+
# 'confidence': 0.506479332,
|
183 |
+
# 'metadata': {
|
184 |
+
# 'nlu': {
|
185 |
+
# 'confidence': 0.506479332,
|
186 |
+
# 'intent': 'question',
|
187 |
+
# 'model_name': 'nlu-general-spacy-ngrams-20191014'
|
188 |
+
# }
|
189 |
+
# },
|
190 |
+
# 'uuid': 'ha7890s2k-hjk2-2476-s8d9-fh9779a8a9ds',
|
191 |
+
# 'value': 'Unclassified'
|
192 |
+
# }],
|
193 |
+
# 'last_status': None,
|
194 |
+
# 'last_status_timestamp': None,
|
195 |
+
# 'on_fallback_channel': False,
|
196 |
+
# 'rendered_content': None,
|
197 |
+
# 'uuid': 's8df79zhws-h89s-hj23-7s8d-thb248d9bh2qn'
|
198 |
+
# }
|
199 |
+
# },
|
200 |
+
# 'from': 57787919091,
|
201 |
+
# 'id': 'hsjkthzZGehkzs09sijWA3',
|
202 |
+
# 'text': {'body': 'eight'},
|
203 |
+
# 'timestamp': 1673318248,
|
204 |
+
# 'type': 'text'
|
205 |
+
# },
|
206 |
+
# 'type': 'message'
|
207 |
+
# }
|
scripts/make_request.sh
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#root_url="localhost:7860"
|
2 |
+
root_url="https://tangibleai-mathtext-fastapi.hf.space"
|
3 |
+
|
4 |
+
ep="/"
|
5 |
+
url=$root_url$ep
|
6 |
+
data=''
|
7 |
+
|
8 |
+
response=$(curl --silent -X GET "$url" -H 'Content-Type: application/json')
|
9 |
+
|
10 |
+
echo "URL: $url"
|
11 |
+
echo "Data: $data"
|
12 |
+
echo "Response: $response"
|
13 |
+
echo
|
14 |
+
|
15 |
+
ep="/hello"
|
16 |
+
url=$root_url$ep
|
17 |
+
data='{"content":"Rori"}'
|
18 |
+
|
19 |
+
response=$(curl --silent -X POST "$url" -H 'Content-Type: application/json' -d "$data")
|
20 |
+
|
21 |
+
echo "URL: $url"
|
22 |
+
echo "Data: $data"
|
23 |
+
echo "Response: $response"
|
24 |
+
echo
|
25 |
+
|
26 |
+
ep="/sentiment-analysis"
|
27 |
+
url=$root_url$ep
|
28 |
+
data='{"content":"I am happy with it!"}'
|
29 |
+
|
30 |
+
response=$(curl --silent -X POST "$url" -H 'Content-Type: application/json' -d "$data")
|
31 |
+
|
32 |
+
echo "URL: $url"
|
33 |
+
echo "Data: $data"
|
34 |
+
echo "Response: $response"
|
35 |
+
echo
|
36 |
+
|
37 |
+
ep="/text2int"
|
38 |
+
url=$root_url$ep
|
39 |
+
data='{"content":"one hundred forty two"}'
|
40 |
+
|
41 |
+
response=$(curl --silent -X POST "$url" -H 'Content-Type: application/json' -d "$data")
|
42 |
+
|
43 |
+
echo "URL: $url"
|
44 |
+
echo "Data: $data"
|
45 |
+
echo "Response: $response"
|
46 |
+
echo
|
scripts/plot_calls.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
from datetime import datetime
|
3 |
+
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
pd.set_option('display.max_columns', None)
|
8 |
+
pd.set_option('display.max_rows', None)
|
9 |
+
|
10 |
+
log_files = [
|
11 |
+
'call_history_sentiment_1_bash.csv',
|
12 |
+
'call_history_text2int_1_bash.csv',
|
13 |
+
]
|
14 |
+
|
15 |
+
for log_file in log_files:
|
16 |
+
path_ = f"./data/{log_file}"
|
17 |
+
df = pd.read_csv(filepath_or_buffer=path_, sep=";")
|
18 |
+
df["finished_ts"] = df["finished"].apply(
|
19 |
+
lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f").timestamp())
|
20 |
+
df["started_ts"] = df["started"].apply(
|
21 |
+
lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f").timestamp())
|
22 |
+
df["elapsed"] = df["finished_ts"] - df["started_ts"]
|
23 |
+
|
24 |
+
df["success"] = df["outputs"].apply(lambda x: 0 if "Time-out" in x else 1)
|
25 |
+
|
26 |
+
student_numbers = sorted(df['active_students'].unique())
|
27 |
+
|
28 |
+
bins_dict = dict() # bins size for each group
|
29 |
+
min_finished_dict = dict() # zero time for each group
|
30 |
+
|
31 |
+
for student_number in student_numbers:
|
32 |
+
# for each student group calculates bins size and zero time
|
33 |
+
min_finished = df["finished_ts"][df["active_students"] == student_number].min()
|
34 |
+
max_finished = df["finished_ts"][df["active_students"] == student_number].max()
|
35 |
+
bins = math.ceil(max_finished - min_finished)
|
36 |
+
bins_dict.update({student_number: bins})
|
37 |
+
min_finished_dict.update({student_number: min_finished})
|
38 |
+
print(f"student number: {student_number}")
|
39 |
+
print(f"min finished: {min_finished}")
|
40 |
+
print(f"max finished: {max_finished}")
|
41 |
+
print(f"bins finished seconds: {bins}, minutes: {bins / 60}")
|
42 |
+
|
43 |
+
df["time_line"] = None
|
44 |
+
for student_number in student_numbers:
|
45 |
+
# calculates time-line for each student group
|
46 |
+
df["time_line"] = df.apply(
|
47 |
+
lambda x: x["finished_ts"] - min_finished_dict[student_number]
|
48 |
+
if x["active_students"] == student_number
|
49 |
+
else x["time_line"],
|
50 |
+
axis=1
|
51 |
+
)
|
52 |
+
|
53 |
+
# creates a '.csv' from the dataframe
|
54 |
+
df.to_csv(f"./data/processed_{log_file}", index=False, sep=";")
|
55 |
+
|
56 |
+
result = df.groupby(['active_students', 'success']) \
|
57 |
+
.agg({
|
58 |
+
'elapsed': ['mean', 'median', 'min', 'max'],
|
59 |
+
'success': ['count'],
|
60 |
+
})
|
61 |
+
|
62 |
+
print(f"Results for {log_file}")
|
63 |
+
print(result, "\n")
|
64 |
+
|
65 |
+
title = None
|
66 |
+
if "sentiment" in log_file.lower():
|
67 |
+
title = "API result for 'sentiment-analysis' endpoint"
|
68 |
+
elif "text2int" in log_file.lower():
|
69 |
+
title = "API result for 'text2int' endpoint"
|
70 |
+
|
71 |
+
for student_number in student_numbers:
|
72 |
+
# Prints percentage of the successful and failed calls
|
73 |
+
try:
|
74 |
+
failed_calls = result.loc[(student_number, 0), 'success'][0]
|
75 |
+
except:
|
76 |
+
failed_calls = 0
|
77 |
+
successful_calls = result.loc[(student_number, 1), 'success'][0]
|
78 |
+
percentage = (successful_calls / (failed_calls + successful_calls)) * 100
|
79 |
+
print(f"Percentage of successful API calls for {student_number} students: {percentage.__round__(2)}")
|
80 |
+
|
81 |
+
rows = len(student_numbers)
|
82 |
+
|
83 |
+
fig, axs = plt.subplots(rows, 2) # (rows, columns)
|
84 |
+
|
85 |
+
for index, student_number in enumerate(student_numbers):
|
86 |
+
# creates a boxplot for each test group
|
87 |
+
data = df[df["active_students"] == student_number]
|
88 |
+
axs[index][0].boxplot(x=data["elapsed"]) # axs[row][column]
|
89 |
+
# axs[index][0].set_title(f'Boxplot for {student_number} students')
|
90 |
+
axs[index][0].set_xlabel(f'student number {student_number}')
|
91 |
+
axs[index][0].set_ylabel('Elapsed time (s)')
|
92 |
+
|
93 |
+
# creates a histogram for each test group
|
94 |
+
axs[index][1].hist(x=data["elapsed"], bins=25) # axs[row][column]
|
95 |
+
# axs[index][1].set_title(f'Histogram for {student_number} students')
|
96 |
+
axs[index][1].set_xlabel('seconds')
|
97 |
+
axs[index][1].set_ylabel('Count of API calls')
|
98 |
+
|
99 |
+
fig.suptitle(title, fontsize=16)
|
100 |
+
|
101 |
+
fig, axs = plt.subplots(rows, 1) # (rows, columns)
|
102 |
+
|
103 |
+
for index, student_number in enumerate(student_numbers):
|
104 |
+
# creates a histogram and shows API calls on a timeline for each test group
|
105 |
+
data = df[df["active_students"] == student_number]
|
106 |
+
|
107 |
+
print(data["time_line"].head(10))
|
108 |
+
|
109 |
+
axs[index].hist(x=data["time_line"], bins=bins_dict[student_number]) # axs[row][column]
|
110 |
+
# axs[index][1].set_title(f'Histogram for {student_number} students')
|
111 |
+
axs[index].set_xlabel('seconds')
|
112 |
+
axs[index].set_ylabel('Count of API calls')
|
113 |
+
|
114 |
+
fig.suptitle(title, fontsize=16)
|
115 |
+
|
116 |
+
plt.show()
|
scripts/quiz/__init__.py
ADDED
File without changes
|
scripts/quiz/data.csv
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
difficulty,start
|
2 |
+
0.01,1
|
3 |
+
0.02,0
|
4 |
+
0.05,5
|
5 |
+
0.07,10
|
6 |
+
0.08,14
|
7 |
+
0.1,20
|
8 |
+
0.11,22
|
9 |
+
0.13,27
|
10 |
+
0.14,28
|
11 |
+
0.16,30
|
12 |
+
0.17,32
|
13 |
+
0.18,34
|
14 |
+
0.2,37
|
15 |
+
0.21,39
|
16 |
+
0.23,42
|
17 |
+
0.25,43
|
18 |
+
0.27,46
|
19 |
+
0.3,50
|
20 |
+
0.34,57
|
21 |
+
0.35,64
|
22 |
+
0.37,78
|
23 |
+
0.39,89
|
24 |
+
0.41,100
|
25 |
+
0.44,112
|
26 |
+
0.45,130
|
27 |
+
0.48,147
|
28 |
+
0.5,164
|
29 |
+
0.52,180
|
30 |
+
0.55,195
|
31 |
+
0.58,209
|
32 |
+
0.6,223
|
33 |
+
0.61,236
|
34 |
+
0.63,248
|
35 |
+
0.64,259
|
36 |
+
0.65,271
|
37 |
+
0.67,284
|
38 |
+
0.69,296
|
39 |
+
0.7,308
|
40 |
+
0.72,321
|
41 |
+
0.73,333
|
42 |
+
0.75,346
|
43 |
+
0.78,359
|
44 |
+
0.8,370
|
45 |
+
0.81,385
|
46 |
+
0.83,399
|
47 |
+
0.84,408
|
48 |
+
0.87,420
|
49 |
+
0.88,435
|
50 |
+
0.89,447
|
51 |
+
0.9,458
|
52 |
+
0.93,469
|
53 |
+
0.94,483
|
54 |
+
0.96,494
|
55 |
+
0.97,500
|
56 |
+
0.99,513
|
static/styles.css
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap');
|
2 |
+
|
3 |
+
body {
|
4 |
+
font-family: 'Roboto', sans-serif;
|
5 |
+
font-size: 16px;
|
6 |
+
background-color: black;
|
7 |
+
color: white
|
8 |
+
}
|
templates/home.html
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<title>Title</title>
|
6 |
+
<link rel="stylesheet" href="{{ url_for('static', path='/styles.css') }}">
|
7 |
+
</head>
|
8 |
+
<body>
|
9 |
+
<h2>Mathbot</h2>
|
10 |
+
<h3>Created with FastAPI</h3>
|
11 |
+
|
12 |
+
<h4>To make a request with python</h4>
|
13 |
+
<pre><code>
|
14 |
+
import requests
|
15 |
+
|
16 |
+
requests.post(
|
17 |
+
url='https://tangibleai-mathtext-fastapi.hf.space/sentiment-analysis',
|
18 |
+
json={"content": "I reject it"}
|
19 |
+
).json()
|
20 |
+
|
21 |
+
requests.post(
|
22 |
+
url='https://tangibleai-mathtext-fastapi.hf.space/text2int',
|
23 |
+
json={"content": "forty two"}
|
24 |
+
).json()
|
25 |
+
|
26 |
+
</code></pre>
|
27 |
+
|
28 |
+
<h4>To make a request with curl</h4>
|
29 |
+
<pre><code>
|
30 |
+
curl --silent -X POST "https://tangibleai-mathtext-fastapi.hf.space/sentiment-analysis" -H 'Content-Type: application/json' -d '{"content":"I am happy with it!"}'
|
31 |
+
|
32 |
+
curl --silent -X POST "https://tangibleai-mathtext-fastapi.hf.space/text2int" -H 'Content-Type: application/json' -d '{"content":"forty two"}'
|
33 |
+
</code></pre>
|
34 |
+
</body>
|
35 |
+
</html>
|
tests/__init__.py
ADDED
File without changes
|
tests/test_text2int.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import unittest
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
from fastapi.testclient import TestClient
|
6 |
+
|
7 |
+
from app import app
|
8 |
+
|
9 |
+
# The raw file URL has to be used for GitLab.
|
10 |
+
URL = "https://gitlab.com/tangibleai/community/mathtext/-/raw/main/mathtext/data/master_test_text2int.csv"
|
11 |
+
|
12 |
+
DATA_DIR = Path(__file__).parent.parent / "mathtext_fastapi" / "data"
|
13 |
+
print(DATA_DIR)
|
14 |
+
|
15 |
+
client = TestClient(app)
|
16 |
+
|
17 |
+
|
18 |
+
class TestStringMethods(unittest.TestCase):
|
19 |
+
|
20 |
+
def setUp(self):
|
21 |
+
"""Creates a fastapi test client"""
|
22 |
+
self.client = TestClient(app)
|
23 |
+
self.df = pd.read_csv(URL)
|
24 |
+
|
25 |
+
def get_response_text2int(self, text):
|
26 |
+
"""Makes a post request to the endpoint"""
|
27 |
+
r = None
|
28 |
+
try:
|
29 |
+
r = self.client.post("/text2int", json={"content": text}) \
|
30 |
+
.json().get("message")
|
31 |
+
except:
|
32 |
+
pass
|
33 |
+
return r
|
34 |
+
|
35 |
+
def test_endpoint_text2int(self):
|
36 |
+
"""Tests if endpoint is working"""
|
37 |
+
response = self.client.post("/text2int",
|
38 |
+
json={"content": "fourteen"}
|
39 |
+
)
|
40 |
+
self.assertEqual(response.status_code, 200)
|
41 |
+
|
42 |
+
def test_acc_score_text2int(self):
|
43 |
+
"""Calculates accuracy score for endpoint"""
|
44 |
+
|
45 |
+
self.df["text2int"] = self.df["input"].apply(func=self.get_response_text2int)
|
46 |
+
self.df["score"] = self.df[["output", "text2int"]].apply(
|
47 |
+
lambda row: row[0] == row[1],
|
48 |
+
axis=1
|
49 |
+
)
|
50 |
+
self.df.to_csv(f"{DATA_DIR}/text2int_results.csv", index=False)
|
51 |
+
acc_score = self.df["score"].mean().__round__(2)
|
52 |
+
|
53 |
+
self.assertGreaterEqual(acc_score, 0.5, f"Accuracy score: '{acc_score}'. Value is too low!")
|
54 |
+
|
55 |
+
|
56 |
+
if __name__ == '__main__':
|
57 |
+
unittest.main()
|
users.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"1": {"skill_score": 0.04, "state": "question", "start": 3, "stop": 3, "step": 1, "answer": 4}}
|