binaychandra commited on
Commit
1228c06
·
1 Parent(s): 1b8860b

Added similarity calculations

Browse files
Files changed (2) hide show
  1. app.py +15 -11
  2. helper.py +175 -0
app.py CHANGED
@@ -11,7 +11,7 @@ from pydantic import BaseModel
11
  from fastapi import Query
12
  from transformers import pipeline
13
 
14
- from helper import generate_random_predictions
15
 
16
  app = FastAPI()
17
 
@@ -89,20 +89,22 @@ def get_prediction_from_jobrun():
89
 
90
  classifier = pipeline("sentiment-analysis") # Defaults to distilbert-base-uncased-finetuned-sst-2-english
91
 
 
 
 
 
92
  @app.post("/get_sentiment")
93
- def get_sentiment_details(input: inputtext):
94
-
95
- result = classifier(input)
96
- print(result) # Output will be a list: [{'label': 'POSITIVE', 'score': 0.999...}]
97
 
98
- # Accessing the results:
99
  label = result[0]['label']
100
  score = result[0]['score']
101
- print(f"Sentiment: {label}, Score: {score}")
102
-
103
- return label, score
104
-
105
 
 
 
106
  @app.post("/get_prediction_on_userinput")
107
  def run_pred_pipeline(input: PredictionInput):
108
 
@@ -112,11 +114,13 @@ def run_pred_pipeline(input: PredictionInput):
112
  ## Hardcoding for testing purposes ##
113
 
114
  temp_predictions_dict = generate_random_predictions()
 
