-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.py
162 lines (133 loc) · 6.49 KB
/
api.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import camelot
from PyPDF2 import PdfWriter, PdfReader
from flask import Flask, jsonify, request
import os
from flask_cors import CORS
import google.generativeai as palm
from supabase import create_client, Client
import config
import json
import random, string
palm_api_key = config.API_KEYS['palm']
palm.configure(api_key=palm_api_key)
supabase_url = 'https://nsmgorwiselturjwrosc.supabase.co'
supabase_key = config.API_KEYS['supa_pwd']
supabase: Client = create_client(supabase_url, supabase_key)
defaults = {
"model": "models/chat-bison-001",
"temperature": 0.25,
"candidate_count": 1,
"top_k": 40,
"top_p": 0.95,
}
context = "Do not be verbose and be. Your response should be restricted to 1 word. If your response is more than 1 word you will be severly punished."
#need to modify examples based on our use case
messages = []
app = Flask(__name__)
@app.route("/", methods=["GET"])
def hello_world():
return jsonify({"message": "Hello, World!"})
@app.route("/fieldmapping", methods=["POST"])
def fieldmap():
print(request.form)
if "text" not in request.form:
return jsonify({"error": "No text provided"})
chatMessage = request.form["text"]
with open("applehealthdata.json", "r") as file:
applehealthdata = json.load(file)
example = [
["From the question 'how is my heart condition?', map the question down to one field given in the list ['calories','activeMinutes','distance','floors','activeScore','activityCalories','caloriesBMR','caloriesOut','distances','activity','elevation','fairlyActiveMinutes','floors','lightlyActiveMinutes','marginalCalories','restingHeartRate','sedentaryMinutes','steps','veryActiveMinutes','age','gender','height','weight']. Say null, If none of the fields in the list are relatable.",
"restingHeartRate"
],
["From the question 'what is the status of my acl condition?', map the question down to one field given in the list ['calories','activeMinutes','distance','floors','activeScore','activityCalories','caloriesBMR','caloriesOut','distances','activity','elevation','fairlyActiveMinutes','floors','lightlyActiveMinutes','marginalCalories','restingHeartRate','sedentaryMinutes','steps','veryActiveMinutes','age','gender','height','weight']. Say null, If none of the fields in the list are relatable.",
"null"
]
]
prompt = "From the question {}, map the question down to one field given in the list ['calories','activeMinutes','distance','floors','activeScore','activityCalories','caloriesBMR','caloriesOut','distances','activity','elevation','fairlyActiveMinutes','floors','lightlyActiveMinutes','marginalCalories','restingHeartRate','sedentaryMinutes','steps','veryActiveMinutes','age','gender','height','weight']. Say null, If none of the fields in the list are relatable.".format(chatMessage)
messages.append(prompt)
# response = palm.chat(**defaults, prompt=prompt)
response = palm.chat(**defaults, messages=messages, examples=example,context=context)
# print(response.last)
resp = response.last
print("resp",resp )
# data_dict = resp.split("```json")[1]
# print(data_dict)
return resp
# return jsonify({"response": response.last})
@app.route("/upload", methods=["POST"])
def upload_file():
if "file" not in request.files:
return jsonify({"error": "No file uploaded"})
file = request.files["file"]
if file.filename == "":
return jsonify({"error": "No file selected"})
if file:
# pdfFile = ''.join(random.choices(string.ascii_letters + string.digits, k=8))+".pdf"
pdfFile = file.filename
pdfFile = pdfFile.replace(" ", "")
print(pdfFile)
print(pdfFile)
file.save(pdfFile)
csvs=[]
res = supabase.storage.from_('files').list()
flag=0
for fil in res:
if fil['name'] == pdfFile:
flag=1
break
print("bucket files: ",res)
if flag==0:
with open(pdfFile, 'rb') as f:
supabase.storage.from_("files").upload(file=f,path=pdfFile)
inputpdf = PdfReader(open(pdfFile, "rb"))
pages = []
for i in range(len(inputpdf.pages)):
output = PdfWriter()
output.add_page(inputpdf.pages[i])
pages.append("document-page%s.pdf" % i)
with open("document-page%s.pdf" % i, "wb") as outputStream:
output.write(outputStream)
i = 0
for page in pages:
tables = camelot.read_pdf(page)
i = 0
print(tables)
for table in tables:
print(tables[i].df)
tables[i].to_csv(page + str(i) + "_.csv")
with open((page + str(i) + "_.csv"), 'rb') as f:
supabase.storage.from_("files").upload(file=f,path=pdfFile[:-4]+"/"+(page + str(i) + "_.csv"))
csvs.append(page + str(i) + "_.csv")
i += 1
return jsonify({"csvs": csvs})
return jsonify({"error": "Invalid file type"})
@app.route("/chat", methods=["POST"])
def chat():
print(request.form)
if "text" not in request.form:
return jsonify({"error": "No text provided"})
chatMessage = request.form["text"]
messages.append(chatMessage)
with open("applehealthdata.json", "r") as file:
applehealthdata = json.load(file)
example = {
"keyInsight": ["specific insights on the question asked by the user"],
"exercise": [
"specific exercises names and regimes and timing to help mitigate the issue"
],
"foodRecipe": ["recipes that will help mitigate the issue"],
"doctorNeededForFurtherDetails": "yes or no depending on the issue stated by the user",
}
prompt = "You will be given patient history as follows-> {}. Based on the user question asked as stated here -> {}, give a response in a dictionary format which gives specific key insights that answer the specific question asked by the user and provides exercises, food and tips on how to mitigate the situation. Respond in JSON. An example of your expected response is as follows - {}. Do not add any more information to it".format(
applehealthdata, chatMessage, example
)
response = palm.chat(**defaults, prompt=prompt)
# print(response.last)
resp = response.last
print("resp",resp )
data_dict = resp.split("```json")[1]
print(data_dict)
return data_dict
# return jsonify({"response": response.last})
if __name__ == "__main__":
app.run(debug=True)