aamirtaymoor commited on
Commit
c7be631
·
verified ·
1 Parent(s): 37b9dc3

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -9
main.py CHANGED
@@ -3,6 +3,7 @@ from pydantic import BaseModel
3
  from fastapi.staticfiles import StaticFiles
4
  from fastapi.responses import FileResponse
5
  from ml_service import MlProcessing
 
6
  import time
7
 
8
  app = FastAPI()
@@ -11,25 +12,43 @@ class TextRatingRequest(BaseModel):
11
  text: str
12
  rating: int
13
 
 
 
 
14
  @app.post("/predict")
15
- def read_root(text_rating: TextRatingRequest):
16
  text = text_rating.text
17
  rating = text_rating.rating
18
  skip = False
19
  raw_data = {"text": text, "star_rating": rating, "skip": skip}
20
  start_time = time.time()
21
- ml = MlProcessing(comment_dict=raw_data)
22
- processed_data = ml.main()
23
- spans = processed_data.get('spans', list())
24
- has_sentiments = True
25
- if not any(spans):
26
- spans = [{'label': text, 'color': '', 'value': '', 'sentiment': '', 'score': ''}]
27
- has_sentiments = False
28
- processed_data['spans'] = spans
 
29
  end_time = time.time()
30
  print(f"Time taken to process the data : {end_time - start_time} seconds")
31
  return {"processed_data": processed_data, "has_sentiments":has_sentiments}
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
34
 
35
  @app.get("/")
 
3
  from fastapi.staticfiles import StaticFiles
4
  from fastapi.responses import FileResponse
5
  from ml_service import MlProcessing
6
+ from ml_service import process_single_comment
7
  import time
8
 
9
  app = FastAPI()
 
12
  text: str
13
  rating: int
14
 
15
+ class AllTextRatingRequest(BaseModel):
16
+ reviews = List[Dict[str, float]]]
17
+
18
  @app.post("/predict")
19
+ def predict_single_review(text_rating: TextRatingRequest):
20
  text = text_rating.text
21
  rating = text_rating.rating
22
  skip = False
23
  raw_data = {"text": text, "star_rating": rating, "skip": skip}
24
  start_time = time.time()
25
+ # ml = MlProcessing(comment_dict=raw_data)
26
+ # processed_data = ml.main()
27
+ # spans = processed_data.get('spans', list())
28
+ # has_sentiments = True
29
+ # if not any(spans):
30
+ # spans = [{'label': text, 'color': '', 'value': '', 'sentiment': '', 'score': ''}]
31
+ # has_sentiments = False
32
+ # processed_data['spans'] = spans
33
+ processed_data = process_single_comment(raw_data)
34
  end_time = time.time()
35
  print(f"Time taken to process the data : {end_time - start_time} seconds")
36
  return {"processed_data": processed_data, "has_sentiments":has_sentiments}
37
 
38
+ @app.post("/predict_all")
39
+ def predict_all_reviews(reviews: AllTextRatingRequest):
40
+ reviews = list(reviews)
41
+ skip = False
42
+ processed_data_list = list()
43
+ start_time = time.time()
44
+ for review in reviews:
45
+ raw_data = {"text":review.get("text", str()), "star_rating":review.get('star_rating', 1), "skip":skip}
46
+ processed_data = process_single_comment(raw_data)
47
+ porcessed_data_list.append(processed_data)
48
+ end_time = time.time()
49
+ print(f"Time taken to process the data : {end_time - start_time} seconds")
50
+ return processed_data_list
51
+
52
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
53
 
54
  @app.get("/")