Spaces:
Sleeping
Sleeping
| 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() | |
| def home(): | |
| model = encoding_model.train_model() | |
| return {"message": "Welcome to Prediction Hub"} | |
| 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}" | |
| 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}" | |
| 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)}") |