Spaces:
Sleeping
Sleeping
Ezhil
commited on
Commit
·
781c355
1
Parent(s):
d6e11bd
modified code
Browse files- Routes/classify_sms.py +0 -9
- main.py +26 -10
- models/train_models.py +0 -30
- schemas/sms_schema.py +0 -7
- services/sms_service.py +0 -14
Routes/classify_sms.py
DELETED
|
@@ -1,9 +0,0 @@
|
|
| 1 |
-
from fastapi import APIRouter
|
| 2 |
-
from schemas.sms_schema import SMSRequest, SMSResponse
|
| 3 |
-
from services.sms_service import classify_sms
|
| 4 |
-
|
| 5 |
-
classify_sms_router = APIRouter()
|
| 6 |
-
|
| 7 |
-
@classify_sms_router.post("/classify_sms", response_model=SMSResponse)
|
| 8 |
-
def classify(request: SMSRequest):
|
| 9 |
-
return classify_sms(request.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main.py
CHANGED
|
@@ -1,16 +1,32 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Initialize FastAPI app
|
| 7 |
-
app = FastAPI(
|
| 8 |
|
| 9 |
@app.get("/")
|
| 10 |
-
def home():
|
| 11 |
-
return {"Message":
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
|
| 7 |
+
# Load the pre-trained model
|
| 8 |
+
model = SentenceTransformer('Alibaba-NLP/gte-base-en-v1.5', trust_remote_code=True)
|
| 9 |
+
|
| 10 |
+
# Define request model
|
| 11 |
+
class MessageRequest(BaseModel):
|
| 12 |
+
messages: List[str]
|
| 13 |
+
|
| 14 |
+
# Define response model
|
| 15 |
+
class EmbeddingResponse(BaseModel):
|
| 16 |
+
dimensions: int # Only return embedding size
|
| 17 |
+
numeric_values: List[List[float]]
|
| 18 |
|
| 19 |
# Initialize FastAPI app
|
| 20 |
+
app = FastAPI()
|
| 21 |
|
| 22 |
@app.get("/")
|
| 23 |
+
def home ():
|
| 24 |
+
return {"Message":"Welcome to homepage, kindly proceed by giving /docs in the URL" }
|
| 25 |
|
| 26 |
+
@app.post("/embed", response_model=EmbeddingResponse)
|
| 27 |
+
def embed(request: MessageRequest):
|
| 28 |
+
new_embeddings = model.encode(request.messages, convert_to_tensor=True)
|
| 29 |
+
return EmbeddingResponse(
|
| 30 |
+
dimensions=new_embeddings.shape[1], # Return only the embedding dimension
|
| 31 |
+
numeric_values=new_embeddings.tolist()
|
| 32 |
+
)
|
models/train_models.py
DELETED
|
@@ -1,30 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
import joblib
|
| 4 |
-
from sentence_transformers import SentenceTransformer
|
| 5 |
-
from sklearn.model_selection import train_test_split
|
| 6 |
-
from sklearn.linear_model import LogisticRegression
|
| 7 |
-
|
| 8 |
-
# Load dataset
|
| 9 |
-
df = pd.read_excel("data/sms_process_data_main.xlsx")
|
| 10 |
-
|
| 11 |
-
# Load SentenceTransformer model
|
| 12 |
-
encoder_model = SentenceTransformer("Alibaba-NLP/gte-base-en-v1.5", trust_remote_code=True)
|
| 13 |
-
|
| 14 |
-
# Generate embeddings
|
| 15 |
-
embeddings = encoder_model.encode(df["MessageText"].tolist(), convert_to_numpy=True)
|
| 16 |
-
|
| 17 |
-
# Encode labels
|
| 18 |
-
label_map = {"Offer": 0, "Transaction": 1}
|
| 19 |
-
df["label"] = df["label"].map(label_map)
|
| 20 |
-
|
| 21 |
-
# Split dataset
|
| 22 |
-
X_train, X_test, y_train, y_test = train_test_split(embeddings, df["label"], test_size=0.2, random_state=42)
|
| 23 |
-
|
| 24 |
-
# Train model
|
| 25 |
-
classifier = LogisticRegression()
|
| 26 |
-
classifier.fit(X_train, y_train)
|
| 27 |
-
|
| 28 |
-
# Save trained model
|
| 29 |
-
joblib.dump(classifier, "models/sms_classifier.pkl")
|
| 30 |
-
print("Model saved as 'sms_classifier.pkl'")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
schemas/sms_schema.py
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
from pydantic import BaseModel
|
| 2 |
-
|
| 3 |
-
class SMSRequest(BaseModel):
|
| 4 |
-
text: str
|
| 5 |
-
|
| 6 |
-
class SMSResponse(BaseModel):
|
| 7 |
-
category: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
services/sms_service.py
DELETED
|
@@ -1,14 +0,0 @@
|
|
| 1 |
-
import joblib
|
| 2 |
-
from sentence_transformers import SentenceTransformer
|
| 3 |
-
import numpy as np
|
| 4 |
-
|
| 5 |
-
# Load the trained model
|
| 6 |
-
classifier = joblib.load("models/sms_classifier.pkl")
|
| 7 |
-
encoder_model = SentenceTransformer("Alibaba-NLP/gte-base-en-v1.5", trust_remote_code=True)
|
| 8 |
-
|
| 9 |
-
def classify_sms(text: str):
|
| 10 |
-
embedding = encoder_model.encode([text], convert_to_numpy=True)
|
| 11 |
-
prediction = classifier.predict(embedding)
|
| 12 |
-
|
| 13 |
-
category = "Offer" if prediction[0] == 0 else "Transaction"
|
| 14 |
-
return {"category": category}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|