Az-r-ow commited on
Commit
e80c2cb
·
1 Parent(s): 3c21054

added text input box

Browse files
Files changed (1) hide show
  1. app/app.py +82 -38
app/app.py CHANGED
@@ -31,31 +31,50 @@ def handle_audio(audio, model, progress=gr.Progress()):
31
  return render_tabs([promptAudio], model, progress)
32
 
33
 
34
- def handle_file(file, model, progress=gr.Progress()):
35
- print("file upload")
36
  progress(0, desc=PROGRESS.ANALYZING_FILE.value)
37
  time.sleep(1)
38
- if file is not None:
39
- with open(file.name, "r") as f:
40
- progress(0.33, desc=PROGRESS.READING_FILE.value)
41
- file_content = f.read()
42
- rows = file_content.split("\n")
43
- sentences = [row for row in rows if row]
44
- return render_tabs(sentences, model, progress)
45
 
46
 
47
  tabs_components = []
48
 
49
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
50
  with gr.Column():
51
  with gr.Row():
52
  audio = gr.Audio(label="Fichier audio", interactive=True)
53
- file = gr.File(
54
- label="Fichier texte",
55
- file_types=["text"],
56
- file_count="single",
57
- interactive=True,
58
- )
 
 
 
 
 
 
 
 
 
 
59
 
60
  model = gr.Dropdown(
61
  label="Modèle NER",
@@ -64,14 +83,35 @@ with gr.Blocks() as demo:
64
  interactive=True,
65
  )
66
 
67
- @gr.render(
68
- inputs=[audio, file, model], triggers=[audio.change, file.upload, model.change]
 
 
 
 
 
 
 
 
 
 
 
69
  )
70
- def handle_changes(audio, file, model):
71
- if audio:
72
- return handle_audio(audio, model)
73
- elif file:
74
- return handle_file(file, model)
 
 
 
 
 
 
 
 
 
 
75
 
76
 
77
  def handleCityChange(city):
@@ -244,25 +284,29 @@ def getDepartureAndArrivalFromText(text: str, model: str):
244
 
245
  if 1 in entities:
246
  dep_idx = entities.index(1)
247
- dep = tokenized_sentence[dep_idx]
248
- start, end = getEntitiesPositions(text, dep)
249
- dep = {
250
- "entity": entities_label_mapping[1],
251
- "word": dep,
252
- "start": start,
253
- "end": end,
254
- }
 
 
255
 
256
  if 2 in entities:
257
  arr_idx = entities.index(2)
258
- arr = tokenized_sentence[arr_idx]
259
- start, end = getEntitiesPositions(text, arr)
260
- arr = {
261
- "entity": entities_label_mapping[2],
262
- "word": arr,
263
- "start": start,
264
- "end": end,
265
- }
 
 
266
 
267
  return dep, arr
268
 
 
31
  return render_tabs([promptAudio], model, progress)
32
 
33
 
34
+ def handle_text(text, model, progress=gr.Progress()):
 
35
  progress(0, desc=PROGRESS.ANALYZING_FILE.value)
36
  time.sleep(1)
37
+ if text and text.strip():
38
+ progress(0.33, desc=PROGRESS.READING_FILE.value)
39
+ sentences = [
40
+ sentence.strip() for sentence in text.split("\n") if sentence.strip()
41
+ ]
42
+ return render_tabs(sentences, model, progress)
 
43
 
44
 
45
  tabs_components = []
46
 
47
  with gr.Blocks() as demo:
48
+ # Add disclaimer
49
+ gr.HTML(
50
+ """
51
+ <div style="background-color: #f0f8ff; padding: 15px; border-radius: 5px; margin-bottom: 20px; border-left: 4px solid #007acc;">
52
+ <p style="margin: 0; font-size: 14px; color: #333;">
53
+ <strong>Disclaimer:</strong> This app is meant to showcase NER for French text and will only work for French text and cities in France since the database is from SNCF.
54
+ </p>
55
+ </div>
56
+ """
57
+ )
58
+
59
  with gr.Column():
60
  with gr.Row():
61
  audio = gr.Audio(label="Fichier audio", interactive=True)
62
+ with gr.Column():
63
+ # Example sentence button
64
+ example_sentence = "Je souhaite aller de Paris à Lyon demain matin."
65
+ example_btn = gr.Button(
66
+ 'Exemple : "' + example_sentence + '"',
67
+ variant="secondary",
68
+ )
69
+
70
+ text_input = gr.Textbox(
71
+ label="Texte français",
72
+ placeholder="Enter text : (ex: Je souhaite aller de Paris à Lyon demain matin.)",
73
+ lines=3,
74
+ interactive=True,
75
+ )
76
+
77
+ submit_btn = gr.Button("Analyser le texte", variant="primary")
78
 
79
  model = gr.Dropdown(
80
  label="Modèle NER",
 
83
  interactive=True,
84
  )
85
 
86
+ # Output container and state
87
+ text_state = gr.State("")
88
+ model_state = gr.State("CamemBERT")
89
+ audio_state = gr.State(None)
90
+
91
+ # Handle example button click
92
+ example_btn.click(lambda: example_sentence, outputs=[text_input])
93
+
94
+ # Handle submit button click - update state
95
+ submit_btn.click(
96
+ lambda text, model: (text, model),
97
+ inputs=[text_input, model],
98
+ outputs=[text_state, model_state],
99
  )
100
+
101
+ # Handle audio input - update state
102
+ audio.change(
103
+ lambda audio, model: (audio, model),
104
+ inputs=[audio, model],
105
+ outputs=[audio_state, model_state],
106
+ )
107
+
108
+ # Render output based on state changes
109
+ @gr.render(inputs=[text_state, model_state, audio_state])
110
+ def render_output(text_input_value, model_value, audio_value):
111
+ if audio_value is not None:
112
+ return handle_audio(audio_value, model_value)
113
+ elif text_input_value and text_input_value.strip():
114
+ return handle_text(text_input_value, model_value)
115
 
116
 
117
  def handleCityChange(city):
 
284
 
285
  if 1 in entities:
286
  dep_idx = entities.index(1)
287
+ # Add bounds checking to prevent IndexError
288
+ if dep_idx < len(tokenized_sentence):
289
+ dep = tokenized_sentence[dep_idx]
290
+ start, end = getEntitiesPositions(text, dep)
291
+ dep = {
292
+ "entity": entities_label_mapping[1],
293
+ "word": dep,
294
+ "start": start,
295
+ "end": end,
296
+ }
297
 
298
  if 2 in entities:
299
  arr_idx = entities.index(2)
300
+ # Add bounds checking to prevent IndexError
301
+ if arr_idx < len(tokenized_sentence):
302
+ arr = tokenized_sentence[arr_idx]
303
+ start, end = getEntitiesPositions(text, arr)
304
+ arr = {
305
+ "entity": entities_label_mapping[2],
306
+ "word": arr,
307
+ "start": start,
308
+ "end": end,
309
+ }
310
 
311
  return dep, arr
312