-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (130 loc) · 5.42 KB
/
main.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
163
from fastapi import FastAPI, Form, File, UploadFile
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
import aiofiles
import shutil
import os
from fer import FER
import matplotlib.pyplot as plt
app = FastAPI()
app.mount("/static", StaticFiles(directory="public"), name="static")
@app.get("/", response_class=HTMLResponse)
async def getRoot():
async with aiofiles.open("public/index.html", mode="r") as f:
data = await f.read()
return data
@app.get("/login", response_class=HTMLResponse)
async def getlogin():
async with aiofiles.open("public/login/login.html", mode="r") as f:
data = await f.read()
return data
@app.get("/photo", response_class=HTMLResponse)
async def getPhoto():
async with aiofiles.open("public/photo/index.html", mode="r") as f:
data = await f.read()
return data
@app.get('/detect', response_class=HTMLResponse)
async def proceed():
async with aiofiles.open("public/photo/emotions.html", mode="r") as f:
data = await f.read()
return data
@app.get("/questionnaire", response_class=HTMLResponse)
async def getQuestionnaireHome():
async with aiofiles.open("public/questionnaire/index.html", mode="r") as f:
data = await f.read()
return data
@app.get("/questionnaire/q", response_class=HTMLResponse)
async def getQuestionnaire():
async with aiofiles.open("public/questionnaire/q/index.html", mode="r") as f:
data = await f.read()
return data
@app.post("/questionnaire/q", response_class=HTMLResponse)
async def postQuestionnaire(
question0: str = Form(...),
question1: str = Form(...),
question2: str = Form(...),
question3: str = Form(...),
question4: str = Form(...),
question5: str = Form(...),
question6: str = Form(...),
question7: str = Form(...),
question8: str = Form(...),
question9: str = Form(...),
question10: str = Form(...),
question11: str = Form(...),
question12: str = Form(...),
question13: str = Form(...),
question14: str = Form(...),
question15: str = Form(...),
question16: str = Form(...),
question17: str = Form(...),
question18: str = Form(...),
question19: str = Form(...),
question20: str = Form(...),
):
anxietyScore = int((sum([ord(x) - ord('a') for x in [question0, question1,
question2, question3, question4, question5, question6]])/7)*5)
stressScore = int((sum([ord(x) - ord('a') for x in [question7, question8,
question9, question10, question11, question12, question13]])/7)*5)
depressionScore = int((sum([ord(x) - ord('a') for x in [question14, question15,
question16, question17, question18, question19, question20]])/7)*5)
anxietyLabel = "Low" if anxietyScore <= 10 / \
3 else "Medium" if anxietyScore <= 20/3 else "High"
stressLabel = "Low" if stressScore <= 10 / \
3 else "Medium" if stressScore <= 20/3 else "High"
depressionLabel = "Low" if depressionScore <= 10 / \
3 else "Medium" if depressionScore <= 20/3 else "High"
async with aiofiles.open("public/questionnaire/q/result.html", mode="r") as f:
data = await f.read()
return data.replace('<!-- anxiety-score -->', anxietyLabel).replace('<!-- stress-score -->', stressLabel).replace('<!-- depression-score -->', depressionLabel)
@app.get("/questionnaire/selfHelpAnxiety", response_class=HTMLResponse)
async def getanxiety():
async with aiofiles.open("public/questionnaire/selfHelp/anxiety.html", mode="r") as f:
data = await f.read()
return data
@app.get("/questionnaire/selfHelpDepp", response_class=HTMLResponse)
async def getdepression():
async with aiofiles.open("public/questionnaire/selfHelp/depression.html", mode="r") as f:
data = await f.read()
return data
@app.get("/questionnaire/selfHelpStress", response_class=HTMLResponse)
async def getstress():
async with aiofiles.open("public/questionnaire/selfHelp/stress.html", mode="r") as f:
data = await f.read()
return data
@app.get("/vent", response_class=HTMLResponse)
async def getVent():
async with aiofiles.open("public/vent/index.html", mode="r") as f:
data = await f.read()
return data
@app.get("/gethelp", response_class=HTMLResponse)
async def gethelp():
async with aiofiles.open("public\counsellor\index.html", mode="r") as f:
data = await f.read()
return data
@app.get("/about", response_class=HTMLResponse)
async def getabout():
async with aiofiles.open("public/about/index.html", mode="r") as f:
data = await f.read()
return data
@app.post("/upload")
def upload_save(image: UploadFile = File(...)):
save_file(image, path="public/photo/temp", save_as="temp")
return{"text": "File Uploaded Successfully"}
@app.post("/predict")
def emotions():
img = plt.imread("public/photo/temp/temp.jpg")
detector = FER(mtcnn=True)
key_value = detector.detect_emotions(img)[0]['emotions']
emo = {}
sorted_keys = sorted(key_value, key=key_value.get, reverse=True)
for w in sorted_keys:
emo[w] = key_value[w]
emotions = list(emo.keys())[0:3]
return {"emo": emotions}
def save_file(uploaded_file, path=".", save_as="default"):
extension = os.path.splitext(uploaded_file.filename)[-1]
temp_file = os.path.join(path, save_as + extension)
with open(temp_file, "wb") as buffer:
shutil.copyfileobj(uploaded_file.file, buffer)
return temp_file