File size: 6,471 Bytes
0a00338
fbb64ce
63b7892
12848a7
db93d88
c7be631
eccb17b
37b9dc3
8a21927
0c19103
f5e37ce
0a00338
635cab1
a0ef9e7
a44b3dc
 
 
 
c3e5080
a43b516
 
 
 
a44b3dc
 
 
 
 
c3e5080
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a44b3dc
fbb64ce
 
 
 
570c1b6
 
 
aa55588
c7be631
fbb64ce
 
4bb6e82
db93d88
37b9dc3
c7be631
 
 
 
 
 
 
 
86f043a
 
 
 
 
37b9dc3
 
db93d88
6e815d1
c7be631
570c1b6
 
c7be631
 
 
 
c2cac49
86f043a
 
 
 
 
e91a9ca
c7be631
 
 
 
0a00338
 
 
 
 
 
1e09ea3
0a00338
 
 
58be0e4
0a00338
 
 
 
 
 
86f043a
 
 
 
 
635cab1
 
0a00338
 
 
58be0e4
 
0a00338
 
63b7892
 
 
 
a823245
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from fastapi import FastAPI, File, UploadFile, HTTPException
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from ml_service import MlProcessing
from ml_service import process_single_comment
from typing import List, Dict, Union
import time
from kafka import KafkaConsumer, KafkaProducer
import asyncio
import json
import pandas as pd
from datetime import datetime

import spacy
from simpletransformers.ner import NERModel
import json
import fasttext
       


app = FastAPI()

labels_file = f"ml_models/labels.json"
ner_model_directory = f"ml_models/ner_model/"
sentiment_model_file = f"ml_models/sentiment_model/model.ft"


# LANGUAGE_MODEL = spacy.load('en_core_web_sm')
# LABELS = ['O', 'B-AMENITIES', 'I-AMENITIES', 'I-CLEANLINESS', 'B-CLEANLINESS', 'I-COMMUNICATION', 'B-COMMUNICATION', 'B-CONDITION', 'I-CONDITION', 'I-CUSTOMER_SERVICE', 'B-CUSTOMER_SERVICE', 'B-EXTERIOR_LIGHTING', 'I-EXTERIOR_LIGHTING', 'B-FINANCIAL', 'I-FINANCIAL', 'B-INTERIOR_LIGHTING', 'I-INTERIOR_LIGHTING', 'B-INTERNET', 'I-INTERNET', 'B-LANDSCAPING_GROUNDS', 'I-LANDSCAPING_GROUNDS', 'B-MAINTENANCE_CLEANLINESS', 'I-MAINTENANCE_CLEANLINESS', 'I-MAINTENANCE_SERVICE', 'B-MAINTENANCE_SERVICE', 'B-MAINTENANCE_TIMELINESS', 'I-MAINTENANCE_TIMELINESS', 'B-MOVE_IN_QUALITY', 'I-MOVE_IN_QUALITY', 'I-NOISE', 'B-NOISE', 'B-PACKAGES_MAIL', 'I-PACKAGES_MAIL', 'B-PARKING', 'I-PARKING', 'I-PESTS', 'B-PESTS', 'B-PET_WASTE', 'I-PET_WASTE', 'I-SECURITY', 'B-SECURITY', 'B-SMOKE', 'I-SMOKE', 'B-TRASH', 'I-TRASH']
# SENTIMENT_MODEL = fasttext.load_model(sentiment_model_file)

class ML_models:
    def __init__(self, sentiment_model_file):
        self.labels = ['O', 'B-AMENITIES', 'I-AMENITIES', 'I-CLEANLINESS', 'B-CLEANLINESS', 'I-COMMUNICATION', 'B-COMMUNICATION', 'B-CONDITION', 'I-CONDITION', 'I-CUSTOMER_SERVICE', 'B-CUSTOMER_SERVICE', 'B-EXTERIOR_LIGHTING', 'I-EXTERIOR_LIGHTING', 'B-FINANCIAL', 'I-FINANCIAL', 'B-INTERIOR_LIGHTING', 'I-INTERIOR_LIGHTING', 'B-INTERNET', 'I-INTERNET', 'B-LANDSCAPING_GROUNDS', 'I-LANDSCAPING_GROUNDS', 'B-MAINTENANCE_CLEANLINESS', 'I-MAINTENANCE_CLEANLINESS', 'I-MAINTENANCE_SERVICE', 'B-MAINTENANCE_SERVICE', 'B-MAINTENANCE_TIMELINESS', 'I-MAINTENANCE_TIMELINESS', 'B-MOVE_IN_QUALITY', 'I-MOVE_IN_QUALITY', 'I-NOISE', 'B-NOISE', 'B-PACKAGES_MAIL', 'I-PACKAGES_MAIL', 'B-PARKING', 'I-PARKING', 'I-PESTS', 'B-PESTS', 'B-PET_WASTE', 'I-PET_WASTE', 'I-SECURITY', 'B-SECURITY', 'B-SMOKE', 'I-SMOKE', 'B-TRASH', 'I-TRASH']
        self.languate_model = spacy.load('en_core_web_sm')
        self.sentiment_model = fasttext.load_model(sentiment_model_file)
        print("Models loaded")

