Skip to content

Commit

Permalink
Refactoring the AI message scoring into a separate service (#71)
Browse files Browse the repository at this point in the history
* Refactoring done

* Removing int conversion

* Removing default value

* Changing the response signature
  • Loading branch information
Filienko authored Jul 24, 2024
1 parent 9445834 commit 0ca25dd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 94 deletions.
19 changes: 14 additions & 5 deletions isacc_messaging/api/isacc_record_creator.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from datetime import datetime, timedelta
from flask import current_app
import re
import requests
from typing import List, Tuple
import json

from fhirclient.models.communication import Communication
from flask import current_app
from twilio.base.exceptions import TwilioRestException

from isacc_messaging.api.email_notifications import send_message_received_notification
from isacc_messaging.api.ml_utils import predict_score
from isacc_messaging.audit import audit_entry
from isacc_messaging.models.fhir import (
HAPI_request,
Expand Down Expand Up @@ -303,12 +304,20 @@ def on_twilio_message_received(self, values):
)

def score_message(self, message):
model_path = current_app.config.get('TORCH_MODEL_PATH')
if not model_path:
ml_service_address = current_app.config.get('ML_SERVICE_ADDRESS')
if not ml_service_address:
return "routine"

try:
score = predict_score(message, model_path)
url = f'{ml_service_address}/predict_score'
response = requests.post(url, json={"message": message})
response.raise_for_status()
audit_entry(
f"predict_score call response: {response.json()}",
level='info'
)

score = response.json().get('score')
if score == 1:
return "stat"
except Exception as e:
Expand Down
84 changes: 0 additions & 84 deletions isacc_messaging/api/ml_utils.py

This file was deleted.

5 changes: 2 additions & 3 deletions isacc_messaging/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ def incoming_sms():
level="error")
return stackstr, 200
if result is not None:
# Occurs when message is incoming from unknown phone
# or request is coming from a subscribed phone number, but
# Occurs when message is incoming from unknown phone
# or request is coming from a subscribed phone number, but
# internal logic renders it invalid
audit_entry(
f"on_twilio_message_received generated error {result}",
Expand Down Expand Up @@ -301,4 +301,3 @@ def deactivate_patient(patient_id):
f"Patient {patient_id} active set to false",
level='info'
)

3 changes: 1 addition & 2 deletions isacc_messaging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_PHONE_NUMBER = os.getenv("TWILIO_PHONE_NUMBER")
TORCH_MODEL_PATH = os.getenv("TORCH_MODEL_PATH")
ML_SERVICE_ADDRESS = os.getenv("ML_SERVICE_ADDRESS")

ISACC_NOTIFICATION_EMAIL_SENDER_ADDRESS = os.getenv("ISACC_NOTIFICATION_EMAIL_SENDER_ADDRESS")
ISACC_NOTIFICATION_EMAIL_PASSWORD = os.getenv("ISACC_NOTIFICATION_EMAIL_PASSWORD")
Expand All @@ -40,4 +40,3 @@
ISACC_APP_URL = os.getenv("ISACC_APP_URL")
EMAIL_PORT = os.getenv("EMAIL_PORT", 465)
EMAIL_SERVER = os.getenv("EMAIL_SERVER", "smtp.gmail.com")

0 comments on commit 0ca25dd

Please sign in to comment.