BK-AI commited on
Commit
258c872
1 Parent(s): 4829b64

update frontend to prepare for office plot

Browse files
Files changed (3) hide show
  1. app.py +80 -26
  2. classes_office.npy +3 -0
  3. requirements.txt +1 -0
app.py CHANGED
@@ -9,19 +9,32 @@ from transformers import (
9
  TextClassificationPipeline,
10
  pipeline,
11
  )
 
12
  from langdetect import detect
13
  from matplotlib import pyplot as plt
14
  import imageio
15
 
16
  # move constants into extra file
17
- ML_MODEL_SURE = 0.6
 
 
 
 
 
 
 
 
 
18
  UNKNOWN_LANG_TEXT = (
19
  "The language is not recognized, it must be either in German or in French."
20
  )
21
- PLACEHOLDER_TEXT = "Geben Sie bitte den Titel und den Sumbmitted Text des Vorstoss ein.\nVeuillez entrer le titre et le Submitted Text de la requête."
22
 
23
  UNSURE_DE_TEXT = "Das ML-Modell ist nicht sicher. Das Departement könnte sein : \n\n"
24
  UNSURE_FR_TEXT = "Le modèle ML n'est pas sûr. Le département pourrait être : \n\n"
 
 
 
25
  BARS_DEP_FR = (
26
  "DDPS",
27
  "DFI",
@@ -60,10 +73,10 @@ def load_model(modelFolder):
60
  return pipe
61
 
62
 
63
- def translate_to_de(inputText):
64
  """Translates french user input to German for the model to reach better classification."""
65
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-de")
66
- translatedText = translator(inputText[0:1000])
67
  text = translatedText[0]["translation_text"]
68
  return text
69
 
@@ -115,35 +128,76 @@ def show_chosen_category(barnames, rates, language):
115
 
116
 
117
  pipeDep = load_model("saved_model_dep")
118
- # pipeOffice = load_model("saved_model_office")
 
 
 
119
 
120
 
121
- # Function called by the UI
122
- def attribution(inputText):
123
  plt.clf()
124
- language = detect(inputText)
125
 
126
  # Translate the input to german if necessary
127
  if language == "fr":
128
- inputText = translate_to_de(inputText)
129
  elif language != "de":
130
  return UNKNOWN_LANG_TEXT, None
131
 
132
  # Make the prediction with the 1000 first characters
133
- prediction = pipeDep(inputText[0:1000], return_all_scores=True)
134
- rates = [row["score"] for row in prediction[0]]
135
-
136
- # Create barplot & output text
137
- im, barnames = create_bar_plot(rates, language)
138
- chosenCategoryText = show_chosen_category(barnames, rates, language)
139
-
140
- return chosenCategoryText, im
141
-
142
-
143
- # display the UI
144
- interface = gr.Interface(
145
- fn=attribution,
146
- inputs=[gr.components.Textbox(lines=20, placeholder=PLACEHOLDER_TEXT)],
147
- outputs=["text", "image"],
148
- )
149
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  TextClassificationPipeline,
10
  pipeline,
11
  )
12
+ from sklearn import preprocessing
13
  from langdetect import detect
14
  from matplotlib import pyplot as plt
15
  import imageio
16
 
17
  # move constants into extra file
18
+ DESCRIPTION = """Diese Anwendung klassifiziert Vorstöße in Departements und schlägt auch ein
19
+ mögliches Office vor. Bitte bewerten Sie für sich, ob Sie dem Office-Vorschlag
20
+ nachkommen wollen, oder Ihren Vorstoß in einem anderen Office sehen, und leiten Sie
21
+ nach eigenem Ermessen weiter. \n\n
22
+ Cette application classe les requêtes dans les départements et propose également un
23
+ office possible. Veuillez évaluer pour vous-même si vous souhaitez suivre la
24
+ proposition d'office ou si vous souhaitez voir votre démarche dans un autre office
25
+ et transmettez à votre discrétion."""
26
+ TITLE_DE = "Automatisierte Einteilung von Vorstößen in Departements & Offices"
27
+ TITLE_FR = "Où aller ? Classification des départements & bureaux"
28
  UNKNOWN_LANG_TEXT = (
29
  "The language is not recognized, it must be either in German or in French."
30
  )
31
+ PLACEHOLDER_TEXT = "Geben Sie bitte den Titel und den 'Submitted Text' des Vorstoss ein.\nVeuillez entrer le titre et le 'Submitted Text' de la requête."
32
 
