lyangas commited on
Commit
860fd6c
·
1 Parent(s): e9753c6

change interface of gradio

Browse files
Files changed (2) hide show
  1. app.py +30 -10
  2. default_input.json +1 -0
app.py CHANGED
@@ -1,4 +1,5 @@
1
  print('INFO: import modules')
 
2
  import gradio as gr
3
  import pickle
4
  from required_classes import *
@@ -13,31 +14,50 @@ try:
13
  except Exception as e:
14
  print(f"ERROR: loading models failed with: {str(e)}")
15
 
16
- def classify_code(text):
17
  embed = model._texts2vecs([text])
18
  probs = model.classifier_code.predict_proba(embed)
19
- best_n = np.flip(np.argsort(probs, axis=1,)[0])
20
  preds = {model.classifier_code.classes_[i]: probs[0][i] for i in best_n}
21
  return preds
22
 
23
- def classify_group(text):
24
  embed = model._texts2vecs([text])
25
  probs = model.classifier_group.predict_proba(embed)
26
- best_n = np.flip(np.argsort(probs, axis=1,)[0])
27
  preds = {model.classifier_group.classes_[i]: probs[0][i] for i in best_n}
28
  return preds
29
 
30
- def classify(text):
31
- return classify_code(text), classify_group(text)
 
 
 
 
 
 
32
 
33
  print('INFO: starting gradio interface')
34
- default_input_text = """Microscopic Evaluation: The specimen is excised into the reticular dermis and demonstrates skin with prominent papillomatosis and hyperkeratosis. The keratinocytes show reactive changes with nuclear enlargement and increased amounts of cytoplasm but no dysplasia is identified. Lymphocytic inflammation is distributed through the base of the lesion. Some pigmentation is present. The lesion approximates the edges of the specimen but appears to be predominantly excised. (GMW/jc) Gross Description: Shave removal and destruction erythematous tender papule with hyperkeratotic scale SCC vs BCC D.. Right medial temple. Final Diagnosis: Pigmented seborrheic keratosis inflamed."""
 
 
 
 
 
 
 
 
 
 
 
 
35
  iface = gr.Interface(
36
  enable_queue=True,
37
  title="ICD10-codes classification",
38
  description="",
39
- fn=classify,
40
- inputs=[gr.Textbox(label="Input text", value=default_input_text)],
41
- outputs=[gr.Label(num_top_classes=4, label="Result class"), gr.Label(num_top_classes=4, label="Result group")],
42
  )
 
43
  iface.launch()
 
1
  print('INFO: import modules')
2
+ import json
3
  import gradio as gr
4
  import pickle
5
  from required_classes import *
 
14
  except Exception as e:
15
  print(f"ERROR: loading models failed with: {str(e)}")
16
 
17
+ def classify_code(text, top_n):
18
  embed = model._texts2vecs([text])
19
  probs = model.classifier_code.predict_proba(embed)
20
+ best_n = np.flip(np.argsort(probs, axis=1,)[0,-top_n:])
21
  preds = {model.classifier_code.classes_[i]: probs[0][i] for i in best_n}
22
  return preds
23
 
24
+ def classify_group(text, top_n):
25
  embed = model._texts2vecs([text])
26
  probs = model.classifier_group.predict_proba(embed)
27
+ best_n = np.flip(np.argsort(probs, axis=1,)[0,-top_n:])
28
  preds = {model.classifier_group.classes_[i]: probs[0][i] for i in best_n}
29
  return preds
30
 
31
+ def classify(text, top_n):
32
+ try:
33
+ top_n = int(top_n)
34
+ res = classify_code(text, top_n), classify_group(text, top_n)
35
+ return res
36
+ except Exception as e:
37
+ error_msg = f"Error: {str(e)}"
38
+ return error_msg, error_msg
39
 
40
  print('INFO: starting gradio interface')
41
+ box_class = gr.Label(label="Result class")
42
+ box_group = gr.Label(label="Result group")
43
+ def predict(text, top_n):
44
+ try:
45
+ top_n = int(top_n)
46
+ predicted_codes = classify_code(text, top_n)
47
+ predicted_groups = classify_group(text, top_n)
48
+ return {box_class: predicted_codes, box_group: predicted_groups}
49
+ except Exception as e:
50
+ error_msg = f"Error: {str(e)}"
51
+ return {box_class: error_msg, box_group: error_msg}
52
+
53
+ default_input_text = json.load(open('default_input.json'))['input_text']
54
  iface = gr.Interface(
55
  enable_queue=True,
56
  title="ICD10-codes classification",
57
  description="",
58
+ fn=predict,
59
+ inputs=[gr.Textbox(label="Input text", value=default_input_text), gr.Number(label="TOP-N candidates", value=3)],
60
+ outputs=[box_class, box_group],
61
  )
62
+
63
  iface.launch()
default_input.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"input_text": "Microscopic Evaluation: The specimen is excised into the reticular dermis and demonstrates skin with prominent papillomatosis and hyperkeratosis. The keratinocytes show reactive changes with nuclear enlargement and increased amounts of cytoplasm but no dysplasia is identified. Lymphocytic inflammation is distributed through the base of the lesion. Some pigmentation is present. The lesion approximates the edges of the specimen but appears to be predominantly excised. (GMW/jc) Gross Description: Shave removal and destruction erythematous tender papule with hyperkeratotic scale SCC vs BCC D.. Right medial temple. Final Diagnosis: Pigmented seborrheic keratosis inflamed."}