Az-r-ow
commited on
Commit
·
e80c2cb
1
Parent(s):
3c21054
added text input box
Browse files- 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
|
| 35 |
-
print("file upload")
|
| 36 |
progress(0, desc=PROGRESS.ANALYZING_FILE.value)
|
| 37 |
time.sleep(1)
|
| 38 |
-
if
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
| 255 |
|
| 256 |
if 2 in entities:
|
| 257 |
arr_idx = entities.index(2)
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 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 |
|