File size: 3,570 Bytes
ba600a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
import json
import os

import pandas as pd
from fastapi import UploadFile

import tasks.data.dataAugmentation as da
import tasks.data.dataEngineering as de
import tasks.data.utility as util
import tasks.training.handle_train as trainingPipeline


def augmentDataUsingVectorSpaceAlgorithm(file: UploadFile, savePath: str):
    try:
        os.makedirs(os.path.dirname(savePath), exist_ok=True)
        data = pd.read_csv(file.file)
        df = da.augmentDataWithVectorSpaceAlgorithm(data)
        df.to_csv(savePath, index=False, encoding='utf-8')
        
        return {
            "success": True,
            "message": "Training data augmented successfully",
            "data": df.head(5).to_dict()
        }
    except Exception as error:
        return {
            "success": False,
            "message": f"Training data augmentation failed. {error}",
            "data": None
        }


def getSymptomsCausesAndDiseaseNameFromJSON(file: UploadFile, savePath: str):
    try:
        os.makedirs(os.path.dirname(savePath), exist_ok=True)
        data = pd.read_csv(file.file)
        
        diseaseDict = util.getSymptomsCausesAndDiseaseNameFromJSON(data)
        
        json.dump(diseaseDict, open(savePath, 'w', encoding='utf-8'), ensure_ascii=False)
        
        return {
            "success": True,
            "message": "Symptoms, causes and disease name extracted successfully",
            "data": None
        }
    except Exception as error:
        return {
            "success": False,
            "message": f"Symptoms, causes and disease name extraction failed. {error}",
            "data": None
        }


def trainingDataFromUTagsJSON(file: UploadFile, savePath: str):
    try:
        os.makedirs(os.path.dirname(savePath), exist_ok=True)
        data = json.loads(file.file.read())
        df = de.trainingDataFromUTagsJSON(data)
        df.to_csv(savePath, index=False, encoding='utf-8')
        
        return {
            "success": True,
            "message": "Training data generated successfully",
            "data": df.head(5).to_dict()
        }
    except Exception as error:
        return {
            "success": False,
            "message": f"Training data generation failed. {error}",
            "data": None
        }


def trainingDataFromPromptsForBERT(file: UploadFile, savePath: str):
    try:
        os.makedirs(os.path.dirname(savePath), exist_ok=True)
        data = json.loads(file.file.read())
        df = de.trainingDataFromPromptsForBERT(data)
        df.to_csv(savePath, index=False, encoding='utf-8')
        
        return {
            "success": True,
            "message": "Training data generated successfully",
            "data": df.head(5).to_dict()
        }
    except Exception as error:
        return {
            "success": False,
            "message": f"Training data generation failed. {error}",
            "data": None
        }
    

def trainModelOnSageMaker(trainDataPath: str, testDataPath: str, file: UploadFile | None = None):
    try:
        hyperparameters = None
        if file is not None:
            hyperparameters = json.loads(file.file.read())
        
        trainingPipeline.train(trainDataPath, testDataPath, hyperparameters)
        
        return {
            "success": True,
            "message": "Model trained successfully",
            "data": None
        }
    except Exception as error:
        return {
            "success": False,
            "message": f"Model training failed. {error}",
            "data": None
        }