115
  data_out = {
116
  "status" : "success",
117
  "data" : {
118
  "id": input.dict()['id'],
119
- "predictions": temp_predictions_dict
 
120
  # "predictions": {
121
  # "ASDA": {
122
  # "Apr-25": 741.86,
 
11
  from fastapi import Query
12
  from transformers import pipeline
13
 
14
+ from helper import generate_random_predictions, get_sample_similarity_attr
15
 
16
  app = FastAPI()
17
 
 
89
 
90
  classifier = pipeline("sentiment-analysis") # Defaults to distilbert-base-uncased-finetuned-sst-2-english
91
 
92
+ # Define input schema
93
+ class InputText(BaseModel):
94
+ text: str # Expect JSON request body with a "text" field
95
+
96
  @app.post("/get_sentiment")
97
+ def get_sentiment_details(input: InputText):
98
+ text = input.text # Extract the actual string from the Pydantic model
99
+
100
+ print(f"===== The type of the text is : {type(text)} =====") # Debugging output
101
 
102
+ result = classifier(text) # Pass only the extracted string
103
  label = result[0]['label']
104
  score = result[0]['score']
 
 
 
 
105
 
106
+ return {"sentiment": label, "score": score}
107
+
108
  @app.post("/get_prediction_on_userinput")
109
  def run_pred_pipeline(input: PredictionInput):
110
 
 
114
  ## Hardcoding for testing purposes ##
115
 
116
  temp_predictions_dict = generate_random_predictions()
117
+ sample_sim_attr = get_sample_similarity_attr()
118
  data_out = {
119
  "status" : "success",
120
  "data" : {
121
  "id": input.dict()['id'],
122
+ "predictions": temp_predictions_dict,
123
+ "similarity_attr": sample_sim_attr
124
  # "predictions": {
125
  # "ASDA": {
126
  # "Apr-25": 741.86,
helper.py CHANGED
@@ -1,6 +1,181 @@
1
  import pandas as pd
2
  import numpy as np
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def generate_random_predictions():
5
 
6
  # Number of rows and columns
 
1
  import pandas as pd
2
  import numpy as np
3
 
4
+ def get_sample_similarity_attr():
5
+ sample_sim = {
6
+ "GB1263": {
7
+ "sell_in_volume": 44426.88,
8
+ "similarity": 66.67,
9
+ "distribution": [
10
+ 0,
11
+ 0,
12
+ 0,
13
+ 0,
14
+ 0,
15
+ 0,
16
+ 8,
17
+ 9,
18
+ 9,
19
+ 9,
20
+ 6,
21
+ 5,
22
+ 7,
23
+ 25,
24
+ 26,
25
+ 27
26
+ ],
27
+ "week_date": [
28
+ "2024-05-13",
29
+ "2024-05-20",
30
+ "2024-05-27",
31
+ "2024-06-03",
32
+ "2024-06-10",
33
+ "2024-06-17",
34
+ "2024-06-24",
35
+ "2024-07-01",
36
+ "2024-07-08",
37
+ "2024-07-15",
38
+ "2024-07-22",
39
+ "2024-07-29",
40
+ "2024-08-05",
41
+ "2024-08-12",
42
+ "2024-08-19",
43
+ "2024-08-26"
44
+ ]
45
+ },
46
+ "GB1264": {
47
+ "sell_in_volume": 26840.97,
48
+ "similarity": 66.67,
49
+ "distribution": [
50
+ 0,
51
+ 0,
52
+ 0,
53
+ 0,
54
+ 0,
55
+ 0,
56
+ 6,
57
+ 8,
58
+ 8,
59
+ 8,
60
+ 5,
61
+ 4,
62
+ 6,
63
+ 25,
64
+ 26,
65
+ 27
66
+ ],
67
+ "week_date": [
68
+ "2024-05-13",
69
+ "2024-05-20",
70
+ "2024-05-27",
71
+ "2024-06-03",
72
+ "2024-06-10",
73
+ "2024-06-17",
74
+ "2024-06-24",
75
+ "2024-07-01",
76
+ "2024-07-08",
77
+ "2024-07-15",
78
+ "2024-07-22",
79
+ "2024-07-29",
80
+ "2024-08-05",
81
+ "2024-08-12",
82
+ "2024-08-19",
83
+ "2024-08-26"
84
+ ]
85
+ },
86
+ "GB1450": {
87
+ "sell_in_volume": 32346.0,
88
+ "similarity": 66.67,
89
+ "distribution": [
90
+ 0,
91
+ 0,
92
+ 0,
93
+ 0,
94
+ 0,
95
+ 0,
96
+ 6,
97
+ 8,
98
+ 9,
99
+ 8,
100
+ 6,
101
+ 4,
102
+ 6,
103
+ 25,
104
+ 26,
105
+ 27
106
+ ],
107
+ "week_date": [
108
+ "2024-05-13",
109
+ "2024-05-20",
110
+ "2024-05-27",
111
+ "2024-06-03",
112
+ "2024-06-10",
113
+ "2024-06-17",
114
+ "2024-06-24",
115
+ "2024-07-01",
116
+ "2024-07-08",
117
+ "2024-07-15",
118
+ "2024-07-22",
119
+ "2024-07-29",
120
+ "2024-08-05",
121
+ "2024-08-12",
122
+ "2024-08-19",
123
+ "2024-08-26"
124
+ ]
125
+ },
126
+ "GB1561": {
127
+ "sell_in_volume": 3265.92,
128
+ "similarity": 77.78,
129
+ "distribution": [
130
+ 91,
131
+ 34,
132
+ 33,
133
+ 33,
134
+ 32,
135
+ 33
136
+ ],
137
+ "week_date": [
138
+ "2024-02-12",
139
+ "2024-02-19",
140
+ "2024-02-26",
141
+ "2024-03-04",
142
+ "2024-03-11",
143
+ "2024-03-18"
144
+ ]
145
+ },
146
+ "GB1650": {
147
+ "sell_in_volume": 0.0,
148
+ "similarity": 77.78,
149
+ "distribution": [
150
+ 38,
151
+ 30,
152
+ 24,
153
+ 17,
154
+ 14,
155
+ 11,
156
+ 9,
157
+ 7,
158
+ 7,
159
+ 6,
160
+ 5
161
+ ],
162
+ "week_date": [
163
+ "2024-02-12",
164
+ "2024-02-19",
165
+ "2024-02-26",
166
+ "2024-03-04",
167
+ "2024-03-11",
168
+ "2024-03-18",
169
+ "2024-03-25",
170
+ "2024-04-01",
171
+ "2024-04-08",
172
+ "2024-04-15",
173
+ "2024-04-22"
174
+ ]
175
+ }
176
+ }
177
+ return sample_sim
178
+
179
  def generate_random_predictions():
180
 
181
  # Number of rows and columns