Gaetano Parente commited on
Commit
c469f55
·
1 Parent(s): 87fbdff
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
- from modules.binary_classification import binary_classification_small as binary
3
- from modules.image_classification import image_classification_small as image
4
- from modules.multilabel_classification import multi_classification_small as multi
5
  import cv2
6
 
7
  image_output = gr.Label()
@@ -14,10 +14,10 @@ def binary_classification(text):
14
 
15
  def multi_classification(text):
16
  if(text != ''):
17
- try:
18
  return multi(text)
19
- except:
20
- raise gr.Error('Il testo deve essere in inglese!')
21
  else:
22
  raise gr.Error('Il testo è obbligatorio!')
23
 
 
1
  import gradio as gr
2
+ from modules.binary_classification import binary_classification as binary
3
+ from modules.image_classification import image_classification as image
4
+ from modules.multilabel_classification import multi_classification as multi
5
  import cv2
6
 
7
  image_output = gr.Label()
 
14
 
15
  def multi_classification(text):
16
  if(text != ''):
17
+ # try:
18
  return multi(text)
19
+ # except:
20
+ # raise gr.Error('Il testo deve essere in inglese!')
21
  else:
22
  raise gr.Error('Il testo è obbligatorio!')
23
 
modules/binary_classification.py CHANGED
@@ -78,69 +78,7 @@ def predict(model_path, weights_path, tokenizer_path, text, debug) :
78
  print('Review: [%s]\nSentiment: %s (%.3f%%)' % (doc, sentiment, percent*100))
79
  return doc, sentiment, percent
80
 
81
- def binary_classification(request):
82
- init()
83
- model_custom = False
84
- tokenizer_custom = False
85
- weights_custom = False
86
- try:
87
- m = request.files['model']
88
- if(utils.allowed_model(m.filename)):
89
- model = MODEL + 'upload_' + m.filename
90
- m.save(model)
91
- model_custom = True
92
- print("importato modello " + m.filename)
93
- else :
94
- raise Exception
95
- except :
96
- model = MODEL + 'binary-classification.keras'
97
- print("uso modello default")
98
- try:
99
- w = request.files['weights']
100
- if(utils.allowed_model(w.filename)):
101
- weights = WEIGHTS + 'upload_' + w.filename
102
- w.save(weights)
103
- weights_custom = True
104
- print("importati pesi " + w.filename)
105
- else :
106
- raise Exception
107
- except :
108
- weights = ''
109
- print("uso pesi default")
110
- try:
111
- t = request.files['token']
112
- if(utils.allowed_tokenizer(t.filename)):
113
- tokenizer = TOKEN + 'upload_' + t.filename
114
- t.save(tokenizer)
115
- tokenizer_custom = True
116
- print("importato tokenizer " + t.filename)
117
- else :
118
- raise Exception
119
- except :
120
- tokenizer = TOKEN + 'binary-classification-tokenizer.json'
121
- print("uso tokenizer default")
122
- try:
123
- text = request.form['text']
124
- if(text == "") : raise Exception
125
- except :
126
- return {"error": "Sentence is required"}, 415
127
- try:
128
- doc, sentiment, percent = predict(model, weights, tokenizer, text, True)
129
- response = {
130
- "lemma" : doc,
131
- "sentiment" : sentiment,
132
- "percent" : str(percent * 100)
133
- }
134
- return jsonify(response)
135
- finally:
136
- if(model_custom) :
137
- os.remove(model)
138
- if(tokenizer_custom) :
139
- os.remove(tokenizer)
140
- if(weights_custom) :
141
- os.remove(weights)
142
-
143
- def binary_classification_small(text):
144
  init()
145
  model = MODEL + 'binary-classification.h5'
146
  weights = ''
 
78
  print('Review: [%s]\nSentiment: %s (%.3f%%)' % (doc, sentiment, percent*100))
79
  return doc, sentiment, percent
80
 
