psycho / mental_health_api.py
MuhammadTechxone's picture
Update mental_health_api.py
0a4d809 verified
Raw History Blame Contribute Delete
1.27 kB
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd
app = FastAPI(title = "Psychotherapy")
model = joblib.load("psychotherapy_svm_model.pkl")
# Create a preprocessing function
def preprocess_text(text):
"""Basic text preprocessing"""
# Convert to lowercase
text = text.lower()
# Remove special characters and digits (keep only letters and spaces)
text = ''.join([char for char in text if char.isalpha() or char.isspace()])
# Remove extra whitespace
text = ' '.join(text.split())
return text
class inputdata(BaseModel):
text: str
@app.get("/")
def home():
return {"message":"Welcome to psychoAPI"}
@app.post("/predict")
def predict(data: inputdata):
df = pd.DataFrame({
"text": [data.text]
})
# Apply preprocessing
df['clean_text'] = df['text'].apply(preprocess_text)
#feature = feature.str.lower()
# Remove special characters and digits (keep only letters and spaces)
#text = ''.join([char for char in feature if char.isalpha() or char.isspace()])
# Remove extra whitespace
#text = ' '.join(text.split())
prediction = model.predict(df['clean_text'])
return {"message": prediction.tolist()}