Spaces:
Paused
Paused
Download mental_health_api.py from MuhammadTechxone/psycho: direct link, hf CLI and curl.
- Browser
- Download file 1.27 kB
-
https://huggingface.co/spaces/MuhammadTechxone/psycho/resolve/main/mental_health_api.py
- Command line
-
hf download hf://spaces/MuhammadTechxone/psycho/mental_health_api.py
-
curl -L -o mental_health_api.py https://huggingface.co/spaces/MuhammadTechxone/psycho/resolve/main/mental_health_api.py
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 | |
| def home(): | |
| return {"message":"Welcome to psychoAPI"} | |
| 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()} | |