File size: 1,753 Bytes
e3cf75b
c82e328
 
 
 
e10ddaa
e3cf75b
c82e328
 
 
 
 
6c2eaf8
c82e328
 
a0f1fda
 
c82e328
e3cf75b
 
 
c82e328
e10ddaa
 
a0f1fda
 
 
e3cf75b
60d1a5c
a0f1fda
e3cf75b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
import os
import sys
src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "src"))
sys.path.append(src_directory)
from modules import encoding_model
from schemas.schemas import CosineSimilarity

app = FastAPI()

@app.get("/")
def home():
    model = encoding_model.train_model()
    return {"message": "Welcome to Prediction Hub"}

@app.get("/dimention")
def display_dimention(message : str = "Hello World"):
    try:
        no_of_dimention  = encoding_model.get_prediction(message)[0]
        dimentions = encoding_model.get_prediction(message)[1]
        return {"message" : {"Prediction":{no_of_dimention:dimentions}}}
    except Exception as e:
        return f"Unable to fetch the data {e}"
    
@app.get("/prediction")
def display_prediction(message : str = "Give me a sms to predict"):
    try:
        prediction = encoding_model.get_prediction(message)[2]
        return {"message" : f"Given sms is a {prediction}"}
    except Exception as e:
        return f"Unable to fetch the data {e}"
    
@app.post("/cosine_similarity")
def display_similarity(similarity: CosineSimilarity):
    try:
        if not similarity.message_1 or not similarity.message_2:
            raise HTTPException(status_code=400, detail="Both messages must be non-empty strings.")

        cosine_similarity = encoding_model.get_cosine_similarity(similarity.message_1, similarity.message_2)

        return {
            "message_1": similarity.message_1,
            "message_2": similarity.message_2,
            "cosine_similarity": cosine_similarity
        }
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Unable to calculate cosine similarity: {str(e)}")