-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (41 loc) · 1.63 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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware # Import the CORS middleware
from pydantic import BaseModel, Field
from typing import Optional
from typing import List
from inferencing import load_and_predict
from VectorSearch import getChatID
app = FastAPI()
# Add CORS middleware to allow requests from your Vue.js application
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Update with the actual origin of your Vue.js app
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class MyInput(BaseModel):
inputs: List[str]
@app.post("/getChatRoomID")
async def getChatroomId(data: MyInput):
inputs = data.inputs
chatID = getChatID(inputs)
return chatID
@app.post("/process_data")
async def process_data(data: MyInput):
try:
inputs = data.inputs
print(f"Received Inputs: {inputs}")
model_path = 'stress_classifier_model.h5'
predicted_stress_levels = load_and_predict(model_path, inputs)
for message, stress_level in zip(inputs, predicted_stress_levels):
print(f"Message: {message} --> Predicted Stress Level: {stress_level}")
predicted_stress_levels = load_and_predict(model_path, inputs)
total_stress = sum(predicted_stress_levels)
average_stress = total_stress / len(predicted_stress_levels)
final_stress = int(average_stress)
print(f"Average Stress Level: {final_stress}")
response = {"Final Stress Level": str(final_stress)}
return response
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))