aamirtaymoor commited on
Commit
a44b3dc
·
verified ·
1 Parent(s): cfc58bd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -3
main.py CHANGED
@@ -12,6 +12,20 @@ import json
12
  import pandas as pd
13
  from datetime import datetime
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  app = FastAPI()
17
 
@@ -39,7 +53,7 @@ def predict_single_review(text_rating: TextRatingRequest):
39
  # spans = [{'label': text, 'color': '', 'value': '', 'sentiment': '', 'score': ''}]
40
  # has_sentiments = False
41
  # processed_data['spans'] = spans
42
- processed_data, has_sentiments = process_single_comment(raw_data)
43
  end_time = time.time()
44
  print(f"Time taken to process the data : {end_time - start_time} seconds")
45
  return {"processed_data": processed_data, "has_sentiments":has_sentiments}
@@ -52,7 +66,7 @@ def predict_all_reviews(reviews: AllTextRatingRequest):
52
  start_time = time.time()
53
  for review in reviews:
54
  raw_data = {"text":review.get('text', str()), "star_rating":review.get('rating', 5), "skip":skip}
55
- processed_data, has_sentiments = process_single_comment(raw_data)
56
  processed_data_list.append({"processed_data":processed_data, "has_sentiments":has_sentiments})
57
  end_time = time.time()
58
  print(f"Time taken to process the data : {end_time - start_time} seconds")
@@ -75,7 +89,7 @@ def predict_file_responses(file: UploadFile = File(...)):
75
  star_rating = row['STAR RATING']
76
  review_id = row['REVIEWID']
77
  raw_data = {"text":text, "star_rating":star_rating, "skip":False}
78
- processed_data, has_sentiments = process_single_comment(raw_data)
79
  now = datetime.now()
80
  print(f"Processed review with index {index} at time {now.time()}")
81
  processed_data_list.append({"processed_data":processed_data, "has_sentiments":has_sentiments, "review_id":review_id})
 
12
  import pandas as pd
13
  from datetime import datetime
14
 
15
+ import spacy
16
+ from simpletransformers.ner import NERModel
17
+ import json
18
+ import fasttext
19
+
20
+ labels_file = f"ml_models/labels.json"
21
+ ner_model_directory = f"ml_models/ner_model/"
22
+ sentiment_model_file = f"ml_models/sentiment_model/model.ft"
23
+
24
+
25
+ LANGUAGE_MODEL = spacy.load('en_core_web_sm')
26
+ 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']
27
+ SENTIMENT_MODEL = fasttext.load_model(sentiment_model_file)
28
+
29
 
30
  app = FastAPI()
31
 
 
53
  # spans = [{'label': text, 'color': '', 'value': '', 'sentiment': '', 'score': ''}]
54
  # has_sentiments = False
55
  # processed_data['spans'] = spans
56
+ processed_data, has_sentiments = process_single_comment(raw_data, LANGUAGE_MODEL, SENTIMENT_MODEL, LABELS )
57
  end_time = time.time()
58
  print(f"Time taken to process the data : {end_time - start_time} seconds")
59
  return {"processed_data": processed_data, "has_sentiments":has_sentiments}
 
66
  start_time = time.time()
67
  for review in reviews:
68
  raw_data = {"text":review.get('text', str()), "star_rating":review.get('rating', 5), "skip":skip}
69
+ processed_data, has_sentiments = process_single_comment(raw_data, LANGUAGE_MODEL, SENTIMENT_MODEL, LABELS )
70
  processed_data_list.append({"processed_data":processed_data, "has_sentiments":has_sentiments})
71
  end_time = time.time()
72
  print(f"Time taken to process the data : {end_time - start_time} seconds")
 
89
  star_rating = row['STAR RATING']
90
  review_id = row['REVIEWID']
91
  raw_data = {"text":text, "star_rating":star_rating, "skip":False}
92
+ processed_data, has_sentiments = process_single_comment(raw_data,LANGUAGE_MODEL, SENTIMENT_MODEL, LABELS )
93
  now = datetime.now()
94
  print(f"Processed review with index {index} at time {now.time()}")
95
  processed_data_list.append({"processed_data":processed_data, "has_sentiments":has_sentiments, "review_id":review_id})