eaedk commited on
Commit
081c5aa
1 Parent(s): 7ccba92
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ venv/
2
+ env/
3
+ .venv/
4
+ .env/
5
+ .env
.gitkeep ADDED
File without changes
__init__.py ADDED
File without changes
article/.gitkeep ADDED
File without changes
img/.gitkeep ADDED
File without changes
main.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Union
2
+ from src.utils import make_incredible_predictions
3
+ from fastapi import FastAPI
4
+
5
+ app = FastAPI()
6
+ # /docs, page to see auto-generated API documentation
7
+
8
+ @app.get("/")
9
+ def read_root():
10
+ return {"Hello": "World", "cohort": "2"}
11
+
12
+
13
+ @app.get("/items/{item_id}")
14
+ def read_item(item_id: int, q: Union[str, None] = None):
15
+ return {"item_id": item_id, "q": q}
16
+
17
+ @app.get("/predict")
18
+ def predict(age, salary, dependentsNumber, gender):
19
+ prediction = None
20
+ # prediction = model.predict(pd.DataFrame([age, salary, dependents_number, gender]))
21
+ return {"age":age,
22
+ "salary":salary,
23
+ "dependents_number":dependentsNumber,
24
+ "gender":gender,"prediction":prediction}
25
+
26
+ @app.post("/predict")
27
+ def predict(age, salary, dependentsNumber, gender):
28
+ prediction = None
29
+ # prediction = model.predict(pd.DataFrame([age, salary, dependents_number, gender]))
30
+ return {"age":age,
31
+ "salary":salary,
32
+ "dependents_number":dependentsNumber,
33
+ "gender":gender,"prediction":prediction}
main_sentiment.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Imports
2
+ import sys
3
+ # sys.path.insert(0, '../src/')
4
+ # sys.path.insert(0, '../src')
5
+ # sys.path.insert(0, 'src/')
6
+ # sys.path.insert(0, 'src')
7
+
8
+ from typing import Union
9
+ from src.utils import preprocess
10
+ from fastapi import FastAPI
11
+ from transformers import AutoModelForSequenceClassification,AutoTokenizer, AutoConfig
12
+ import numpy as np
13
+ #convert logits to probabilities
14
+ from scipy.special import softmax
15
+
16
+ # Config
17
+
18
+ app = FastAPI()
19
+ #/docs, page to see auto-generated API documentation
20
+
21
+ #loading ML/DL components
22
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
23
+ model_path = f"Junr-syl/tweet_sentiments_analysis"
24
+ config = AutoConfig.from_pretrained(model_path)
25
+ config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
26
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
27
+
28
+ # Endpoints
29
+ @app.get("/")
30
+ def read_root():
31
+ "Home endpoint"
32
+ return {"greeting": "Hello World..!",
33
+ "cohort": "2",
34
+ }
35
+
36
+ @app.post("/predict")
37
+ def predict(text:str):
38
+ "prediction endpoint, classifying tweets"
39
+
40
+ text = preprocess(text)
41
+
42
+ # PyTorch-based models
43
+ encoded_input = tokenizer(text, return_tensors='pt')
44
+ output = model(**encoded_input)
45
+ scores = output[0][0].detach().numpy()
46
+ scores = softmax(scores)
47
+
48
+ #Process scores
49
+ ranking = np.argsort(scores)
50
+ ranking = ranking[::-1]
51
+ predicted_label = config.id2label[ranking[0]]
52
+ predicted_score = scores[ranking[0]]
53
+
54
+
55
+ return {"text":text,
56
+ "predicted_label":predicted_label,
57
+ "confidence_score":predicted_score
58
+ }
notebook/.gitkeep ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ jupyter
2
+ pandas
3
+ scikit-learn
4
+ fastapi[all]
5
+ transformers
6
+ torch
7
+ seaborn
8
+ plotly
src/.gitkeep ADDED
File without changes
src/__init__.py ADDED
File without changes
src/main.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Union
2
+ from src.utils import make_incredible_predictions
3
+ from fastapi import FastAPI
4
+
5
+ app = FastAPI()
6
+ # /docs, page to see auto-generated API documentation
7
+
8
+ @app.get("/")
9
+ def read_root():
10
+ return {"Hello": "World", "cohort": "2"}
11
+
12
+
13
+ @app.get("/items/{item_id}")
14
+ def read_item(item_id: int, q: Union[str, None] = None):
15
+ return {"item_id": item_id, "q": q}
16
+
17
+ @app.get("/predict")
18
+ def predict(age, salary, dependentsNumber, gender):
19
+ prediction = None
20
+ # prediction = model.predict(pd.DataFrame([age, salary, dependents_number, gender]))
21
+ return {"age":age,
22
+ "salary":salary,
23
+ "dependents_number":dependentsNumber,
24
+ "gender":gender,"prediction":prediction}
25
+
26
+ @app.post("/predict")
27
+ def predict(age, salary, dependentsNumber, gender):
28
+ prediction = None
29
+ # prediction = model.predict(pd.DataFrame([age, salary, dependents_number, gender]))
30
+ return {"age":age,
31
+ "salary":salary,
32
+ "dependents_number":dependentsNumber,
33
+ "gender":gender,"prediction":prediction}
src/main_sentiment.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Imports
2
+ import sys
3
+ # sys.path.insert(0, '../src/')
4
+ # sys.path.insert(0, '../src')
5
+ # sys.path.insert(0, 'src/')
6
+ # sys.path.insert(0, 'src')
7
+
8
+ from typing import Union
9
+ from src.utils import preprocess
10
+ from fastapi import FastAPI
11
+ from transformers import AutoModelForSequenceClassification,AutoTokenizer, AutoConfig
12
+ import numpy as np
13
+ #convert logits to probabilities
14
+ from scipy.special import softmax
15
+
16
+ # Config
17
+
18
+ app = FastAPI()
19
+ #/docs, page to see auto-generated API documentation
20
+
21
+ #loading ML/DL components
22
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
23
+ model_path = f"Junr-syl/tweet_sentiments_analysis"
24
+ config = AutoConfig.from_pretrained(model_path)
25
+ config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
26
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
27
+
28
+ # Endpoints
29
+ @app.get("/")
30
+ def read_root():
31
+ "Home endpoint"
32
+ return {"greeting": "Hello World..!",
33
+ "cohort": "2",
34
+ }
35
+
36
+ @app.post("/predict")
37
+ def predict(text:str):
38
+ "prediction endpoint, classifying tweets"
39
+
40
+ text = preprocess(text)
41
+
42
+ # PyTorch-based models
43
+ encoded_input = tokenizer(text, return_tensors='pt')
44
+ output = model(**encoded_input)
45
+ scores = output[0][0].detach().numpy()
46
+ scores = softmax(scores)
47
+
48
+ #Process scores
49
+ ranking = np.argsort(scores)
50
+ ranking = ranking[::-1]
51
+ predicted_label = config.id2label[ranking[0]]
52
+ predicted_score = scores[ranking[0]]
53
+
54
+
55
+ return {"text":text,
56
+ "predicted_label":predicted_label,
57
+ "confidence_score":predicted_score
58
+ }
src/utils.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def make_incredible_predictions():
2
+ "This is the best function that have created"
3
+ pass
4
+
5
+ def preprocess(text):
6
+ "preprocessing function of the input tweet"
7
+
8
+ new_text = []#initiate an empty list
9
+ #split text by space
10
+ for t in text.split(" "):
11
+ #set username to @user
12
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
13
+ #set tweet source to http
14
+ t = 'http' if t.startswith('http') else t
15
+ #store text in the list
16
+ new_text.append(t)
17
+ #change text from list back to string
18
+ return " ".join(new_text)
utils.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def make_incredible_predictions():
2
+ "This is the best function that have created"
3
+ pass
4
+
5
+ def preprocess(text):
6
+ "preprocessing function of the input tweet"
7
+
8
+ new_text = []#initiate an empty list
9
+ #split text by space
10
+ for t in text.split(" "):
11
+ #set username to @user
12
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
13
+ #set tweet source to http
14
+ t = 'http' if t.startswith('http') else t
15
+ #store text in the list
16
+ new_text.append(t)
17
+ #change text from list back to string
18
+ return " ".join(new_text)