Spaces:
Paused
Paused
Update testapi.py
Browse files- testapi.py +20 -5
testapi.py
CHANGED
|
@@ -5,6 +5,17 @@ import pandas as pd
|
|
| 5 |
app = FastAPI(title = "Psychotherapy")
|
| 6 |
model = joblib.load("psychotherapy_svm_model.pkl")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
class inputdata(BaseModel):
|
| 9 |
text: str
|
| 10 |
@app.get("/")
|
|
@@ -12,14 +23,18 @@ def home():
|
|
| 12 |
return {"message":"Welcome to psychoAPI"}
|
| 13 |
@app.post("/predict")
|
| 14 |
def predict(data: inputdata):
|
| 15 |
-
|
| 16 |
"text": [data.text]
|
| 17 |
})
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Remove special characters and digits (keep only letters and spaces)
|
| 20 |
-
text = ''.join([char for char in feature if char.isalpha() or char.isspace()])
|
| 21 |
# Remove extra whitespace
|
| 22 |
-
text = ' '.join(text.split())
|
| 23 |
-
prediction = model.predict(
|
| 24 |
return{"message":prediction}
|
| 25 |
|
|
|
|
| 5 |
app = FastAPI(title = "Psychotherapy")
|
| 6 |
model = joblib.load("psychotherapy_svm_model.pkl")
|
| 7 |
|
| 8 |
+
# Create a preprocessing function
|
| 9 |
+
def preprocess_text(text):
|
| 10 |
+
"""Basic text preprocessing"""
|
| 11 |
+
# Convert to lowercase
|
| 12 |
+
text = text.lower()
|
| 13 |
+
# Remove special characters and digits (keep only letters and spaces)
|
| 14 |
+
text = ''.join([char for char in text if char.isalpha() or char.isspace()])
|
| 15 |
+
# Remove extra whitespace
|
| 16 |
+
text = ' '.join(text.split())
|
| 17 |
+
return text
|
| 18 |
+
|
| 19 |
class inputdata(BaseModel):
|
| 20 |
text: str
|
| 21 |
@app.get("/")
|
|
|
|
| 23 |
return {"message":"Welcome to psychoAPI"}
|
| 24 |
@app.post("/predict")
|
| 25 |
def predict(data: inputdata):
|
| 26 |
+
df = pd.DataFrame({
|
| 27 |
"text": [data.text]
|
| 28 |
})
|
| 29 |
+
|
| 30 |
+
# Apply preprocessing
|
| 31 |
+
df['clean_text'] = df['text'].apply(preprocess_text)
|
| 32 |
+
|
| 33 |
+
#feature = feature.str.lower()
|
| 34 |
# Remove special characters and digits (keep only letters and spaces)
|
| 35 |
+
#text = ''.join([char for char in feature if char.isalpha() or char.isspace()])
|
| 36 |
# Remove extra whitespace
|
| 37 |
+
#text = ' '.join(text.split())
|
| 38 |
+
prediction = model.predict(df['clean_text'])
|
| 39 |
return{"message":prediction}
|
| 40 |
|