MuhammadTechxone commited on
Commit
32a4588
·
verified ·
1 Parent(s): d1aa2dc

Update testapi.py

Browse files
Files changed (1) hide show
  1. testapi.py +22 -15
testapi.py CHANGED
@@ -2,39 +2,46 @@ from fastapi import FastAPI
2
  from pydantic import BaseModel
3
  import joblib
4
  import pandas as pd
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("/")
22
  def home():
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
-
 
 
 
 
 
2
  from pydantic import BaseModel
3
  import joblib
4
  import pandas as pd
5
+
6
+ # Add this for Hugging Face Spaces
7
+ import sys
8
+ print("Python path:", sys.path)
9
+
10
+ app = FastAPI(title="Psychotherapy")
11
+
12
+ # Add this middleware for Hugging Face Spaces
13
+ @app.middleware("http")
14
+ async def add_process_time_header(request, call_next):
15
+ response = await call_next(request)
16
+ return response
17
+
18
  model = joblib.load("psychotherapy_svm_model.pkl")
19
 
 
20
  def preprocess_text(text):
21
  """Basic text preprocessing"""
 
22
  text = text.lower()
 
23
  text = ''.join([char for char in text if char.isalpha() or char.isspace()])
 
24
  text = ' '.join(text.split())
25
  return text
26
 
27
  class inputdata(BaseModel):
28
  text: str
29
+
30
  @app.get("/")
31
  def home():
32
+ return {"message": "Welcome to psychoAPI"}
33
+
34
  @app.post("/predict")
35
  def predict(data: inputdata):
36
  df = pd.DataFrame({
37
  "text": [data.text]
38
  })
39
 
 
40
  df['clean_text'] = df['text'].apply(preprocess_text)
 
 
 
 
 
 
41
  prediction = model.predict(df['clean_text'])
42
+
43
+ # Convert prediction to list if it's numpy array for JSON serialization
44
+ if hasattr(prediction, 'tolist'):
45
+ prediction = prediction.tolist()
46
+
47
+ return {"message": prediction}