Ezhil commited on
Commit
17c8f2e
·
1 Parent(s): 989f2d4

Added endpoint in main and changes in dockerfile

Browse files
Files changed (2) hide show
  1. Dockerfile +3 -2
  2. main.py +41 -5
Dockerfile CHANGED
@@ -20,7 +20,8 @@ RUN pip install --no-cache-dir -r requirements.txt
20
  COPY . .
21
 
22
  # Expose the FastAPI default port
23
- EXPOSE 7860
24
 
25
  # Run FastAPI with Uvicorn
26
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
20
  COPY . .
21
 
22
  # Expose the FastAPI default port
23
+ EXPOSE 8000
24
 
25
  # Run FastAPI with Uvicorn
26
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
27
+
main.py CHANGED
@@ -1,27 +1,34 @@
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):
@@ -31,6 +38,13 @@
31
  # numeric_values=new_embeddings.tolist()
32
  # )
33
 
 
 
 
 
 
 
 
34
  from fastapi import FastAPI
35
  from pydantic import BaseModel
36
  from typing import List
@@ -48,6 +62,9 @@ class CosineSimilarityRequest(BaseModel):
48
  text1: str
49
  text2: str
50
 
 
 
 
51
  # Define response models
52
  class EmbeddingResponse(BaseModel):
53
  dimensions: int # Only return embedding size
@@ -56,6 +73,9 @@ class EmbeddingResponse(BaseModel):
56
  class CosineSimilarityResponse(BaseModel):
57
  similarity: float
58
 
 
 
 
59
  # Initialize FastAPI app
60
  app = FastAPI()
61
 
@@ -76,3 +96,19 @@ def cosine_similarity(request: CosineSimilarityRequest):
76
  embeddings = model.encode([request.text1, request.text2], convert_to_tensor=True)
77
  cos_sim = np.dot(embeddings[0], embeddings[1]) / (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1]))
78
  return CosineSimilarityResponse(similarity=cos_sim)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # from fastapi import FastAPI
2
  # from pydantic import BaseModel
3
+ # from typing import List
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 models
11
  # class MessageRequest(BaseModel):
12
  # messages: List[str]
13
 
14
+ # class CosineSimilarityRequest(BaseModel):
15
+ # text1: str
16
+ # text2: str
17
+
18
+ # # Define response models
19
  # class EmbeddingResponse(BaseModel):
20
  # dimensions: int # Only return embedding size
21
  # numeric_values: List[List[float]]
22
 
23
+ # class CosineSimilarityResponse(BaseModel):
24
+ # similarity: float
25
+
26
  # # Initialize FastAPI app
27
  # app = FastAPI()
28
 
29
  # @app.get("/")
30
+ # def home():
31
+ # return {"Message": "Welcome to homepage, kindly proceed by giving /docs in the URL"}
32
 
33
  # @app.post("/embed", response_model=EmbeddingResponse)
34
  # def embed(request: MessageRequest):
 
38
  # numeric_values=new_embeddings.tolist()
39
  # )
40
 
41
+ # @app.post("/cosine_similarity", response_model=CosineSimilarityResponse)
42
+ # def cosine_similarity(request: CosineSimilarityRequest):
43
+ # embeddings = model.encode([request.text1, request.text2], convert_to_tensor=True)
44
+ # cos_sim = np.dot(embeddings[0], embeddings[1]) / (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1]))
45
+ # return CosineSimilarityResponse(similarity=cos_sim)
46
+
47
+
48
  from fastapi import FastAPI
49
  from pydantic import BaseModel
50
  from typing import List
 
62
  text1: str
63
  text2: str
64
 
65
+ class SMSClassificationRequest(BaseModel):
66
+ text: str
67
+
68
  # Define response models
69
  class EmbeddingResponse(BaseModel):
70
  dimensions: int # Only return embedding size
 
73
  class CosineSimilarityResponse(BaseModel):
74
  similarity: float
75
 
76
+ class SMSClassificationResponse(BaseModel):
77
+ category: str
78
+
79
  # Initialize FastAPI app
80
  app = FastAPI()
81
 
 
96
  embeddings = model.encode([request.text1, request.text2], convert_to_tensor=True)
97
  cos_sim = np.dot(embeddings[0], embeddings[1]) / (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1]))
98
  return CosineSimilarityResponse(similarity=cos_sim)
99
+
100
+ @app.post("/classify_sms", response_model=SMSClassificationResponse)
101
+ def classify_sms(request: SMSClassificationRequest):
102
+ offer_keywords = ["discount", "offer", "sale", "deal", "promo", "free"]
103
+ transaction_keywords = ["payment", "transaction", "debit", "credit", "purchase", "order"]
104
+
105
+ text_lower = request.text.lower()
106
+
107
+ if any(word in text_lower for word in offer_keywords):
108
+ category = "offer"
109
+ elif any(word in text_lower for word in transaction_keywords):
110
+ category = "transaction"
111
+ else:
112
+ category = "unknown"
113
+
114
+ return SMSClassificationResponse(category=category)