File size: 3,527 Bytes
58dc04e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c76ade6
58dc04e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from sentence_transformers import SentenceTransformer,util
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import uvicorn
import numpy as np
import pandas as pd


# Initialize the FastAPI app
app = FastAPI()

# Load the pre-trained SentenceTransformer model
model = SentenceTransformer("Alibaba-NLP/gte-base-en-v1.5", trust_remote_code=True)

# Define the request body schema
class TextInput(BaseModel):
    text: str

# Home route
@app.get("/")
async def home():
    return {"message": "welcome to home page"}

# Define the API endpoint for generating embeddings
@app.post("/embed")
async def generate_embedding(text_input: TextInput):
    """

    Generate a 768-dimensional embedding for the input text.

    Returns the embedding in a structured format with rounded values.

    """
    try:
        # Generate the embedding
        embedding = model.encode(text_input.text, convert_to_tensor=True).cpu().numpy()

        # Round embedding values to 2 decimal places
        rounded_embedding = np.round(embedding, 2).tolist()

        # Return structured response
        return {
            "dimensions": len(rounded_embedding),
            "embeddings": [rounded_embedding]
        }

    except Exception as e:
        # Handle any errors
        raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
    

# Load pre-trained SentenceTransformer model
model = SentenceTransformer("Alibaba-NLP/gte-base-en-v1.5", trust_remote_code=True)

# Train the Logistic Regression model during app startup
df = pd.read_excel("sms_process_data_main.xlsx")
X_train, X_test, y_train, y_test = train_test_split(df["MessageText"], df["label"], test_size=0.2, random_state=42)
X_train_embeddings = model.encode(X_train.tolist())

# Initialize and train the Logistic Regression model
logreg_model = LogisticRegression(max_iter=100)
logreg_model.fit(X_train_embeddings, y_train)

# Define input schema
class TextInput(BaseModel):
    text: str

@app.post("/prediction")
async def generate_prediction(text_input: TextInput):
    """

    Predict the label for the given text input using the trained model.

    """
    try:
        # Generate embedding for the input text
        new_embedding = model.encode([text_input.text])

        # Predict the label using the trained Logistic Regression model
        prediction = logreg_model.predict(new_embedding).tolist()[0]  # Extract single prediction

        # Return structured response
        return {
            
            "predicted_label": prediction
        }
    except Exception as e:
        # Handle any errors
        raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
    
class SentencesInput(BaseModel):
    sentence1: str
    sentence2: str
@app.post("/text_to_tensor")
def text_to_tensor(input: SentencesInput):
    try:
        # Generate embeddings
        embeddings = model.encode([input.sentence1, input.sentence2])

        # Compute cosine similarity
        cosine_similarity = util.cos_sim(embeddings[0], embeddings[1]).item()

        return {"cosine_similarity": round(cosine_similarity, 3)}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)