ml_model = ML_models(sentiment_model_file)
LANGUAGE_MODEL = ml_model.languate_model
SENTIMENT_MODEL = ml_model.sentiment_model
LABELS = ml_model.labels
print("Models loading instances created...")


class TextRatingRequest(BaseModel):
    text: str
    rating: int

class AllTextRatingRequest(BaseModel):
    reviews : List[Dict[str, Union[str, dict, int]]]

@app.post("/predict")
def predict_single_review(text_rating: TextRatingRequest):
    text = text_rating.text
    rating = text_rating.rating
    skip = False
    raw_data = {"text": text, "star_rating": rating, "skip": skip}
    start_time = time.time()
    # ml = MlProcessing(comment_dict=raw_data)
    # processed_data = ml.main()
    # spans = processed_data.get('spans', list())
    # has_sentiments = True
    # if not any(spans):
    #     spans = [{'label': text, 'color': '', 'value': '', 'sentiment': '', 'score': ''}]
    #     has_sentiments = False
    # processed_data['spans'] = spans
    try:
        processed_data, has_sentiments = process_single_comment(raw_data, LANGUAGE_MODEL, SENTIMENT_MODEL, LABELS )
    except Exception as e:
        print("error during prediction: ", e)
        return {"error":e}
    end_time = time.time()
    print(f"Time taken to process the data : {end_time - start_time} seconds")
    return {"processed_data": processed_data, "has_sentiments":has_sentiments}

@app.post("/predict_all")
def predict_all_reviews(reviews: AllTextRatingRequest):
    reviews = reviews.reviews
    skip = False
    processed_data_list = list()
    start_time = time.time()
    for review in reviews:
        raw_data = {"text":review.get('text', str()), "star_rating":review.get('rating', 5), "skip":skip}
        try:
            processed_data, has_sentiments = process_single_comment(raw_data, LANGUAGE_MODEL, SENTIMENT_MODEL, LABELS )
        except Exception as e:
            print("error during prediction: ", e)
            return {"error":e}
        processed_data_list.append({"processed_data":processed_data, "has_sentiments":has_sentiments})
    end_time = time.time()
    print(f"Time taken to process the data : {end_time - start_time} seconds")
    return processed_data_list

@app.post("/predict_file")
def predict_file_responses(file: UploadFile = File(...)):
    if not file.filename.endswith(".csv"):
        raise HTTPException(status_code=400, detail="Only CSV files are allowed")
    try:
        df = pd.read_csv(file.file)
        df = df.fillna('')
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Error processing CSV file: {e}")
    processed_data_list = list()
    start_time = time.time()
    for index, row in df.iterrows():
        try:
            text = row['ACTUAL REVIEW']
            star_rating = row['STAR RATING']
            review_id = row['REVIEWID']
            raw_data = {"text":text, "star_rating":star_rating, "skip":False}
            try:
                processed_data, has_sentiments = process_single_comment(raw_data,LANGUAGE_MODEL, SENTIMENT_MODEL, LABELS ) 
            except Exception as e:
                print("error during prediction: ", e)
                return {"error":e}
            now = datetime.now()
            print(f"Processed review with index {index} at time {now.time()}")
            processed_data_list.append({"processed_data":processed_data, "has_sentiments":has_sentiments, "review_id":review_id})
        finally:
            pass
    end_time = time.time()
    print(f">>>>>>>>>>>>>>>>>>>>>>>> Processing time : {end_time - start_time} seconds >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    return processed_data_list

app.mount("/", StaticFiles(directory="static", html=True), name="static")

@app.get("/")
def index() -> FileResponse:
    return FileResponse(path="/static/index.html", media_type="text/html")