Spaces:
Runtime error
Runtime error
Merge staging into feature-wormhole
Browse files- CHANGELOG.md +18 -0
- app.py +21 -0
- mathtext_fastapi/intent_classification.py +6 -2
- mathtext_fastapi/nlu.py +7 -3
- requirements.txt +1 -0
- scripts/make_request.py +13 -15
CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
## [0.0.12](https://gitlab.com/tangibleai/community/mathtext-fastapi/-/tags/0.0.12)
|
| 3 |
+
|
| 4 |
+
Improve NLU capabilities
|
| 5 |
+
- Improved handling for integers (1), floats (1.0), and text numbers (one)
|
| 6 |
+
- Integrates fuzzy keyword matching for 'easier', 'exit', 'harder', 'hint', 'next', 'stop'
|
| 7 |
+
- Integrates intent classification for user messages
|
| 8 |
+
- Improved conversation management system
|
| 9 |
+
- Created a data-driven quiz prototype
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## [0.0.0](https://gitlab.com/tangibleai/community/mathtext-fastapi/-/tags/0.0.0)
|
| 13 |
+
|
| 14 |
+
Initial release
|
| 15 |
+
- Basic text to integer NLU evaluation of user responses
|
| 16 |
+
- Basic sentiment analysis evaluation of user responses
|
| 17 |
+
- Prototype conversation manager using finite state machines
|
| 18 |
+
- Support for logging of user message data
|
app.py
CHANGED
|
@@ -5,6 +5,8 @@ or
|
|
| 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
|
|
@@ -19,6 +21,20 @@ from mathtext_fastapi.v2_conversation_manager import manage_conversation_respons
|
|
| 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")
|
|
@@ -35,6 +51,11 @@ 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}!"}
|
|
|
|
| 5 |
"""
|
| 6 |
import ast
|
| 7 |
import mathactive.microlessons.num_one as num_one_quiz
|
| 8 |
+
import sentry_sdk
|
| 9 |
+
|
| 10 |
from fastapi import FastAPI, Request
|
| 11 |
from fastapi.responses import JSONResponse
|
| 12 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 21 |
from mathtext_fastapi.nlu import evaluate_message_with_nlu
|
| 22 |
from mathtext_fastapi.nlu import run_intent_classification
|
| 23 |
|
| 24 |
+
import os
|
| 25 |
+
from dotenv import load_dotenv
|
| 26 |
+
|
| 27 |
+
load_dotenv()
|
| 28 |
+
|
| 29 |
+
sentry_sdk.init(
|
| 30 |
+
dsn=os.environ.get('SENTRY_DNS'),
|
| 31 |
+
|
| 32 |
+
# Set traces_sample_rate to 1.0 to capture 100%
|
| 33 |
+
# of transactions for performance monitoring.
|
| 34 |
+
# We recommend adjusting this value in production,
|
| 35 |
+
traces_sample_rate=0.20,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
app = FastAPI()
|
| 39 |
|
| 40 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
| 51 |
return templates.TemplateResponse("home.html", {"request": request})
|
| 52 |
|
| 53 |
|
| 54 |
+
@app.get("/sentry-debug")
|
| 55 |
+
async def trigger_error():
|
| 56 |
+
division_by_zero = 1 / 0
|
| 57 |
+
|
| 58 |
+
|
| 59 |
@app.post("/hello")
|
| 60 |
def hello(content: Text = None):
|
| 61 |
content = {"message": f"Hello {content.content}!"}
|
mathtext_fastapi/intent_classification.py
CHANGED
|
@@ -41,9 +41,13 @@ def retrieve_intent_classification_model():
|
|
| 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)
|
|
|
|
| 41 |
return model
|
| 42 |
|
| 43 |
|
| 44 |
+
encoder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 45 |
+
# model = retrieve_intent_classification_model()
|
| 46 |
+
DATA_DIR = Path(__file__).parent.parent / "mathtext_fastapi" / "data" / "intent_classification_model.joblib"
|
| 47 |
+
model = load(DATA_DIR)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
def predict_message_intent(message):
|
|
|
|
|
|
|
| 51 |
tokenized_utterance = np.array([list(encoder.encode(message))])
|
| 52 |
predicted_label = model.predict(tokenized_utterance)
|
| 53 |
predicted_probabilities = model.predict_proba(tokenized_utterance)
|
mathtext_fastapi/nlu.py
CHANGED
|
@@ -110,7 +110,10 @@ def run_intent_classification(message_text):
|
|
| 110 |
]
|
| 111 |
|
| 112 |
for command in commands:
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
if ratio > 80:
|
| 115 |
nlu_response['data'] = command
|
| 116 |
nlu_response['confidence'] = ratio / 100
|
|
@@ -129,7 +132,7 @@ def evaluate_message_with_nlu(message_data):
|
|
| 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'],
|
|
@@ -141,11 +144,12 @@ def evaluate_message_with_nlu(message_data):
|
|
| 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())
|
|
|
|
| 110 |
]
|
| 111 |
|
| 112 |
for command in commands:
|
| 113 |
+
try:
|
| 114 |
+
ratio = fuzz.ratio(command, message_text.lower())
|
| 115 |
+
except:
|
| 116 |
+
ratio = 0
|
| 117 |
if ratio > 80:
|
| 118 |
nlu_response['data'] = command
|
| 119 |
nlu_response['confidence'] = ratio / 100
|
|
|
|
| 132 |
"""
|
| 133 |
# Keeps system working with two different inputs - full and filtered @event object
|
| 134 |
try:
|
| 135 |
+
message_text = str(message_data['message_body'])
|
| 136 |
except KeyError:
|
| 137 |
message_data = {
|
| 138 |
'author_id': message_data['message']['_vnd']['v1']['chat']['owner'],
|
|
|
|
| 144 |
'message_inserted_at': message_data['message']['_vnd']['v1']['chat']['inserted_at'],
|
| 145 |
'message_updated_at': message_data['message']['_vnd']['v1']['chat']['updated_at'],
|
| 146 |
}
|
| 147 |
+
message_text = str(message_data['message_body'])
|
| 148 |
|
| 149 |
# Run intent classification only for keywords
|
| 150 |
intent_api_response = run_intent_classification(message_text)
|
| 151 |
if intent_api_response['data']:
|
| 152 |
+
prepare_message_data_for_logging(message_data, intent_api_response)
|
| 153 |
return intent_api_response
|
| 154 |
|
| 155 |
number_api_resp = text2int(message_text.lower())
|
requirements.txt
CHANGED
|
@@ -11,6 +11,7 @@ sentencepiece
|
|
| 11 |
openpyxl
|
| 12 |
python-Levenshtein
|
| 13 |
sentence-transformers
|
|
|
|
| 14 |
supabase
|
| 15 |
transitions
|
| 16 |
uvicorn
|
|
|
|
| 11 |
openpyxl
|
| 12 |
python-Levenshtein
|
| 13 |
sentence-transformers
|
| 14 |
+
sentry-sdk[fastapi]
|
| 15 |
supabase
|
| 16 |
transitions
|
| 17 |
uvicorn
|
scripts/make_request.py
CHANGED
|
@@ -66,21 +66,19 @@ def run_simulated_request(endpoint, sample_answer, context=None):
|
|
| 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 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 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')
|
|
|
|
| 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('manager', '')
|
| 83 |
# run_simulated_request('manager', 'add')
|
| 84 |
# run_simulated_request('manager', 'subtract')
|