Spaces:
Sleeping
Sleeping
working version
Browse files- main.py +0 -33
- main_sentiment.py +0 -58
- src/main_sentiment.py +31 -23
main.py
DELETED
@@ -1,33 +0,0 @@
|
|
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
DELETED
@@ -1,58 +0,0 @@
|
|
1 |
-
# Imports
|
2 |
-
import os
|
3 |
-
from typing import Union
|
4 |
-
from src.utils import preprocess
|
5 |
-
from fastapi import FastAPI
|
6 |
-
from transformers import AutoModelForSequenceClassification,AutoTokenizer, AutoConfig
|
7 |
-
import numpy as np
|
8 |
-
#convert logits to probabilities
|
9 |
-
from scipy.special import softmax
|
10 |
-
|
11 |
-
# Config
|
12 |
-
|
13 |
-
app = FastAPI()
|
14 |
-
#/docs, page to see auto-generated API documentation
|
15 |
-
|
16 |
-
#loading ML/DL components
|
17 |
-
os.environ['SENTENCE_TRANSFORMERS_HOME'] = './.cache'
|
18 |
-
tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
|
19 |
-
model_path = f"Junr-syl/tweet_sentiments_analysis"
|
20 |
-
config = AutoConfig.from_pretrained(model_path)
|
21 |
-
config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
|
22 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
23 |
-
|
24 |
-
# Endpoints
|
25 |
-
@app.get("/")
|
26 |
-
def read_root():
|
27 |
-
"Home endpoint"
|
28 |
-
return {"greeting": "Hello World..!",
|
29 |
-
"cohort": "2",
|
30 |
-
}
|
31 |
-
|
32 |
-
@app.post("/predict")
|
33 |
-
def predict(text:str):
|
34 |
-
"prediction endpoint, classifying tweets"
|
35 |
-
try:
|
36 |
-
text = preprocess(text)
|
37 |
-
|
38 |
-
# PyTorch-based models
|
39 |
-
encoded_input = tokenizer(text, return_tensors='pt')
|
40 |
-
output = model(**encoded_input)
|
41 |
-
scores = output[0][0].detach().numpy()
|
42 |
-
scores = softmax(scores)
|
43 |
-
|
44 |
-
#Process scores
|
45 |
-
ranking = np.argsort(scores)
|
46 |
-
ranking = ranking[::-1]
|
47 |
-
predicted_label = config.id2label[ranking[0]]
|
48 |
-
predicted_score = scores[ranking[0]]
|
49 |
-
|
50 |
-
|
51 |
-
return {"text":text,
|
52 |
-
"predicted_label":predicted_label,
|
53 |
-
"confidence_score":predicted_score
|
54 |
-
}
|
55 |
-
except Exception as e:
|
56 |
-
return {
|
57 |
-
"error": e
|
58 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/main_sentiment.py
CHANGED
@@ -1,10 +1,5 @@
|
|
1 |
# Imports
|
2 |
-
import
|
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
|
@@ -19,6 +14,7 @@ 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)
|
@@ -36,23 +32,35 @@ def read_root():
|
|
36 |
@app.post("/predict")
|
37 |
def predict(text:str):
|
38 |
"prediction endpoint, classifying tweets"
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Imports
|
2 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
3 |
from typing import Union
|
4 |
from src.utils import preprocess
|
5 |
from fastapi import FastAPI
|
|
|
14 |
#/docs, page to see auto-generated API documentation
|
15 |
|
16 |
#loading ML/DL components
|
17 |
+
os.environ['SENTENCE_TRANSFORMERS_HOME'] = './.cache'
|
18 |
tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
|
19 |
model_path = f"Junr-syl/tweet_sentiments_analysis"
|
20 |
config = AutoConfig.from_pretrained(model_path)
|
|
|
32 |
@app.post("/predict")
|
33 |
def predict(text:str):
|
34 |
"prediction endpoint, classifying tweets"
|
35 |
+
print(f"\n[Info] Starting prediction")
|
36 |
+
try:
|
37 |
+
text = preprocess(text)
|
38 |
|
39 |
+
# PyTorch-based models
|
40 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
41 |
+
output = model(**encoded_input)
|
42 |
+
scores = output[0][0].detach().numpy()
|
43 |
+
scores = softmax(scores)
|
44 |
|
45 |
+
#Process scores
|
46 |
+
ranking = np.argsort(scores)
|
47 |
+
ranking = ranking[::-1]
|
48 |
+
predicted_label = config.id2label[ranking[0]]
|
49 |
+
predicted_score = float(scores[ranking[0]])
|
50 |
|
51 |
+
|
52 |
+
response = {"text":text,
|
53 |
+
"predicted_label":predicted_label,
|
54 |
+
"confidence_score":predicted_score
|
55 |
+
}
|
56 |
+
|
57 |
+
print(f"\n[Info] Prediction done.")
|
58 |
+
print(f"\n[Info] Have a look at the API response")
|
59 |
+
print(response)
|
60 |
+
|
61 |
+
return response
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
return {
|
65 |
+
"error": str(e)
|
66 |
+
}
|