Spaces:
Running
on
T4
Running
on
T4
koenverhagen
commited on
Commit
•
60a68a6
1
Parent(s):
481dffe
init
Browse files- .DS_Store +0 -0
- .gitignore +2 -0
- .gitlab-ci.yml +59 -0
- .idea/.gitignore +8 -0
- .idea/HF_CardTagger.iml +14 -0
- .idea/inspectionProfiles/Project_Default.xml +16 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- Dockerfile +21 -0
- __init__.py +0 -0
- cardtagger.py +120 -0
- model/cardtagger/life-event-2.pkl +3 -0
- model/cardtagger/mapping-life-event.csv +10 -0
- model/cardtagger/mapping.csv +221 -0
- model/cardtagger/tags.pkl +3 -0
- requirements.txt +11 -0
- server.py +86 -0
- test.jpg +0 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
img.png
|
2 |
+
example-request.py
|
.gitlab-ci.yml
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
image: ubuntu
|
2 |
+
|
3 |
+
stages:
|
4 |
+
- deploy
|
5 |
+
|
6 |
+
variables:
|
7 |
+
GIT_DEPTH: 10
|
8 |
+
SERVICE_DIR: card-tagger-kc
|
9 |
+
REMOTE_ADDRESS: 74.82.28.76
|
10 |
+
REMOTE_USER: gitlab-runner
|
11 |
+
CONTAINER_NAME: ft-api
|
12 |
+
|
13 |
+
before_script:
|
14 |
+
##
|
15 |
+
## Install ssh-agent if not already installed, it is required by Docker.
|
16 |
+
## (change apt-get to yum if you use an RPM-based image)
|
17 |
+
##
|
18 |
+
- 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
|
19 |
+
|
20 |
+
##
|
21 |
+
## Run ssh-agent (inside the build environment)
|
22 |
+
##
|
23 |
+
- eval $(ssh-agent -s)
|
24 |
+
|
25 |
+
##
|
26 |
+
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
|
27 |
+
## We're using tr to fix line endings which makes ed25519 keys work
|
28 |
+
## without extra base64 encoding.
|
29 |
+
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
|
30 |
+
##
|
31 |
+
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
|
32 |
+
|
33 |
+
##
|
34 |
+
## Create the SSH directory and give it the right permissions
|
35 |
+
##
|
36 |
+
- mkdir -p ~/.ssh
|
37 |
+
- chmod 700 ~/.ssh
|
38 |
+
|
39 |
+
##
|
40 |
+
## Use ssh-keyscan to scan the keys of your private server. Replace gitlab.com
|
41 |
+
## with your own domain name. You can copy and repeat that command if you have
|
42 |
+
## more than one server to connect to.
|
43 |
+
##
|
44 |
+
- ssh-keyscan $REMOTE_ADDRESS >> ~/.ssh/known_hosts
|
45 |
+
- chmod 644 ~/.ssh/known_hosts
|
46 |
+
|
47 |
+
##
|
48 |
+
## install rsync
|
49 |
+
##
|
50 |
+
- apt-get update -y && apt-get install rsync -y
|
51 |
+
|
52 |
+
deploy:
|
53 |
+
stage: deploy
|
54 |
+
script:
|
55 |
+
- rsync -v --stats -a --exclude .git --exclude __pycache__ --delete . $REMOTE_USER@$REMOTE_ADDRESS:service/$SERVICE_DIR/
|
56 |
+
- ssh -t $REMOTE_USER@$REMOTE_ADDRESS "cd service/ && docker-compose build $CONTAINER_NAME"
|
57 |
+
- ssh -t $REMOTE_USER@$REMOTE_ADDRESS "cd service/ && docker-compose up --no-deps -d $CONTAINER_NAME"
|
58 |
+
only:
|
59 |
+
- master
|
.idea/.gitignore
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Default ignored files
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
4 |
+
# Editor-based HTTP Client requests
|
5 |
+
/httpRequests/
|
6 |
+
# Datasource local storage ignored files
|
7 |
+
/dataSources/
|
8 |
+
/dataSources.local.xml
|
.idea/HF_CardTagger.iml
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<module type="PYTHON_MODULE" version="4">
|
3 |
+
<component name="NewModuleRootManager">
|
4 |
+
<content url="file://$MODULE_DIR$">
|
5 |
+
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
6 |
+
</content>
|
7 |
+
<orderEntry type="inheritedJdk" />
|
8 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
9 |
+
</component>
|
10 |
+
<component name="PyDocumentationSettings">
|
11 |
+
<option name="format" value="PLAIN" />
|
12 |
+
<option name="myDocStringFormat" value="Plain" />
|
13 |
+
</component>
|
14 |
+
</module>
|
.idea/inspectionProfiles/Project_Default.xml
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<profile version="1.0">
|
3 |
+
<option name="myName" value="Project Default" />
|
4 |
+
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
5 |
+
<inspection_tool class="PyCompatibilityInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
6 |
+
<option name="ourVersions">
|
7 |
+
<value>
|
8 |
+
<list size="2">
|
9 |
+
<item index="0" class="java.lang.String" itemvalue="2.7" />
|
10 |
+
<item index="1" class="java.lang.String" itemvalue="3.11" />
|
11 |
+
</list>
|
12 |
+
</value>
|
13 |
+
</option>
|
14 |
+
</inspection_tool>
|
15 |
+
</profile>
|
16 |
+
</component>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<settings>
|
3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
+
<version value="1.0" />
|
5 |
+
</settings>
|
6 |
+
</component>
|
.idea/misc.xml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (HF_CardTagger)" project-jdk-type="Python SDK" />
|
4 |
+
</project>
|
.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectModuleManager">
|
4 |
+
<modules>
|
5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/HF_CardTagger.iml" filepath="$PROJECT_DIR$/.idea/HF_CardTagger.iml" />
|
6 |
+
</modules>
|
7 |
+
</component>
|
8 |
+
</project>
|
.idea/vcs.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="VcsDirectoryMappings">
|
4 |
+
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
5 |
+
</component>
|
6 |
+
</project>
|
Dockerfile
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#FROM nvidia/cuda:11.0.3-base-ubuntu18.04
|
2 |
+
#FROM python:3.7.2-stretch
|
3 |
+
FROM fastdotai/fastai:latest
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
RUN apt-get update -y && \
|
8 |
+
apt-get install -y python3-pip python-dev
|
9 |
+
|
10 |
+
#RUN mkdir /app
|
11 |
+
|
12 |
+
WORKDIR /code
|
13 |
+
|
14 |
+
COPY ./requirements.txt /code/requirements.txt
|
15 |
+
|
16 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
17 |
+
|
18 |
+
|
19 |
+
COPY . .
|
20 |
+
|
21 |
+
CMD ["uvicorn", "server:app", "--reload", "--host", "0.0.0.0", "--port", "7860"]
|
__init__.py
ADDED
File without changes
|
cardtagger.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision.all import load_learner, PILImage
|
2 |
+
import torch
|
3 |
+
import csv
|
4 |
+
import hashlib
|
5 |
+
import json
|
6 |
+
from pathlib import Path
|
7 |
+
import os
|
8 |
+
|
9 |
+
|
10 |
+
def get_preds(obj, learn, model_name='tags', thresh=15):
|
11 |
+
|
12 |
+
labels = []
|
13 |
+
|
14 |
+
'''
|
15 |
+
get list of classes from Learner object
|
16 |
+
'''
|
17 |
+
for item in learn.dls.vocab:
|
18 |
+
labels.append(item)
|
19 |
+
|
20 |
+
'''
|
21 |
+
open mapping from csv into dictionary and get only the onces with mapping
|
22 |
+
'''
|
23 |
+
if model_name == 'life-event':
|
24 |
+
input_file = "./model/cardtagger/mapping-life-event.csv"
|
25 |
+
|
26 |
+
else:
|
27 |
+
input_file = "./model/cardtagger/mapping.csv"
|
28 |
+
|
29 |
+
data = csv.DictReader(open(input_file))
|
30 |
+
|
31 |
+
dic = dict()
|
32 |
+
|
33 |
+
for row in data:
|
34 |
+
if row['tag'] != row['alternatives']:
|
35 |
+
dic[row['tag']] = row['alternatives'].split(',')
|
36 |
+
|
37 |
+
'''
|
38 |
+
combine the classnames with the result and get those with > threshold back
|
39 |
+
add the synonym mapping list to the dictionary
|
40 |
+
'''
|
41 |
+
predictions = []
|
42 |
+
x = 0
|
43 |
+
for item in obj:
|
44 |
+
acc = round(item.item()*100, 1)
|
45 |
+
|
46 |
+
if acc > thresh:
|
47 |
+
|
48 |
+
synonyms = []
|
49 |
+
|
50 |
+
for i in dic:
|
51 |
+
if labels[x] == i:
|
52 |
+
synonyms = dic[i]
|
53 |
+
|
54 |
+
predictions.append({"label": labels[x], "probability" : acc, "synonyms" : synonyms })
|
55 |
+
#predictions[labels[x]] = acc
|
56 |
+
x += 1
|
57 |
+
|
58 |
+
predictions = {"predictions": predictions}
|
59 |
+
|
60 |
+
|
61 |
+
return predictions
|
62 |
+
|
63 |
+
|
64 |
+
def cardtagger(image):
|
65 |
+
|
66 |
+
img = PILImage(PILImage.create(image).resize((128,128)))
|
67 |
+
|
68 |
+
'''
|
69 |
+
get classification of images that already where send to api or predict on new
|
70 |
+
'''
|
71 |
+
base = Path("./tmp/")
|
72 |
+
|
73 |
+
md5hash = hashlib.md5(img.tobytes()).hexdigest()
|
74 |
+
|
75 |
+
file = os.path.join(base, md5hash)
|
76 |
+
|
77 |
+
if os.path.exists(file):
|
78 |
+
result = json.load(open(base / (md5hash)))
|
79 |
+
|
80 |
+
else:
|
81 |
+
|
82 |
+
'''
|
83 |
+
get classification of tags
|
84 |
+
'''
|
85 |
+
|
86 |
+
tag_model = load_learner('./model/cardtagger/tags.pkl')
|
87 |
+
|
88 |
+
tag_prediction, _, tag_probs = tag_model.predict(img)
|
89 |
+
|
90 |
+
result_tags = get_preds(tag_probs, tag_model, 'tags')
|
91 |
+
|
92 |
+
'''
|
93 |
+
get classification of life event
|
94 |
+
'''
|
95 |
+
|
96 |
+
life_event_model = load_learner('./model/cardtagger/life-event-2.pkl')
|
97 |
+
|
98 |
+
life_event_prediction, _, life_event_probs = life_event_model.predict(img)
|
99 |
+
|
100 |
+
result_life = get_preds(life_event_probs, life_event_model, 'life-event', 30)
|
101 |
+
|
102 |
+
|
103 |
+
'''
|
104 |
+
comebine tag predictions ...
|
105 |
+
'''
|
106 |
+
result = {"predictions": result_tags['predictions']+result_life['predictions']}
|
107 |
+
|
108 |
+
'''
|
109 |
+
write the json to a temp file and return the results
|
110 |
+
'''
|
111 |
+
|
112 |
+
|
113 |
+
out_file = open(file, "w+")
|
114 |
+
|
115 |
+
json.dump(result, out_file)
|
116 |
+
|
117 |
+
|
118 |
+
return result
|
119 |
+
|
120 |
+
#cardtagger('test.jpg')
|
model/cardtagger/life-event-2.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:27cf390c5bbb7031ee2fd3bb8829995ff4b159288f150400a0f3f49d9f5990cb
|
3 |
+
size 242471959
|
model/cardtagger/mapping-life-event.csv
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tag,alternatives
|
2 |
+
geboortekaartje,"geboortekaartje,geboorte,geboortekaartjes"
|
3 |
+
jubileumkaart,"huwelijksjubileum,jubileum,jubileum-huwelijk,jubileumkaarten,felicitatie-huwelijksjubileum"
|
4 |
+
kerstkaart,kerstkaarten
|
5 |
+
save-the-date-kaart,"save-the-date-kaart,save-the-date,save-the-date-kaarten,save-the-date-kaart-trouwen"
|
6 |
+
trouwkaart,"trouwkaart,soort-trouwkaarten,trouwkaarten,huwelijksuitnodiging,trouwkaarten-set,wij-gaan-trouwen,gaan-trouwen,trouwen"
|
7 |
+
uitnodigingskaart,"uitnodigingen,uitnodiging,uitnodigingfeestpagina"
|
8 |
+
verhuiskaart,"verhuiskaarten,nieuw-home,verhuisbericht,verhuizing,verhuizen,verhuizing-housewarming"
|
9 |
+
verjaardagskaart,verjaardag
|
10 |
+
wenskaart,"verjaardag-wenskaart,geboorte-wenskaart,huwelijk-wenskaart,pensioen-wenskaart,bedank-wenskaart"
|
model/cardtagger/mapping.csv
ADDED
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tag,alternatives
|
2 |
+
12.5,cat-jubileum-kaarten-12-5-jaar
|
3 |
+
2022,2022
|
4 |
+
21-diner,21diner
|
5 |
+
25,"cat-uitnodiging-kaart-25-jaar,25-jaar-getrouwd,vijfentwintig,25-jaar"
|
6 |
+
30,"verjaardag-30-jaar,cat-uitnodiging-kaart-30-jaar,dertig,30-jaar"
|
7 |
+
40,"cat-jubileum-kaarten-40-jaar,verjaardag-40-jaar,veertig,40-jaar"
|
8 |
+
40-jaar-getrouwd,40-jaar-getrouwd
|
9 |
+
50,"cat-uitnodiging-kaart-50-jaar,verjaardag-50,vijftig,50-jaar"
|
10 |
+
50-jaar-getrouwd,50-jaar-getrouwd
|
11 |
+
60,"verjaardag-60,cat-uitnodiging-kaart-60-jaar,zestig,60-jaar"
|
12 |
+
65,"verjaardag-65,cat-uitnodiging-kaart-65-jaar,vijfenzestig,65-jaar"
|
13 |
+
70,"cat-uitnodiging-kaart-70-jaar,70-jaar,zeventig"
|
14 |
+
80,cat-uitnodiging-kaart-80-jaar
|
15 |
+
aap,aap
|
16 |
+
abraham,abraham
|
17 |
+
adventure,adventure
|
18 |
+
aquarel,aquarel-achtergrond
|
19 |
+
auto,auto
|
20 |
+
avontuurlijk,avontuurlijk
|
21 |
+
baby,baby
|
22 |
+
babykaartje,babykaartje
|
23 |
+
ballon,"ballonnen,thema-ballonnen,luchtballon"
|
24 |
+
basic,basic
|
25 |
+
bedankkaart,bedankkaart
|
26 |
+
bedankkaart-rouw,bedankkaart-rouw
|
27 |
+
beer,wasbeer
|
28 |
+
beige,beige
|
29 |
+
blaadjes,blaadjes-takjes
|
30 |
+
blanco,"zelf-aanpassen,zelf-maken,zelfmaken,zelf-ontwerpen-blanco-kaart,zelf-maken-kaarten,zelf-ontwerpen,zelf-ontwerpen-blanco-poster"
|
31 |
+
blauw,"grijs-blauw,petrol-blauw"
|
32 |
+
bloem,bloem
|
33 |
+
bloemen,"bloemetjes,bloem-en-bloemen,bloemenpatroon,bloemen-illsutratie,droogbloemen,rozen,bloemen-achtergrond,bloemen-foto,bloemen-lijntekening"
|
34 |
+
boerderij,boerderij
|
35 |
+
bohemian,"bohemian-classic,bohemian-flowers"
|
36 |
+
boom,boom
|
37 |
+
borden,borden
|
38 |
+
bosdieren,bosdieren
|
39 |
+
botanisch,"botanical,botanisch-blad,botanische-print,basic-botanisch,casual-botanic,classic-botanic"
|
40 |
+
broer,grote-broer
|
41 |
+
broertje,broertje
|
42 |
+
brons,brons
|
43 |
+
bruiloft,bruiloft
|
44 |
+
bruin,"taupe,bruin-taupe"
|
45 |
+
champagne,champagne
|
46 |
+
chique,"chic,chique-en-clean,modern-chique"
|
47 |
+
christelijk,christelijk
|
48 |
+
clean,clean
|
49 |
+
communie,"communie-bedankkaart,communiekaarten,communiekaartjes,communie-bedank,communiekaartjes-bedankt,menukaart-communie"
|
50 |
+
confetti,confetti-feest
|
51 |
+
derde-kindje,derde-kindje
|
52 |
+
dieren,"dier,dieren-thema,gezin-dieren,dieren-achtergrond,dierenprint,boerderijdieren,kerst-dieren,wilde-dieren,wilde-dieren-collectie"
|
53 |
+
dochter,dochter
|
54 |
+
donkerblauw,donkerblauw
|
55 |
+
echt-hout,"op-echt-hout,kaart-van-echt-hout,houten-kaarten"
|
56 |
+
eenvoud,eenvoud
|
57 |
+
effen-achtergrond,effen-achtergrond
|
58 |
+
elk-seizoen,elk-seizoen
|
59 |
+
exclusief,exclusief
|
60 |
+
exclusieve-trouwkaarten,exclusieve-trouwkaarten
|
61 |
+
feestdagen,feestdagen
|
62 |
+
feestelijk,feestelijk
|
63 |
+
feestje,feestje
|
64 |
+
fiets,fiets
|
65 |
+
flamingo,flamingo
|
66 |
+
floral,"floral-and-blue,rustic-floral,floral-season"
|
67 |
+
foliedruk,"folieprint,folie-druk,geboortekaart-folie,trouwkaart-folie,dubbelzijdig-folie,enkelzijdig-folie,kerstkaart-folie,uitnodiging-folie,dubbelzijdig-foliedruk,enkelzijdig-foliedruk,folie-geboorte,foliedruk-geboorte,metfolie,folie"
|
68 |
+
geel,geel
|
69 |
+
geometrisch,"geometrische-vlakken,geometrische-lijnen,geometrisch-patroon"
|
70 |
+
getekend,gezin-getekend
|
71 |
+
giraf,giraffe
|
72 |
+
glitter,glitter-glamour
|
73 |
+
goedkoop,goedkoop
|
74 |
+
goud,goud-thema
|
75 |
+
goudfolie,goudfolie
|
76 |
+
grappig,grappig
|
77 |
+
grijs,grijs
|
78 |
+
groen,"leger-groen,grijs-groen,petrol-groen,olijf-groen"
|
79 |
+
handlettering,handlettering
|
80 |
+
hart,hart
|
81 |
+
hartje,echt-houten-hartje
|
82 |
+
hartjes,"hartjes-achtergrond,hartjeslabel"
|
83 |
+
herfst,herfstkaart
|
84 |
+
herfst-winter,herfst-winter
|
85 |
+
hertje,hertje
|
86 |
+
hip,"hippe,hip-trendy,hippe-trouwkaarten"
|
87 |
+
hout,"hout-steigerhout,patroon-hout,thema-hout,houten,houten-kaartje,houtpatroon,houten-achtergrond"
|
88 |
+
houten-hartje,houten-hartje
|
89 |
+
huwelijksuitnodiging,huwelijksuitnodiging
|
90 |
+
icoontjes,icoontjes
|
91 |
+
illustratief,illustraties
|
92 |
+
inprintkaart,inprintkaart
|
93 |
+
instagram,instagram
|
94 |
+
jaartal,jaartal
|
95 |
+
jongen,stoere-jongen
|
96 |
+
jungle,"trendy-jungle,jungle-collectie"
|
97 |
+
jungledieren,jungle-dieren
|
98 |
+
karton,karton-look
|
99 |
+
kerst,"kerstkaarten-2015,kerstkaarten-2016,zakelijke-kerstkaarten,kerstkaart-zakelijk,kerst-verhuiskaart,kerst-zonder-foto,kerstster,kersttak,foto-kerst,kerst-ster,corona-kerstkaart"
|
100 |
+
kerst-save-the-date,save-the-date-kerstkaart
|
101 |
+
kerstboom,kerstboom
|
102 |
+
kinderfeestje,kinderfeest
|
103 |
+
kinderwagen,kinderwagen
|
104 |
+
klassiek,classic
|
105 |
+
klassieke-trouwkaarten,klassieke-trouwkaarten
|
106 |
+
kleurrijk,kleur
|
107 |
+
konijn,konijn
|
108 |
+
koper,"koperkleur,clean-koper"
|
109 |
+
koperfolie,koperfolie
|
110 |
+
kraft,"kraft-karton,kraft-kraftlook,karton-kraft-achtergrond,kraftpapier-karton,kraftlook,echt-kraft,kraftpapier,echt-kraft-papier,gedrukt-op-echt-kraft,kraft-algemeen"
|
111 |
+
krans,kerstkrans
|
112 |
+
krijtbord,krijtbord-achtergrond
|
113 |
+
label,"labels,met-labeltje,echt-label,labelkaart,labeltje"
|
114 |
+
langwerpig,langwerpig
|
115 |
+
leeuw,leeuw
|
116 |
+
lente,"lentekaart,lentefeest,lentefeest-uitnodiging"
|
117 |
+
lente-zomer,lente-zomer
|
118 |
+
lief,lief
|
119 |
+
lijntekening,lijntekening
|
120 |
+
logo,"met-eigen-logo,logo-initialen"
|
121 |
+
luxe,luxe
|
122 |
+
maan,houten-maantje
|
123 |
+
man,man
|
124 |
+
man-vrouw,man-en-vrouw
|
125 |
+
meisje,lief-meisje
|
126 |
+
menukaart,vormsel-menukaarten
|
127 |
+
met-foto,"foto,metfoto,1-foto,3-foto,foto-geboorte,foto-algemeen,fotokaart,2-foto,foto-trouwen,kerst-met-foto,foto-jubileum,4-foto,kerstkaart-fotolijst,trouwkaart-met-foto,gezin-foto,foto-voorkant"
|
128 |
+
minimalistisch,minimalistisch
|
129 |
+
mint,mint
|
130 |
+
mintgroen,mint-groen
|
131 |
+
modern,modern
|
132 |
+
natuurlijk,"natuur,nature"
|
133 |
+
nieuw-jaar,"nieuwjaarskaarten,nieuwjaarsborrel,nieuwjaarskaart"
|
134 |
+
okergeel,oker-geel
|
135 |
+
olifant,olifant
|
136 |
+
ooievaar,ooievaar
|
137 |
+
oranje,oranje
|
138 |
+
origineel,origineel
|
139 |
+
oudroze,oud-roze
|
140 |
+
paars,paars
|
141 |
+
panter,panter
|
142 |
+
pastel,"softpastel,pure-pastel"
|
143 |
+
peach,peach
|
144 |
+
perzik,perzik
|
145 |
+
populair,populair
|
146 |
+
poster,poster
|
147 |
+
regenboog,regenboog
|
148 |
+
retro,retro-patroon
|
149 |
+
roestbruin,roestbruin
|
150 |
+
romantisch,"romantic,romantische-trouwkaarten"
|
151 |
+
rood,rood
|
152 |
+
rose,rose
|
153 |
+
rosegoudfolie,"rosefolie,rose-goudfolie,rose-goud,rosegoud"
|
154 |
+
roze,zalm-roze
|
155 |
+
rustig,rustig
|
156 |
+
safari,safaridieren
|
157 |
+
samen-100,samen-100
|
158 |
+
sarah,sarah
|
159 |
+
seizoenskaartjes,seizoenen
|
160 |
+
sierllijk,sierllijk
|
161 |
+
silhouet,"silhouette,gezin-silhouet,silhouetje,silhouet-meisje"
|
162 |
+
simpel,simpel
|
163 |
+
slinger,slingers
|
164 |
+
spetters,spetters
|
165 |
+
sprookjes,sprookje
|
166 |
+
stempel,stempel
|
167 |
+
ster,ster
|
168 |
+
sterren,sterren
|
169 |
+
sterrenhemel,sterrenhemel
|
170 |
+
sterretje,sterretje
|
171 |
+
sterretjes,sterretjes
|
172 |
+
stijlvol,stijlvol
|
173 |
+
stippen,"stipjes,stippen-patroon"
|
174 |
+
stoer,stoer
|
175 |
+
strak,strak
|
176 |
+
strand,"strandpaal,zee-strand,strandstijl"
|
177 |
+
strepen,strepen
|
178 |
+
strikje,echte-strikje
|
179 |
+
symbool,symbool
|
180 |
+
takjes,"takje,tak"
|
181 |
+
themafeest,themafeest
|
182 |
+
tijdlijn,tijdlijn
|
183 |
+
tijger,tijger
|
184 |
+
trend,trend-2020
|
185 |
+
trendy,trendy
|
186 |
+
tuinfeest,tuinfeestje
|
187 |
+
tweede-kindje,"tweedekind,tweede-kind"
|
188 |
+
tweeling,"jongenstweeling,meisjestweeling"
|
189 |
+
typografie,"typografisch,stijlen-typografie"
|
190 |
+
uil,uiltje
|
191 |
+
uitnodiging-kraamfeest,uitnodiging-kraamfeest
|
192 |
+
unisex,uniseks
|
193 |
+
velvet,velvet
|
194 |
+
verjaardag-volwassen,verjaardag-volwassen
|
195 |
+
vintage,"vintages,vintage-achtergrond"
|
196 |
+
vlakken-en-prints,vlakken-en-prints
|
197 |
+
vlinder,vlindertje
|
198 |
+
voertuig,voertuig
|
199 |
+
voetjes,voetjes
|
200 |
+
vogel,vogeltje
|
201 |
+
vos,vos
|
202 |
+
vrolijk,vrolijk
|
203 |
+
vrouw,vrouw
|
204 |
+
walvis,walvis
|
205 |
+
watercolour,"aquarel-watercolor,aquarel-verf,watercolor,metwatercolour"
|
206 |
+
waterdieren,zeedieren
|
207 |
+
wereldbol,wereldbol
|
208 |
+
wij-zijn-getrouwd,wij-zijn-getrouwd
|
209 |
+
winter,winterkaart
|
210 |
+
wit,witte
|
211 |
+
wolkje,"wolkjes,wolken"
|
212 |
+
zakelijke-kaart,zakelijke-kaart
|
213 |
+
zakelijke-kerstkaart,zakelijke-kerstkaart
|
214 |
+
zilver,zilver
|
215 |
+
zilverfolie,zilverfolie
|
216 |
+
zomer,zomerkaart
|
217 |
+
zoon,zoon
|
218 |
+
zusje,"grote-zus,zus"
|
219 |
+
zwart,zwart
|
220 |
+
zwart-wit,zwart-en-wit
|
221 |
+
zwartfolie,zwartfolie
|
model/cardtagger/tags.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:780e2a76ef841f5962a3b6bfab0d31935ed5f869f24e771c3c1de08b698ac0c8
|
3 |
+
size 243033119
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy==1.24.3
|
2 |
+
fastapi==0.68.1
|
3 |
+
uvicorn==0.15.0
|
4 |
+
pydantic==1.8.2
|
5 |
+
requests==2.25.1
|
6 |
+
aiofiles==0.7.0
|
7 |
+
python-multipart==0.0.5
|
8 |
+
fastai==2.7.9
|
9 |
+
Pillow==9.2.0
|
10 |
+
torch==1.12.0
|
11 |
+
torchvision==0.13.0
|
server.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Response, Request, Header, Form, UploadFile, Body, BackgroundTasks
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from fastapi.responses import FileResponse
|
4 |
+
from starlette.middleware.cors import CORSMiddleware
|
5 |
+
|
6 |
+
from cardtagger import cardtagger
|
7 |
+
|
8 |
+
import io
|
9 |
+
from PIL import Image
|
10 |
+
import os, time
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
app.add_middleware(
|
14 |
+
CORSMiddleware,
|
15 |
+
allow_origins=["*"], # Allows all origins
|
16 |
+
allow_credentials=True,
|
17 |
+
allow_methods=["*"], # Allows all methods
|
18 |
+
allow_headers=["*"], # Allows all headers
|
19 |
+
)
|
20 |
+
|
21 |
+
# API_TOKEN = os.environ["API_TOKEN"]
|
22 |
+
API_TOKEN = '34dsadfF$$%#$TGREGEFGE%Q*)(*&%'
|
23 |
+
|
24 |
+
|
25 |
+
# ---------------------------------------------------------------------------
|
26 |
+
# - Token auth method -
|
27 |
+
# ---------------------------------------------------------------------------
|
28 |
+
def check_auth(authorization_header):
|
29 |
+
if authorization_header == API_TOKEN :
|
30 |
+
return True
|
31 |
+
else:
|
32 |
+
return False
|
33 |
+
|
34 |
+
# ---------------------------------------------------------------------------
|
35 |
+
# - Delete cache files after 30 days -
|
36 |
+
# ---------------------------------------------------------------------------
|
37 |
+
def delete_old_files():
|
38 |
+
path = "./tmp"
|
39 |
+
now = time.time()
|
40 |
+
|
41 |
+
for filename in os.listdir(path):
|
42 |
+
filestamp = os.stat(os.path.join(path, filename)).st_mtime
|
43 |
+
filecompare = now - 14 * 86400
|
44 |
+
if filestamp < filecompare:
|
45 |
+
print(filename)
|
46 |
+
os.remove(os.path.join(path, filename))
|
47 |
+
|
48 |
+
@app.on_event("startup")
|
49 |
+
def startup_event() :
|
50 |
+
'''
|
51 |
+
Create out dir
|
52 |
+
'''
|
53 |
+
if not os.path.exists("/tmp"):
|
54 |
+
os.makedirs("/tmp")
|
55 |
+
|
56 |
+
@app.get("/")
|
57 |
+
async def root():
|
58 |
+
return {"message": "Hello image"}
|
59 |
+
|
60 |
+
@app.post("/cardtagger")
|
61 |
+
async def resolution(request : Request, background_tasks: BackgroundTasks, response : Response, data: UploadFile = Form(...)):
|
62 |
+
|
63 |
+
authorization_token = request.headers.get("Authorization", None)
|
64 |
+
|
65 |
+
check_token = check_auth(authorization_token)
|
66 |
+
|
67 |
+
if check_token:
|
68 |
+
|
69 |
+
image = io.BytesIO(await data.read())
|
70 |
+
image.seek(0)
|
71 |
+
|
72 |
+
result = cardtagger(image.read())
|
73 |
+
|
74 |
+
background_tasks.add_task(delete_old_files)
|
75 |
+
|
76 |
+
return result
|
77 |
+
|
78 |
+
|
79 |
+
else :
|
80 |
+
response.status_code = 401
|
81 |
+
return response
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
#uvicorn server:app --host 0.0.0.0 --port 5050 --workers 4
|
test.jpg
ADDED