Skip to content

Commit

Permalink
interactions demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-Adly committed Sep 27, 2023
1 parent 24e4ff9 commit 810bf3b
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,39 @@ def stewardship():
return render_template("stewardship.html")


@app.route("/interactions", methods=["GET", "POST"])
def interactions():
if request.method == "POST":
# get the comma separated drugs and turn into a list, stripping all spaces
drug_list = [drug.strip().lower() for drug in request.form["drugs"].split(",")]
print(drug_list)
# send question to GalenAI server
url = f"{BASE_URL}/api/v1/create-interactions-streaming-channel/"
payload = {"drugs": drug_list}
token = f"token {TOKEN}" # your token here
headers = {"Authorization": token, "Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)

# handle errors.
if response.status_code != 200:
return render_template("interactions.html", error=response.json()["detail"])

# get a channel_name where generated text will be sent too as SSE (server sent events) as they become ready immmediately
# Each request is a new channel_name generated on the fly.
channel_name = response.json()["channel_name"]

# once the query is finished streaming, you can retrieve the full details via this query_id
# Save this in your DB to retrieve the full details later
query_id = response.json()["query_id"]
return render_template(
"listening_interactions.html",
channel_name=channel_name,
query_id=query_id,
base_url=BASE_URL,
)

return render_template("interactions.html")


if __name__ == "__main__":
app.run(debug=True)
1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ <h1>Question Form</h1>
</form>
<p> <a href="/icd10"> Go to ICD10 Code Demo </a> </p>
<p> <a href="/stewardship"> Go to StewardshipAI Demo </a> </p>
<p> <a href="/interactions"> Go to Interactions Demo </a> </p>
</body>
</html>

20 changes: 20 additions & 0 deletions templates/interactions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

<!DOCTYPE html>
<html>
<head>
<title>Itnteractions Form</title>
</head>
<body>
<h1>Interactions Form</h1>
{% if error %}
<p style="color: red;">{{ error }}</p>
{% endif %}
<form action="/interactions" method="POST">
<label for="drugs">Drugs:</label>
<input type="text" id="drugs" name="drugs" required placeholder="atorvastatin, norvasc">
<p> Enter up to 25 drugs separated by commas</p>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
109 changes: 109 additions & 0 deletions templates/listening_interactions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!DOCTYPE html>
<html>
<head>
<title>Listening Drug Interactions Page</title>
</head>
<body>
<h1>Channel Name: {{channel_name}}</h1>
<p>Query_ID = {{query_id}}</p>
<button onclick="start()">Start Listening</button> <!-- button is here for illustration, should call start on window load-->
<p id="error"> </p>
<p id="answer"> Answer: </p>
<p id="references"> References <br> </p>


<script src="{{base_url}}/static/js/galenSDK.min.js"> </script>

<script>

/*
window.onload = function() {
start();
};
*/
function start() {
// this comes from the galenAI server
var channelName = "{{ channel_name }}"
var queryID = "{{ query_id }}"

// connect to the channel and listen to events. This must be done first, before calling the ReadyTOListen function
galenConnect(channelName, queryID, function(channel) {

// start of stream
channel.bind('start', function(data) {
console.log("started stream..");
//set up your DOM for receiving the data
});

// answer
channel.bind("answer", function (data) {
// add the answer to the div
// you will get an answer event if there are no interactions
document.getElementById("answer").innerHTML += data.text;
});

// for each interaction, you will get
// new interaction event (for each new interaction)
// drug_pair event (for each interacting drug pair)
// description event (for description of the interaction)
// recommendation event
// reference event


channel.bind("new_interaction", function (data) {
let id = data.id;
// make a new div for each new interaction that has drug pair, description, recommendation, references
document.getElementById("answer").innerHTML += `<div id=${id}-drug-pair> "</div>`;

document.getElementById("answer").innerHTML += `<div id=${id}-description> "</div>`;
document.getElementById("answer").innerHTML += `<div id=${id}-recommendation> "</div>`;
document.getElementById("answer").innerHTML += `<div id=${id}-references> "</div>`;

});

channel.bind("drug_pair", function (data) {
// add the drug pair to the div

document.getElementById(`${data.id}-drug-pair`).innerHTML += data.text;
});


channel.bind("description", function (data) {
// add the description to the div
document.getElementById(`${data.id}-description`).innerHTML += data.text;
});

channel.bind("recommendation", function (data) {
// add the recommendation to the div
document.getElementById(`${data.id}-recommendation`).innerHTML += data.text;
});

channel.bind("references", function (data) {
// make a div for title, doi, url, abstract inside references
document.getElementById(`${data.id}-references`).innerHTML += "<div> title:" + data.title + "</div>";
if (data.doi != null) {
document.getElementById(`${data.id}-references`).innerHTML += "<div> doi:" + data.doi + "</div>";
document.getElementById(`${data.id}-references`).innerHTML += "<div> url:" + data.url + "</div>";
document.getElementById(`${data.id}-references`).innerHTML += "<div> abstract:" + data.abstract + "</div>";
}
});

// end
channel.bind("end", function (data) {
channel.unbind(); //unbind the channel when you are done listening
console.log("ended stream..");
// do something when the stream ends
});
});



// Call the ready to listen.
// note - this function, must be called after the channel is connected.
// only call when you are ready to receive events
galenReadyToListen(channelName, queryID);


}

</script>

0 comments on commit 810bf3b

Please sign in to comment.