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