81
+ def binary_classification(text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  init()
83
  model = MODEL + 'binary-classification.h5'
84
  weights = ''
modules/image_classification.py CHANGED
@@ -23,40 +23,7 @@ def image_predict(image_path, model_path):
23
  pred_labels = np.argmax(predictions, axis= 1)
24
  return predictions, pred_labels
25
 
26
- def image_classification(request):
27
- model_custom = False
28
- try:
29
- m = request.files['model']
30
- if(allowed_model(m.filename)):
31
- model = MODEL + 'upload_' + m.filename
32
- m.save(model)
33
- model_custom = True
34
- print("importato modello " + m.filename)
35
- else :
36
- raise Exception
37
- except :
38
- model = MODEL + 'medical-image-classification.keras'
39
- print("uso modello default ")
40
- try:
41
- image = request.files['image']
42
- image.save(image.filename)
43
- except :
44
- return {"error": "Image is required"}, 415
45
- try:
46
- labels, max_label = image_predict(image.filename, model)
47
- response = []
48
- for i, label in enumerate(labels[0]):
49
- response.append({
50
- "classe" : class_names[i],
51
- "percent" : "%.8f" % float(label*100)
52
- })
53
- return jsonify(response)
54
- finally:
55
- if(model_custom) :
56
- os.remove(model)
57
- os.remove(image.filename)
58
-
59
- def image_classification_small(numpy_image):
60
  model = MODEL + 'medical-image-classification.keras'
61
  myuuid = uuid.uuid4()
62
  filename = 'test_' + str(myuuid) + '.png'
 
23
  pred_labels = np.argmax(predictions, axis= 1)
24
  return predictions, pred_labels
25
 
26
+ def image_classification(numpy_image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  model = MODEL + 'medical-image-classification.keras'
28
  myuuid = uuid.uuid4()
29
  filename = 'test_' + str(myuuid) + '.png'
modules/multilabel_classification.py CHANGED
@@ -35,55 +35,7 @@ def predict(model_path, tokenizer_path, sentence):
35
  predicted_label = class_names[np.argmax(prediction[0])]
36
  return prediction, predicted_label
37
 
38
- def multi_classification(request):
39
- model_custom = False
40
- tokenizer_custom = False
41
- try:
42
- m = request.files['model']
43
- if(utils.allowed_model(m.filename)):
44
- model = MODEL + 'upload_' + m.filename
45
- m.save(model)
46
- model_custom = True
47
- print("importato modello " + m.filename)
48
- else :
49
- raise Exception
50
- except :
51
- model = MODEL + 'multi-classification.keras'
52
- print("uso modello default ")
53
- try:
54
- t = request.files['token']
55
- if(utils.allowed_tokenizer(t.filename)):
56
- tokenizer = TOKEN + 'upload_' + t.filename
57
- t.save(tokenizer)
58
- tokenizer_custom = True
59
- print("importato tokenizer " + t.filename)
60
- else :
61
- raise Exception
62
- except :
63
- tokenizer = TOKEN + 'multi-classification-tokenizer.json'
64
- print("uso tokenizer default")
65
- try:
66
- text = request.form['text']
67
- if(text == "") : raise Exception
68
- except :
69
- return {"error": "Sentence is required"}, 415
70
- try:
71
- labels, max_label = predict(model, tokenizer, text)
72
- response = []
73
- for i, label in enumerate(labels[0]):
74
- response.append({
75
- "classe" : class_names[i],
76
- "percent" : "%.8f" % float(label*100)
77
- })
78
- return jsonify(response)
79
- finally:
80
- if(model_custom) :
81
- os.remove(model)
82
- if(tokenizer_custom) :
83
- os.remove(tokenizer)
84
- os.remove('temp.txt')
85
-
86
- def multi_classification_small(text):
87
  model = MODEL + 'multi-classification.h5'
88
  tokenizer = TOKEN + 'multi-classification-tokenizer.json'
89
  try:
 
35
  predicted_label = class_names[np.argmax(prediction[0])]
36
  return prediction, predicted_label
37
 
38
+ def multi_classification(text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  model = MODEL + 'multi-classification.h5'
40
  tokenizer = TOKEN + 'multi-classification-tokenizer.json'
41
  try: