GaetanoParente commited on
Commit
2ff9250
·
verified ·
1 Parent(s): ceb5fe4

Upload 78 files

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +4 -0
  2. README.md +221 -13
  3. app.py +130 -0
  4. data/gallery/No_Pneumonia/IM-0005-0001.jpeg +0 -0
  5. data/gallery/No_Pneumonia/IM-0029-0001.jpeg +0 -0
  6. data/gallery/No_Pneumonia/IM-0030-0001.jpeg +0 -0
  7. data/gallery/No_Pneumonia/IM-0033-0001-0002.jpeg +0 -0
  8. data/gallery/No_Pneumonia/IM-0035-0001.jpeg +0 -0
  9. data/gallery/No_Pneumonia/IM-0036-0001.jpeg +0 -0
  10. data/gallery/No_Pneumonia/IM-0077-0001.jpeg +0 -0
  11. data/gallery/No_Pneumonia/IM-0079-0001.jpeg +0 -0
  12. data/gallery/No_Pneumonia/IM-0085-0001.jpeg +0 -0
  13. data/gallery/No_Pneumonia/IM-0086-0001.jpeg +0 -0
  14. data/gallery/No_Pneumonia/IM-0117-0001.jpeg +0 -0
  15. data/gallery/No_Pneumonia/IM-0122-0001.jpeg +0 -0
  16. data/gallery/No_Pneumonia/IM-0125-0001.jpeg +0 -0
  17. data/gallery/No_Pneumonia/IM-0127-0001.jpeg +0 -0
  18. data/gallery/No_Pneumonia/IM-0128-0001.jpeg +0 -0
  19. data/gallery/No_Pneumonia/IM-0131-0001.jpeg +0 -0
  20. data/gallery/No_Tubercolosi/Normal-10.png +0 -0
  21. data/gallery/No_Tubercolosi/Normal-13.png +0 -0
  22. data/gallery/No_Tubercolosi/Normal-16.png +0 -0
  23. data/gallery/No_Tubercolosi/Normal-2.png +0 -0
  24. data/gallery/No_Tubercolosi/Normal-21.png +0 -0
  25. data/gallery/No_Tubercolosi/Normal-30.png +0 -0
  26. data/gallery/No_Tubercolosi/Normal-32.png +0 -0
  27. data/gallery/No_Tubercolosi/Normal-45.png +0 -0
  28. data/gallery/No_Tubercolosi/Normal-46.png +0 -0
  29. data/gallery/No_Tubercolosi/Normal-49.png +0 -0
  30. data/gallery/No_Tubercolosi/Normal-59.png +0 -0
  31. data/gallery/No_Tubercolosi/Normal-63.png +0 -0
  32. data/gallery/No_Tubercolosi/Normal-65.png +0 -0
  33. data/gallery/No_Tubercolosi/Normal-67.png +0 -0
  34. data/gallery/Pneumonia/person10_bacteria_43.jpeg +0 -0
  35. data/gallery/Pneumonia/person13_bacteria_49.jpeg +0 -0
  36. data/gallery/Pneumonia/person13_bacteria_50.jpeg +0 -0
  37. data/gallery/Pneumonia/person16_bacteria_55.jpeg +0 -0
  38. data/gallery/Pneumonia/person19_virus_50.jpeg +0 -0
  39. data/gallery/Pneumonia/person1_bacteria_1.jpeg +0 -0
  40. data/gallery/Pneumonia/person1_virus_9.jpeg +0 -0
  41. data/gallery/Pneumonia/person20_bacteria_70.jpeg +0 -0
  42. data/gallery/Pneumonia/person3_virus_17.jpeg +0 -0
  43. data/gallery/Pneumonia/person4_bacteria_14.jpeg +0 -0
  44. data/gallery/Pneumonia/person5_bacteria_19.jpeg +0 -0
  45. data/gallery/Pneumonia/person7_bacteria_25.jpeg +0 -0
  46. data/gallery/Pneumonia/person8_bacteria_37.jpeg +0 -0
  47. data/gallery/Pneumonia/person9_bacteria_38.jpeg +0 -0
  48. data/gallery/Tubercolosi/Tuberculosis-101.png +0 -0
  49. data/gallery/Tubercolosi/Tuberculosis-122.png +0 -0
  50. data/gallery/Tubercolosi/Tuberculosis-131.png +0 -0
.gitattributes CHANGED
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ data/model/medical-image-classification.keras filter=lfs diff=lfs merge=lfs -text
37
+ data/model/model.keras filter=lfs diff=lfs merge=lfs -text
38
+ data/model/multi-classification.keras filter=lfs diff=lfs merge=lfs -text
39
+ data/tokenizer/multi-classification-tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,13 +1,221 @@
1
- ---
2
- title: Ngt Ai Platfomr
3
- emoji: 🏃
4
- colorFrom: blue
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 4.21.0
8
- app_file: app.py
9
- pinned: false
10
- license: apache-2.0
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # NGT AI Platform
3
+
4
+ La piattaforma si propone di esporre i seguenti moduli:
5
+ 1. binary classification di un testo fornito in input
6
+ 2. image classification di una immagine fornita in input (Classi : Basket, Bowling, Calcio, Golf, Hockey, Rugby, Volley, Tennis)
7
+ 3. multilabel classification di un testo fornito in input (Classi: alt.atheism, comp.graphics, comp.os.ms-windows.misc, comp.sys.ibm.pc.hardware, comp.sys.mac.hardware, comp.windows.x, misc.forsale, rec.autos, rec.motorcycles, rec.sport.baseball, rec.sport.hockey, sci.crypt, sci.electronics, sci.med, sci.space, soc.religion.christian, talk.politics.guns, talk.politics.mideast, talk.politics.misc, talk.religion.misc)
8
+
9
+
10
+ ## Required
11
+
12
+ Prima di procedere è necessario installare anaconda utilizzando la seguente [guida](https://docs.anaconda.com/free/anaconda/install/linux/)
13
+
14
+ La lemmatizzazione del testo viene eseguita con la libreria [spacy](https://spacy.io/usage).
15
+ Procedere con i seguenti passaggi
16
+
17
+ ```bash
18
+ pip install -U pip setuptools wheel
19
+ pip install -U spacy
20
+ python -m spacy download it_core_news_lg
21
+ ```
22
+
23
+ Fondamentale installare anche la libreria tensorflow
24
+
25
+ ```bash
26
+ pip install tensorflow
27
+ ```
28
+
29
+ ## Run Locally
30
+
31
+ Clona il progetto
32
+
33
+ ```bash
34
+ git clone git@github.com:gaeparente/ngt-ai-platform.git
35
+ ```
36
+
37
+ Installa il micro-framework Flask
38
+
39
+ ```bash
40
+ python -m pip install flask
41
+ ```
42
+
43
+ Installa libreria CORS di Flask
44
+
45
+ ```bash
46
+ pip install flask_cors
47
+ ```
48
+
49
+ Posizionati nella directory del file app.py
50
+
51
+ ```bash
52
+ cd ngt-ai-platform/
53
+ ```
54
+
55
+ Avvia il server
56
+
57
+ ```bash
58
+ flask run
59
+ ```
60
+
61
+ I moduli saranno quindi raggiungibili:
62
+ 1. binary classification all'indirizzo http://127.0.0.1:5000/binary-classification
63
+ 2. image classification all'indirizzo http://127.0.0.1:5000/image-classification
64
+ 3. multilabel classification all'indirizzo http://127.0.0.1:5000/multi-classification
65
+
66
+
67
+ ## Usage/Examples Binary classification
68
+
69
+ Effettuare una chiamata POST all'indirizzo indicato in precedenza. Il body dovrà essere in formato form-data con le seguenti property:
70
+ 1. text (required) -> contenente la sentence per cui si richiede la classificazione
71
+ 2. model (optional) -> contenente il file del modello (.keras o .h5)
72
+ 3. token (optional) -> contenente il file del tokenizer (.json)
73
+
74
+ La risposta sarà quindi
75
+
76
+ ```json
77
+ {
78
+ "lemma": "che posto ragazzo ! uno cucina ricercare in piccolo cortile di altro tempo . bello , buone , bravissimo . prenotare con largo anticipo .",
79
+ "percent": "99.95895028114319",
80
+ "sentiment": "POSITIVE"
81
+ }
82
+ ```
83
+
84
+ ## Usage/Examples Image Classification
85
+
86
+ Effettuare una chiamata POST all'indirizzo indicato in precedenza. Il body dovrà essere in formato form-data con le seguenti property:
87
+ 1. image (required) -> contenente il file per cui si richiede la classificazione
88
+ 2. model (optional) -> contenente il file del modello (.keras o .h5)
89
+
90
+ La risposta sarà quindi
91
+
92
+ ```json
93
+ [
94
+ {
95
+ "classe": "Basket",
96
+ "percent": "0.02414761"
97
+ },
98
+ {
99
+ "classe": "Bowling",
100
+ "percent": "0.12304398"
101
+ },
102
+ {
103
+ "classe": "Calcio",
104
+ "percent": "0.00155318"
105
+ },
106
+ {
107
+ "classe": "Golf",
108
+ "percent": "0.00484183"
109
+ },
110
+ {
111
+ "classe": "Hockey",
112
+ "percent": "0.05853807"
113
+ },
114
+ {
115
+ "classe": "Rugby",
116
+ "percent": "0.26000361"
117
+ },
118
+ {
119
+ "classe": "Volley",
120
+ "percent": "99.52762127"
121
+ },
122
+ {
123
+ "classe": "Tennis",
124
+ "percent": "0.00024511"
125
+ }
126
+ ]
127
+ ```
128
+
129
+ ## Usage/Examples Multilabel classification
130
+
131
+ Effettuare una chiamata POST all'indirizzo indicato in precedenza. Il body dovrà essere in formato form-data con le seguenti property:
132
+ 1. text (required) -> contenente la sentence per cui si richiede la classificazione
133
+ 2. model (optional) -> contenente il file del modello (.keras o .h5)
134
+ 3. token (optional) -> contenente il file del tokenizer (.json)
135
+
136
+ La risposta sarà quindi
137
+
138
+ ```json
139
+ [
140
+ {
141
+ "classe": "alt.atheism",
142
+ "percent": "20.58875114"
143
+ },
144
+ {
145
+ "classe": "comp.graphics",
146
+ "percent": "5.57006039"
147
+ },
148
+ {
149
+ "classe": "comp.os.ms-windows.misc",
150
+ "percent": "1.00294100"
151
+ },
152
+ {
153
+ "classe": "comp.sys.ibm.pc.hardware",
154
+ "percent": "0.17852880"
155
+ },
156
+ {
157
+ "classe": "comp.sys.mac.hardware",
158
+ "percent": "0.24781623"
159
+ },
160
+ {
161
+ "classe": "comp.windows.x",
162
+ "percent": "3.20503265"
163
+ },
164
+ {
165
+ "classe": "misc.forsale",
166
+ "percent": "0.16137564"
167
+ },
168
+ {
169
+ "classe": "rec.autos",
170
+ "percent": "0.23865439"
171
+ },
172
+ {
173
+ "classe": "rec.motorcycles",
174
+ "percent": "0.35177895"
175
+ },
176
+ {
177
+ "classe": "rec.sport.baseball",
178
+ "percent": "1.18482364"
179
+ },
180
+ {
181
+ "classe": "rec.sport.hockey",
182
+ "percent": "0.21046386"
183
+ },
184
+ {
185
+ "classe": "sci.crypt",
186
+ "percent": "4.29985709"
187
+ },
188
+ {
189
+ "classe": "sci.electronics",
190
+ "percent": "2.09880602"
191
+ },
192
+ {
193
+ "classe": "sci.med",
194
+ "percent": "19.70048994"
195
+ },
196
+ {
197
+ "classe": "sci.space",
198
+ "percent": "5.71478717"
199
+ },
200
+ {
201
+ "classe": "soc.religion.christian",
202
+ "percent": "11.07885465"
203
+ },
204
+ {
205
+ "classe": "talk.politics.guns",
206
+ "percent": "1.57866161"
207
+ },
208
+ {
209
+ "classe": "talk.politics.mideast",
210
+ "percent": "1.79922581"
211
+ },
212
+ {
213
+ "classe": "talk.politics.misc",
214
+ "percent": "3.07453331"
215
+ },
216
+ {
217
+ "classe": "talk.religion.misc",
218
+ "percent": "17.71455258"
219
+ }
220
+ ]
221
+ ```
app.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <<<<<<< HEAD
2
+ from flask import Flask, request
3
+ from flask_cors import CORS
4
+ from modules.binary_classification import binary_classification as binary
5
+ from modules.image_classification import image_classification as image
6
+ from modules.multilabel_classification import multi_classification as multi
7
+
8
+ app = Flask(__name__)
9
+ CORS(app)
10
+
11
+ @app.post("/binary-classification")
12
+ def binary_classification():
13
+ return binary(request)
14
+
15
+ @app.post("/image-classification")
16
+ def image_classification():
17
+ return image(request)
18
+
19
+ @app.post("/multi-classification")
20
+ def multilabel_classification():
21
+ return multi(request)
22
+
23
+
24
+ =======
25
+ from flask import Flask, request, jsonify
26
+
27
+ app = Flask(__name__)
28
+
29
+ import keras.models as models
30
+ import spacy
31
+ import string
32
+ import re
33
+ import json
34
+ from nltk.corpus import stopwords
35
+
36
+ nlp = spacy.load("it_core_news_sm")
37
+ BASE_PATH = 'data/'
38
+ MODEL = BASE_PATH + 'model/'
39
+ VOCAB = BASE_PATH + 'vocab.txt'
40
+
41
+ def load_doc(filename):
42
+ # open the file as read only
43
+ file = open(filename, 'r')
44
+ # read all text
45
+ text = file.read()
46
+ # close the file
47
+ file.close()
48
+ return text
49
+
50
+ def load_vocab():
51
+ vocab=load_doc(VOCAB)
52
+ vocab=vocab.split()
53
+ vocab = set(vocab)
54
+ return vocab
55
+
56
+ from keras.preprocessing.text import Tokenizer
57
+ from keras.preprocessing.text import tokenizer_from_json
58
+
59
+ def load_tokenizer():
60
+ with open(MODEL + 'tokenizer.json') as f:
61
+ data = json.load(f)
62
+ tokenizer = Tokenizer()
63
+ tokenizer = tokenizer_from_json(data)
64
+ return tokenizer
65
+
66
+ def lemma_text(text):
67
+ doc = nlp(text)
68
+ lemmatized_tokens = [token.lemma_ for token in doc]
69
+ lemmatized_text = ' '.join(lemmatized_tokens)
70
+ return lemmatized_text
71
+
72
+ def clean_doc(text):
73
+ doc = lemma_text(text)
74
+ # split into tokens by white space
75
+ tokens = doc.split()
76
+ # prepare regex for char filtering
77
+ re_punc = re.compile('[%s]' % re.escape(string.punctuation)) # remove punctuation from each word
78
+ tokens = [re_punc.sub('', w) for w in tokens]
79
+ # remove remaining tokens that are not alphabetic
80
+ tokens = [word for word in tokens if word.isalpha()]
81
+ # filter out stop words
82
+ stop_words = set(stopwords.words('italian'))
83
+ tokens = [w for w in tokens if not w in stop_words]
84
+ # filter out short tokens
85
+ tokens = [word for word in tokens if len(word) > 1]
86
+ # rimuovo le parole show e less
87
+ tokens = [word for word in tokens if word not in ('show', 'less')]
88
+ return tokens
89
+
90
+ def predict_sentiment(review, vocab, tokenizer, model):
91
+ # clean
92
+ tokens = clean_doc(review)
93
+ # filter by vocab
94
+ tokens = [w for w in tokens if w in vocab]
95
+ # convert to line
96
+ line = ' '.join(tokens)
97
+ # encode
98
+ encoded = tokenizer.texts_to_matrix([line], mode='tfidf')
99
+ # predict sentiment
100
+ yhat = model.predict(encoded, verbose=0)
101
+ # retrieve predicted percentage and label
102
+ percent_pos = yhat[0,0]
103
+ if round(percent_pos) == 0:
104
+ return (1-percent_pos), 'NEGATIVE'
105
+ return percent_pos, 'POSITIVE'
106
+
107
+ def predict(text) :
108
+ model = models.load_model(MODEL + 'model.keras', compile=False)
109
+ vocab = load_vocab()
110
+ tokenizer = load_tokenizer()
111
+ doc = lemma_text(text)
112
+ percent, sentiment = predict_sentiment(doc, vocab, tokenizer, model)
113
+ print('Review: [%s]\nSentiment: %s (%.3f%%)' % (doc, sentiment, percent*100))
114
+ return doc, sentiment, percent
115
+
116
+ @app.post("/text-classification")
117
+ def text_classification():
118
+ if request.is_json:
119
+ text = request.get_json()
120
+ sentence = text["text"]
121
+ doc, sentiment, percent = predict(sentence)
122
+ response = {
123
+ "lemma" : doc,
124
+ "sentiment" : sentiment,
125
+ "percent" : str(percent * 100)
126
+ }
127
+ return jsonify(response)
128
+ return {"error": "Request must be JSON"}, 415
129
+
130
+ >>>>>>> 255b26b (first commit)
data/gallery/No_Pneumonia/IM-0005-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0029-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0030-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0033-0001-0002.jpeg ADDED
data/gallery/No_Pneumonia/IM-0035-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0036-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0077-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0079-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0085-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0086-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0117-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0122-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0125-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0127-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0128-0001.jpeg ADDED
data/gallery/No_Pneumonia/IM-0131-0001.jpeg ADDED
data/gallery/No_Tubercolosi/Normal-10.png ADDED
data/gallery/No_Tubercolosi/Normal-13.png ADDED
data/gallery/No_Tubercolosi/Normal-16.png ADDED
data/gallery/No_Tubercolosi/Normal-2.png ADDED
data/gallery/No_Tubercolosi/Normal-21.png ADDED
data/gallery/No_Tubercolosi/Normal-30.png ADDED
data/gallery/No_Tubercolosi/Normal-32.png ADDED
data/gallery/No_Tubercolosi/Normal-45.png ADDED
data/gallery/No_Tubercolosi/Normal-46.png ADDED
data/gallery/No_Tubercolosi/Normal-49.png ADDED
data/gallery/No_Tubercolosi/Normal-59.png ADDED
data/gallery/No_Tubercolosi/Normal-63.png ADDED
data/gallery/No_Tubercolosi/Normal-65.png ADDED
data/gallery/No_Tubercolosi/Normal-67.png ADDED
data/gallery/Pneumonia/person10_bacteria_43.jpeg ADDED
data/gallery/Pneumonia/person13_bacteria_49.jpeg ADDED
data/gallery/Pneumonia/person13_bacteria_50.jpeg ADDED
data/gallery/Pneumonia/person16_bacteria_55.jpeg ADDED
data/gallery/Pneumonia/person19_virus_50.jpeg ADDED
data/gallery/Pneumonia/person1_bacteria_1.jpeg ADDED
data/gallery/Pneumonia/person1_virus_9.jpeg ADDED
data/gallery/Pneumonia/person20_bacteria_70.jpeg ADDED
data/gallery/Pneumonia/person3_virus_17.jpeg ADDED
data/gallery/Pneumonia/person4_bacteria_14.jpeg ADDED
data/gallery/Pneumonia/person5_bacteria_19.jpeg ADDED
data/gallery/Pneumonia/person7_bacteria_25.jpeg ADDED
data/gallery/Pneumonia/person8_bacteria_37.jpeg ADDED
data/gallery/Pneumonia/person9_bacteria_38.jpeg ADDED
data/gallery/Tubercolosi/Tuberculosis-101.png ADDED
data/gallery/Tubercolosi/Tuberculosis-122.png ADDED
data/gallery/Tubercolosi/Tuberculosis-131.png ADDED