Initial Upload
Browse files- .gitignore +7 -0
- Dockerfile +14 -0
- main.py +50 -0
- requirements.txt +8 -0
- src/base/recommender.py +39 -0
- src/book_rec/data.json +0 -0
- src/book_rec/main.py +12 -0
- src/cat_and_dog/main.py +40 -0
- src/cat_and_dog/model_85.9.h5 +3 -0
- src/movie_rec/data.json +0 -0
- src/movie_rec/main.py +12 -0
- src/movie_reviews/main.py +70 -0
- src/movie_reviews/pipeline.pkl +3 -0
.gitignore
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/__pycache__
|
2 |
+
|
3 |
+
/src/base/__pycache__
|
4 |
+
/src/book_rec/__pycache__
|
5 |
+
/src/cat_and_dog/__pycache__
|
6 |
+
/src/movie_rec/__pycache__
|
7 |
+
/src/movie_reviews/__pycache__
|
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
|
3 |
+
# Importing Models and Schemas
|
4 |
+
from src.movie_reviews.main import movie_reviews, Schema as MovieReviewsSchema
|
5 |
+
from src.cat_and_dog.main import cat_and_dog, Schema as CatAndDogSchema
|
6 |
+
from src.book_rec.main import book_rec, Schema as BookRecSchema
|
7 |
+
from src.movie_rec.main import movie_rec, Schema as MovieRecSchema
|
8 |
+
|
9 |
+
# Initializing App
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# Allowing Cross Origins
|
13 |
+
from fastapi.middleware.cors import CORSMiddleware
|
14 |
+
origins = [
|
15 |
+
"http://localhost.tiangolo.com",
|
16 |
+
"https://localhost.tiangolo.com",
|
17 |
+
"http://localhost",
|
18 |
+
"http://localhost:8080",
|
19 |
+
]
|
20 |
+
app.add_middleware(
|
21 |
+
CORSMiddleware,
|
22 |
+
allow_origins=["*"],
|
23 |
+
allow_credentials=True,
|
24 |
+
allow_methods=["*"],
|
25 |
+
allow_headers=["*"],
|
26 |
+
)
|
27 |
+
|
28 |
+
print(" ........... App Started ........... ")
|
29 |
+
|
30 |
+
# Endpoints
|
31 |
+
|
32 |
+
@app.get("/")
|
33 |
+
def index():
|
34 |
+
return "Welcome to the API of PyModelsAI"
|
35 |
+
|
36 |
+
@app.post("/movie_reviews")
|
37 |
+
def endpoint_movie_reviews(req: MovieReviewsSchema):
|
38 |
+
return movie_reviews(req)
|
39 |
+
|
40 |
+
@app.post("/cat_and_dog")
|
41 |
+
def endpoint_cat_and_dog(req: CatAndDogSchema):
|
42 |
+
return cat_and_dog(req)
|
43 |
+
|
44 |
+
@app.post("/book_rec")
|
45 |
+
def endpoint_book_rec(req: BookRecSchema):
|
46 |
+
return book_rec(req)
|
47 |
+
|
48 |
+
@app.post("/movie_rec")
|
49 |
+
def endpoint_movie_rec(req: MovieRecSchema):
|
50 |
+
return movie_rec(req)
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
joblib
|
4 |
+
scikit-learn
|
5 |
+
numpy
|
6 |
+
tensorflow-cpu
|
7 |
+
keras
|
8 |
+
Pillow
|
src/base/recommender.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from pydantic import BaseModel
|
3 |
+
|
4 |
+
# SCHEMA
|
5 |
+
class Schema(BaseModel):
|
6 |
+
title: str
|
7 |
+
n: int = 5
|
8 |
+
|
9 |
+
# Request Handler
|
10 |
+
def recommender(req, data):
|
11 |
+
title = req.title
|
12 |
+
n = req.n
|
13 |
+
output = predict(title, n, data)
|
14 |
+
return output
|
15 |
+
|
16 |
+
def predict(title, n, data):
|
17 |
+
index = data['titles'].index(title)
|
18 |
+
recs = data['recs'][index][:n]
|
19 |
+
output = []
|
20 |
+
|
21 |
+
for rec in [[index, 0]] + recs:
|
22 |
+
i, score = rec
|
23 |
+
new_rec = {
|
24 |
+
"title": data['titles'][i],
|
25 |
+
"score": score,
|
26 |
+
"img": None,
|
27 |
+
"info": None
|
28 |
+
}
|
29 |
+
if (data['imgs']):
|
30 |
+
new_rec['img'] = data['imgs'][i]
|
31 |
+
if (data['infos']):
|
32 |
+
new_rec['info'] = data['infos'][i],
|
33 |
+
|
34 |
+
if type(new_rec['info']) == tuple:
|
35 |
+
new_rec['info'] = new_rec['info'][0]
|
36 |
+
|
37 |
+
output.append(new_rec)
|
38 |
+
|
39 |
+
return output
|
src/book_rec/data.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
src/book_rec/main.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from src.base.recommender import recommender
|
2 |
+
from src.base.recommender import Schema
|
3 |
+
|
4 |
+
import json
|
5 |
+
data_path = "./src/book_rec/data.json"
|
6 |
+
with open(data_path, 'rb') as f:
|
7 |
+
data = json.load(f)
|
8 |
+
|
9 |
+
# Request Handler
|
10 |
+
def book_rec(req):
|
11 |
+
# Sending to Base
|
12 |
+
return recommender(req, data)
|
src/cat_and_dog/main.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
import io, base64, requests
|
4 |
+
from pydantic import BaseModel
|
5 |
+
|
6 |
+
# SCHEMA
|
7 |
+
class Schema(BaseModel):
|
8 |
+
resized_img_base64:str = None,
|
9 |
+
img_url:str = None
|
10 |
+
|
11 |
+
# Request Handler
|
12 |
+
def cat_and_dog(req):
|
13 |
+
resized_img_base64 = req.resized_img_base64
|
14 |
+
img_url = req.img_url
|
15 |
+
output = predict(resized_img_base64, img_url)
|
16 |
+
return output
|
17 |
+
|
18 |
+
model_path = "./src/cat_and_dog/model_85.9.h5"
|
19 |
+
"""
|
20 |
+
This Model has an accuracy of 85.9%
|
21 |
+
"""
|
22 |
+
|
23 |
+
model = tf.keras.models.load_model(model_path)
|
24 |
+
|
25 |
+
def predict(img_data, img_url):
|
26 |
+
if img_url == None:
|
27 |
+
content = img_data.replace(" ", "+")
|
28 |
+
converted = bytes(content, "utf-8")
|
29 |
+
img = base64.decodebytes(converted)
|
30 |
+
else:
|
31 |
+
img = requests.get(img_url).content
|
32 |
+
|
33 |
+
img = io.BytesIO(img)
|
34 |
+
img = tf.keras.preprocessing.image.load_img(img, target_size=model.input_shape[1:])
|
35 |
+
img = np.array(img)
|
36 |
+
img = img.reshape(1, *img.shape)
|
37 |
+
img = img / 255.
|
38 |
+
pred = model.predict(img)[0, 0]
|
39 |
+
pred = float(pred)
|
40 |
+
return [round(1-pred, 3), round(pred, 3)]
|
src/cat_and_dog/model_85.9.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:84e57acb8a4be38f5aed70b36a2c4b6f18aa87c755df651121b6bca1097556e0
|
3 |
+
size 440216
|
src/movie_rec/data.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
src/movie_rec/main.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from src.base.recommender import recommender
|
2 |
+
from src.base.recommender import Schema
|
3 |
+
|
4 |
+
import json
|
5 |
+
data_path = "./src/movie_rec/data.json"
|
6 |
+
with open(data_path, 'rb') as f:
|
7 |
+
data = json.load(f)
|
8 |
+
|
9 |
+
# Request Handler
|
10 |
+
def movie_rec(req):
|
11 |
+
# Sending to Base
|
12 |
+
return recommender(req, data)
|
src/movie_reviews/main.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import string
|
2 |
+
import re
|
3 |
+
import joblib
|
4 |
+
from pydantic import BaseModel
|
5 |
+
|
6 |
+
# SCHEMA
|
7 |
+
class Schema(BaseModel):
|
8 |
+
text: str
|
9 |
+
|
10 |
+
# Request Handler
|
11 |
+
def movie_reviews(req):
|
12 |
+
text = req.text
|
13 |
+
output = predict(text)
|
14 |
+
return output
|
15 |
+
|
16 |
+
# PREPROCESSING
|
17 |
+
punc = string.punctuation
|
18 |
+
abbv = {
|
19 |
+
"AFAIK":"as far as I know",
|
20 |
+
"IMO": "in my opinion",
|
21 |
+
"IMHO": "in my humble opinion",
|
22 |
+
"LGTM": "look good to me",
|
23 |
+
"AKA": "also know as",
|
24 |
+
"ASAP": "as sone as possible",
|
25 |
+
"BTW": "by the way",
|
26 |
+
"FAQ": "frequently asked questions",
|
27 |
+
"DIY": "do it yourself",
|
28 |
+
"DM": "direct message",
|
29 |
+
"FYI": "for your information",
|
30 |
+
"IC": "i see",
|
31 |
+
"IOW": "in other words",
|
32 |
+
"IIRC": "If I Remember Correctly",
|
33 |
+
"icymi":"In case you missed it",
|
34 |
+
"CUZ": "because",
|
35 |
+
"COS": "because",
|
36 |
+
"nv": "nevermind",
|
37 |
+
"PLZ": "please",
|
38 |
+
}
|
39 |
+
html_pattern = re.compile('<.*?>')
|
40 |
+
urls_pattern = re.compile(r'https?://\S+|www\.\S+')
|
41 |
+
emoji_pattern = re.compile("["
|
42 |
+
u"\U0001F600-\U0001F64F" # emoticons
|
43 |
+
u"\U0001F300-\U0001F5FF" # symbols & pictographs
|
44 |
+
u"\U0001F680-\U0001F6FF" # transport & map symbols
|
45 |
+
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
|
46 |
+
"]+", flags=re.UNICODE)
|
47 |
+
|
48 |
+
# PIPELINE
|
49 |
+
pipeline = joblib.load("./src/movie_reviews/pipeline.pkl")
|
50 |
+
|
51 |
+
def predict(text):
|
52 |
+
cleaned = preprocess(text)
|
53 |
+
pred = pipeline.predict([cleaned])[0]
|
54 |
+
output = [0, 0]
|
55 |
+
output[pred] = 0.8
|
56 |
+
output[1-pred] = 0.2
|
57 |
+
return output
|
58 |
+
|
59 |
+
def preprocess(text):
|
60 |
+
text = text.lower() # Lowercase
|
61 |
+
text = html_pattern.sub(r'', text) # HTML Tags
|
62 |
+
text = urls_pattern.sub(r'', text) # urls
|
63 |
+
text = text.translate(str.maketrans("", "", punc)) # punctuations
|
64 |
+
text = emoji_pattern.sub(r'', text) # Emojis
|
65 |
+
new_text = []
|
66 |
+
for word in text.split(" "):
|
67 |
+
word = abbv.get(word.upper(), word) # abbreviations
|
68 |
+
new_text.append(word)
|
69 |
+
text = " ".join(new_text)
|
70 |
+
return text
|
src/movie_reviews/pipeline.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8cace6b6bc66a701a90b8c4e19f6ae96e87b57c4be47ad4accf772380e3536a3
|
3 |
+
size 2182254
|