Vela commited on
Commit
e10ddaa
·
1 Parent(s): 95076c1

Modified Project

Browse files
src/api/__pycache__/main.cpython-312.pyc CHANGED
Binary files a/src/api/__pycache__/main.cpython-312.pyc and b/src/api/__pycache__/main.cpython-312.pyc differ
 
src/api/main.py CHANGED
@@ -3,18 +3,23 @@ import os
3
  import sys
4
  src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "src"))
5
  sys.path.append(src_directory)
6
- from modules import logistic_regression
7
 
8
  app = FastAPI()
9
 
10
  @app.get("/")
11
  def home():
 
12
  return {"message": "Welcome to Prediction Hub"}
13
 
14
  @app.get("/predict")
15
  def display_prediction(message : str = "Hello World"):
16
  try:
17
- dimention = logistic_regression.get_label(message)
18
  return dimention
19
  except Exception as e:
20
- return f"Unable to fetch the data {e}"
 
 
 
 
 
3
  import sys
4
  src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "src"))
5
  sys.path.append(src_directory)
6
+ from modules import encoding_model
7
 
8
  app = FastAPI()
9
 
10
  @app.get("/")
11
  def home():
12
+ encoding_model.train_model()
13
  return {"message": "Welcome to Prediction Hub"}
14
 
15
  @app.get("/predict")
16
  def display_prediction(message : str = "Hello World"):
17
  try:
18
+ dimention = encoding_model.get_label(message)
19
  return dimention
20
  except Exception as e:
21
+ return f"Unable to fetch the data {e}"
22
+
23
+ # @app.post("/predict")
24
+ # def post_messsage(message, response_model ):
25
+ # logistic_regression.create_embending(message)
src/modules/__pycache__/encoding_model.cpython-312.pyc ADDED
Binary file (2.4 kB). View file
 
src/modules/__pycache__/logistic_regression.cpython-312.pyc CHANGED
Binary files a/src/modules/__pycache__/logistic_regression.cpython-312.pyc and b/src/modules/__pycache__/logistic_regression.cpython-312.pyc differ
 
src/modules/encoding_model.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import SentenceTransformer
2
+ model = SentenceTransformer('Alibaba-NLP/gte-base-en-v1.5', trust_remote_code=True)
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.linear_model import LogisticRegression
5
+ import pandas as pd
6
+ import os
7
+ import sys
8
+ src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "src"))
9
+ sys.path.append(src_directory)
10
+ from data import sample_data
11
+
12
+ encoding_model = model
13
+ logreg_model = None
14
+ X_train_embeddings = None
15
+
16
+ file_path = r"src/data/sms_process_data_main.xlsx"
17
+ df = sample_data.get_data_frame(file_path)
18
+
19
+ def train_model():
20
+ logreg_model, X_train_embeddings
21
+
22
+ if logreg_model is None:
23
+ X_train, X_test, y_train, y_test = train_test_split(df['MessageText'], df['label'], test_size=0.2, random_state=42)
24
+ X_train_embeddings = encoding_model.encode(X_train.tolist())
25
+
26
+ logreg_model = LogisticRegression(max_iter=100)
27
+ logreg_model.fit(X_train_embeddings, y_train)
28
+
29
+ def get_label(message):
30
+ if logreg_model is None:
31
+ raise ValueError("Model has not been trained yet. Please call train_model first.")
32
+
33
+ new_embeddings = encoding_model.encode([message])
34
+
35
+ prediction = logreg_model.predict(new_embeddings)
36
+
37
+ no_of_dimensions = len(new_embeddings[0])
38
+ dimension_df = pd.DataFrame(new_embeddings[0], columns=["Dimension"])
39
+ return {"Prediction": prediction[0], "Prediction_Dimension": {no_of_dimensions: dimension_df}}
src/modules/logistic_regression.py CHANGED
@@ -1,27 +1,33 @@
1
- from sentence_transformers import SentenceTransformer
2
- import pandas as pd
3
- from sklearn.model_selection import train_test_split
4
- from sklearn.linear_model import LogisticRegression
5
- from sklearn.metrics import accuracy_score, classification_report
6
- import numpy as np
7
- import os
8
- import sys
9
- src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "src"))
10
- sys.path.append(src_directory)
11
- from data import sample_data
12
 
13
- file_path = r"src/data/sms_process_data_main.xlsx"
14
- df = sample_data.get_data_frame(file_path)
15
 
16
- def get_label(message):
17
- X_train, X_test, y_train, y_test = train_test_split(df['MessageText'], df['label'], test_size=0.2, random_state=42)
18
- model = SentenceTransformer('Alibaba-NLP/gte-base-en-v1.5', trust_remote_code=True)
19
- X_train_embeddings = model.encode(X_train.tolist())
20
- models = LogisticRegression(max_iter=100)
21
- models.fit(X_train_embeddings, y_train)
22
- new_embeddings = model.encode(message)
23
- no_of_dimention = len(new_embeddings)
24
- array = np.array(new_embeddings).tolist()
25
- # new_predictions = models.predict(new_embeddings)
26
- dimention = pd.DataFrame(array,columns=["Dimention"])
27
- return {"Prediction_Dimention":{no_of_dimention: dimention}}
 
 
 
 
 
 
 
1
+ # import pandas as pd
2
+ # from sklearn.model_selection import train_test_split
3
+ # from sklearn.linear_model import LogisticRegression
4
+ # from sklearn.metrics import accuracy_score, classification_report
5
+ # import numpy as np
6
+ # import os
7
+ # import sys
8
+ # src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "src"))
9
+ # sys.path.append(src_directory)
10
+ # from data import sample_data
11
+ # from modules import encoding_model
12
 
13
+ # file_path = r"src/data/sms_process_data_main.xlsx"
14
+ # df = sample_data.get_data_frame(file_path)
15
 
16
+ # def get_label(message):
17
+ # from sentence_transformers import SentenceTransformer
18
+ # # model = SentenceTransformer('Alibaba-NLP/gte-base-en-v1.5', trust_remote_code=True)
19
+ # X_train, X_test, y_train, y_test = train_test_split(df['MessageText'], df['label'], test_size=0.2, random_state=42)
20
+ # X_train_embeddings = encoding_model.model.encode(X_train.tolist())
21
+ # models = LogisticRegression(max_iter=100)
22
+ # models.fit(X_train_embeddings, y_train)
23
+ # new_embeddings = encoding_model.model.encode(message)
24
+ # no_of_dimention = len(new_embeddings)
25
+ # array = np.array(new_embeddings).tolist()
26
+ # # new_predictions = models.predict(new_embeddings)
27
+ # dimention = pd.DataFrame(array,columns=["Dimention"])
28
+ # return {"Prediction_Dimention":{no_of_dimention: dimention}}
29
+
30
+ # def create_embending(message:str):
31
+ # embending_message = encoding_model.model.encode(message)
32
+ # result = np.array(embending_message).tolist()
33
+ # return result