33
  UNSURE_DE_TEXT = "Das ML-Modell ist nicht sicher. Das Departement könnte sein : \n\n"
34
  UNSURE_FR_TEXT = "Le modèle ML n'est pas sûr. Le département pourrait être : \n\n"
35
+
36
+ ML_MODEL_SURE = 0.6
37
+
38
  BARS_DEP_FR = (
39
  "DDPS",
40
  "DFI",
 
73
  return pipe
74
 
75
 
76
+ def translate_to_de(SubmittedText):
77
  """Translates french user input to German for the model to reach better classification."""
78
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-de")
79
+ translatedText = translator(SubmittedText[0:1000])
80
  text = translatedText[0]["translation_text"]
81
  return text
82
 
 
128
 
129
 
130
  pipeDep = load_model("saved_model_dep")
131
+ pipeOffice = load_model("saved_model_dep")
132
+
133
+ labelencoderOffice = preprocessing.LabelEncoder()
134
+ labelencoderOffice.classes_ = np.load("classes_office.npy")
135
 
136
 
137
+ def textclassification(SubmittedText):
 
138
  plt.clf()
139
+ language = detect(SubmittedText)
140
 
141
  # Translate the input to german if necessary
142
  if language == "fr":
143
+ SubmittedText = translate_to_de(SubmittedText)
144
  elif language != "de":
145
  return UNKNOWN_LANG_TEXT, None
146
 
147
  # Make the prediction with the 1000 first characters
148
+ images = []
149
+ chosenCategoryTexts = []
150
+ for pipe in (pipeDep, pipeOffice):
151
+ prediction = pipe(SubmittedText[0:1000], return_all_scores=True)
152
+ rates = [row["score"] for row in prediction[0]]
153
+
154
+ # Create barplot & output text
155
+ im, barnames = create_bar_plot(rates, language)
156
+ images.append(im)
157
+
158
+ chosenCategoryText = show_chosen_category(barnames, rates, language)
159
+ chosenCategoryTexts.append(chosenCategoryText)
160
+
161
+ # return chosenCategoryText & image for both predictions
162
+ return chosenCategoryTexts[0], images[0], chosenCategoryTexts[1], images[1]
163
+
164
+
165
+ # TODO set example picture upon loading
166
+ # TODO vielleicht ein paar Sachen zum Einstellen im Frontend?
167
+
168
+ # Launch UI
169
+ with gr.Blocks(
170
+ # Set theme matching BK CH
171
+ gr.themes.Monochrome(
172
+ primary_hue="red",
173
+ secondary_hue="red",
174
+ font=[gr.themes.GoogleFont("Inter"), "Arial", "sans-serif"],
175
+ )
176
+ ) as demo:
177
+ gr.Markdown(f"# {TITLE_DE}\n # {TITLE_FR}\n\n {DESCRIPTION}")
178
+
179
+ # Organize layout in three columns for input, prediction I and prediction II
180
+ with gr.Row():
181
+ with gr.Column(scale=2):
182
+ name = gr.Textbox(label="", lines=28, placeholder=PLACEHOLDER_TEXT)
183
+ predict_btn = gr.Button("Submit | Soumettre")
184
+ with gr.Column(scale=2):
185
+ output_text_dep = gr.Textbox(label="Departement prediction:")
186
+ output_image_dep = gr.Image(label="Departement")
187
+ with gr.Column(scale=2):
188
+ output_text_office = gr.Textbox(label="Office prediction:")
189
+ output_image_office = gr.Image(label="Office")
190
+
191
+ predict_btn.click(
192
+ fn=textclassification,
193
+ inputs=name,
194
+ outputs=[
195
+ output_text_dep,
196
+ output_image_dep,
197
+ output_text_office,
198
+ output_image_office,
199
+ ],
200
+ api_name="predict",
201
+ )
202
+
203
+ demo.launch()
classes_office.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:91aa3c28bb43aeb228af856650169f97f6326064b2dedb4cb438d5541918a94f
3
+ size 1480
requirements.txt CHANGED
@@ -5,6 +5,7 @@ matplotlib
5
  imageio
6
  torch
7
  sentencepiece
 
8
 
9
  gradio
10
  langdetect
 
5
  imageio
6
  torch
7
  sentencepiece
8
+ sklearn
9
 
10
  gradio
11
  langdetect