changed
Browse files- Dockerfile +22 -0
- main.py +100 -0
- requirements.txt +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
COPY . /app
|
| 5 |
+
|
| 6 |
+
ENV HF_HOME=/app/.cache
|
| 7 |
+
|
| 8 |
+
RUN mkdir -p /app/.cache/huggingface/hub && \
|
| 9 |
+
chmod -R 777 /app/.cache && \
|
| 10 |
+
chmod -R 777 /app/.cache/huggingface
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
RUN pip install --upgrade pip
|
| 15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
+
|
| 17 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 18 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 19 |
+
|
| 20 |
+
EXPOSE 7860
|
| 21 |
+
|
| 22 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
# Initialize FastAPI
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Load the sentence transformer model
|
| 11 |
+
try:
|
| 12 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", trust_remote_code=True)
|
| 13 |
+
print("β
Model loaded successfully")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
raise RuntimeError(f"β Failed to load model: {str(e)}")
|
| 16 |
+
|
| 17 |
+
# Define request schemas
|
| 18 |
+
class CosineSimilarityInput(BaseModel):
|
| 19 |
+
text1: str
|
| 20 |
+
text2: str
|
| 21 |
+
|
| 22 |
+
class MessageInput(BaseModel):
|
| 23 |
+
message: str
|
| 24 |
+
|
| 25 |
+
# Load SMS dataset from Excel
|
| 26 |
+
file_path = "sms_process_data_main.xlsx"
|
| 27 |
+
df = pd.read_excel(file_path)
|
| 28 |
+
|
| 29 |
+
# Precompute embeddings
|
| 30 |
+
transactional_examples = df[df['label'] == 'Transaction']['MessageText'].tolist()
|
| 31 |
+
offer_examples = df[df['label'] == 'Offer']['MessageText'].tolist()
|
| 32 |
+
|
| 33 |
+
transactional_embeddings = [model.encode(msg, convert_to_tensor=True).cpu().numpy() for msg in transactional_examples]
|
| 34 |
+
offer_embeddings = [model.encode(msg, convert_to_tensor=True).cpu().numpy() for msg in offer_examples]
|
| 35 |
+
|
| 36 |
+
# Function to compute cosine similarity
|
| 37 |
+
def cosine_similarity(vec1, vec2):
|
| 38 |
+
norm1 = np.linalg.norm(vec1)
|
| 39 |
+
norm2 = np.linalg.norm(vec2)
|
| 40 |
+
if norm1 == 0 or norm2 == 0:
|
| 41 |
+
return 0.0 # Prevent division by zero
|
| 42 |
+
return np.dot(vec1, vec2) / (norm1 * norm2)
|
| 43 |
+
|
| 44 |
+
# π 1οΈβ£ Homepage Endpoint
|
| 45 |
+
@app.get("/")
|
| 46 |
+
async def home():
|
| 47 |
+
return {"message": "Welcome to Classification of SMS"}
|
| 48 |
+
|
| 49 |
+
# π’ 2οΈβ£ Cosine Similarity Endpoint
|
| 50 |
+
@app.post("/cosine_similarity")
|
| 51 |
+
async def compute_similarity(input_data: CosineSimilarityInput):
|
| 52 |
+
"""
|
| 53 |
+
Compute cosine similarity between two input texts.
|
| 54 |
+
"""
|
| 55 |
+
try:
|
| 56 |
+
emb1 = model.encode(input_data.text1, convert_to_tensor=True).cpu().numpy()
|
| 57 |
+
emb2 = model.encode(input_data.text2, convert_to_tensor=True).cpu().numpy()
|
| 58 |
+
similarity = cosine_similarity(emb1, emb2)
|
| 59 |
+
return {"cosine_similarity": round(float(similarity), 4)}
|
| 60 |
+
except Exception as e:
|
| 61 |
+
raise HTTPException(status_code=500, detail=f"Error computing similarity: {str(e)}")
|
| 62 |
+
|
| 63 |
+
# π© 3οΈβ£ SMS Classification Endpoint
|
| 64 |
+
@app.post("/predict_label/")
|
| 65 |
+
async def classify_message(input_data: MessageInput):
|
| 66 |
+
"""
|
| 67 |
+
Classify an SMS as either 'Transaction' or 'Offer'.
|
| 68 |
+
"""
|
| 69 |
+
try:
|
| 70 |
+
# Validate input
|
| 71 |
+
text_input = input_data.message.strip()
|
| 72 |
+
if not text_input:
|
| 73 |
+
raise HTTPException(status_code=400, detail="Input message cannot be empty")
|
| 74 |
+
|
| 75 |
+
# Encode input text
|
| 76 |
+
input_embedding = model.encode(text_input, convert_to_tensor=True).cpu().numpy()
|
| 77 |
+
|
| 78 |
+
# Compute similarity scores
|
| 79 |
+
transactional_scores = [cosine_similarity(input_embedding, emb) for emb in transactional_embeddings]
|
| 80 |
+
offer_scores = [cosine_similarity(input_embedding, emb) for emb in offer_embeddings]
|
| 81 |
+
|
| 82 |
+
# Get max similarity
|
| 83 |
+
max_transactional = max(transactional_scores, default=0)
|
| 84 |
+
max_offer = max(offer_scores, default=0)
|
| 85 |
+
|
| 86 |
+
# Determine label and probability
|
| 87 |
+
if max_transactional > max_offer:
|
| 88 |
+
label = "Transaction"
|
| 89 |
+
|
| 90 |
+
else:
|
| 91 |
+
label = "Offer"
|
| 92 |
+
|
| 93 |
+
return {
|
| 94 |
+
"label": label
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
| 99 |
+
|
| 100 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
pandas
|
| 3 |
+
scikit-learn
|
| 4 |
+
joblib
|
| 5 |
+
uvicorn
|
| 6 |
+
sentence-transformers
|
| 7 |
+
numpy
|
| 8 |
+
openpyxl
|