-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (59 loc) · 2.26 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask, render_template, request, jsonify
from utils import preprocess, predict
import json
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
import os
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('inference.html')
@app.route('/analyze', methods=['POST'])
def inference():
global age
global sex
global predictions
age = request.form['age']
sex = request.form['sex']
left_scan_file = request.files['left-scan']
right_scan_file = request.files['right-scan']
left_img = preprocess(left_scan_file)
right_img = preprocess(right_scan_file)
predictions = predict(left=left_img, right=right_img, age=age, sex=sex)
return json.dumps(predictions)
load_dotenv()
apikey = os.getenv("OPENAI_API_KEY")
@app.route('/cgpt_suggest', methods=['POST'])
def CGTP_Suggestions():
llm = ChatOpenAI()
medical_QA_template = ChatPromptTemplate.from_messages(
[
(
"system",
f"""You are a opthalamologist. You are tasked to provide the following sections:
1. Disease description
2. Possible cures
3. Next steps.
You will be provided with pateint details and multiclass disease predictions from an AI model.
Consider only those deseases whose confidence is higher than 50%.
If none of the diseases have a confidence percentage higher than 50%, then select the disease with highest confidence.
Patient details contains the age, sex and predictions contains the output probabilities for each of the class of diseases.
Note:
1. Ignore the class "others".
2. While providing information, keep in consideration the patient's age and sex.
""",
),
("user", "{input}"),
]
)
medical_QA_chain = medical_QA_template | llm
output = medical_QA_chain.invoke(
{"input": f"Age: {age}, Sex: {sex}, Confidences:{predictions}"}
).content
response = {
'cgpt_text': output
}
return jsonify(response)
if __name__=='__main__':
app.run(port=3000, debug=True)