from fastapi import FastAPI from pydantic import BaseModel import joblib import pandas as pd # Add this for Hugging Face Spaces import sys print("Python path:", sys.path) app = FastAPI(title="Psychotherapy") # Add this middleware for Hugging Face Spaces @app.middleware("http") async def add_process_time_header(request, call_next): response = await call_next(request) return response model = joblib.load("psychotherapy_svm_model.pkl") def preprocess_text(text): """Basic text preprocessing""" text = text.lower() text = ''.join([char for char in text if char.isalpha() or char.isspace()]) 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] }) df['clean_text'] = df['text'].apply(preprocess_text) prediction = model.predict(df['clean_text']) # Convert prediction to list if it's numpy array for JSON serialization if hasattr(prediction, 'tolist'): prediction = prediction.tolist() return {"message": prediction} # At the bottom of your file, add: if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860)