forked from Jonathan-Adly/clair_streaming_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7019ec5
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Environments | ||
.env | ||
.env.dev | ||
dev.env | ||
.env.staging | ||
.env.staging.db | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
|
||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import requests | ||
from flask import Flask, render_template, request | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route("/", methods=['GET', 'POST']) | ||
def home(): | ||
if request.method == 'POST': | ||
query = request.form['question'] # get question from user | ||
# send question to GalenAI server | ||
url = "https://galenai.co/api/v1/get-clinical-query-streaming-channel/" | ||
payload = {'query': query} | ||
token = f"token {your_token}" #your token here | ||
headers = {'Authorization': token, 'Content-Type': 'application/json'} | ||
response = requests.post(url, json=payload, headers=headers) | ||
|
||
# by default, GalenAI will not process queries outside of scope of the model. | ||
if response.status_code != 200: | ||
return render_template('index.html', error=response.json()['details']) | ||
|
||
# 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'] | ||
|
||
# client will call ready to listen endpoint, then listen to the SSE as they become ready | ||
return render_template('listening.html', channel_name=channel_name, query_id=query_id) | ||
|
||
return render_template('index.html') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Question Form</title> | ||
</head> | ||
<body> | ||
<h1>Question Form</h1> | ||
{% if error %} | ||
<p style="color: red;">{{ error }}</p> | ||
{% endif %} | ||
<form action="/" method="POST"> | ||
<label for="question">Question:</label> | ||
<input type="text" id="question" name="question" required> | ||
<br><br> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Listening 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="context"> Context: </p> | ||
<p id="references"> References <br> </p> | ||
|
||
<script src="https:/galenai.co/static/js/galenSDK.min.js"> </script> | ||
|
||
<script> | ||
|
||
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) { | ||
document.getElementById("answer").innerHTML += data.text; | ||
}); | ||
|
||
// context | ||
channel.bind("context", function (data) { | ||
document.getElementById("context").innerHTML += data.text; | ||
}); | ||
|
||
// references | ||
channel.bind("references", function (data) { | ||
// make a div for title, doi, url, abstract inside references | ||
document.getElementById("references").innerHTML += "<div> title:" + data.title + "</div>"; | ||
if (data.doi != null) { | ||
document.getElementById("references").innerHTML += "<div> doi:" + data.doi + "</div>"; | ||
document.getElementById("references").innerHTML += "<div> url:" + data.url + "</div>"; | ||
document.getElementById("references").innerHTML += "<div> abstract:" + data.abstract + "</div>"; | ||
} | ||
}); | ||
|
||
// end | ||
channel.bind("end", function (data) { | ||
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> |