Vadym Myroshnyk commited on
Commit ·
2b6b332
1
Parent(s): 0953a96
save
Browse files
listen_transcribe/highlighter.py
CHANGED
|
@@ -7,29 +7,29 @@ def highlight_fuzzy_diff(user_text, original_text, threshold=85):
|
|
| 7 |
|
| 8 |
highlighted = []
|
| 9 |
user_index = 0
|
|
|
|
| 10 |
|
| 11 |
for orig_word in original_words:
|
| 12 |
if user_index < len(user_words):
|
| 13 |
user_word = user_words[user_index]
|
| 14 |
score = fuzz.ratio(orig_word, user_word)
|
| 15 |
if score >= threshold:
|
| 16 |
-
|
| 17 |
highlighted.append(
|
| 18 |
f"<span style='background-color:#e6ffe6; color:#006600; padding:4px 10px; "
|
| 19 |
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>{orig_word}</span>"
|
| 20 |
)
|
| 21 |
user_index += 1
|
| 22 |
else:
|
| 23 |
-
# ❌ Word mismatch, consider as missing
|
| 24 |
highlighted.append(
|
| 25 |
f"<span style='background-color:#ffe6e6; color:#990000; padding:4px 10px; "
|
| 26 |
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>...</span>"
|
| 27 |
)
|
| 28 |
else:
|
| 29 |
-
# ❌ No more user input, word is missing
|
| 30 |
highlighted.append(
|
| 31 |
f"<span style='background-color:#ffe6e6; color:#990000; padding:4px 10px; "
|
| 32 |
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>...</span>"
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
| 7 |
|
| 8 |
highlighted = []
|
| 9 |
user_index = 0
|
| 10 |
+
correct_count = 0
|
| 11 |
|
| 12 |
for orig_word in original_words:
|
| 13 |
if user_index < len(user_words):
|
| 14 |
user_word = user_words[user_index]
|
| 15 |
score = fuzz.ratio(orig_word, user_word)
|
| 16 |
if score >= threshold:
|
| 17 |
+
correct_count += 1
|
| 18 |
highlighted.append(
|
| 19 |
f"<span style='background-color:#e6ffe6; color:#006600; padding:4px 10px; "
|
| 20 |
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>{orig_word}</span>"
|
| 21 |
)
|
| 22 |
user_index += 1
|
| 23 |
else:
|
|
|
|
| 24 |
highlighted.append(
|
| 25 |
f"<span style='background-color:#ffe6e6; color:#990000; padding:4px 10px; "
|
| 26 |
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>...</span>"
|
| 27 |
)
|
| 28 |
else:
|
|
|
|
| 29 |
highlighted.append(
|
| 30 |
f"<span style='background-color:#ffe6e6; color:#990000; padding:4px 10px; "
|
| 31 |
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>...</span>"
|
| 32 |
)
|
| 33 |
|
| 34 |
+
score_percent = round((correct_count / len(original_words)) * 100) if original_words else 0
|
| 35 |
+
return " ".join(highlighted), score_percent
|
listen_transcribe/listen_transcribe_ui.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
-
from rapidfuzz import fuzz
|
| 5 |
|
| 6 |
from listen_transcribe.audio_files import get_audio_structure
|
| 7 |
from listen_transcribe.dialogue import load_dialogue_lines
|
|
@@ -96,12 +95,10 @@ def transcribe_ui():
|
|
| 96 |
def on_check(*user_inputs):
|
| 97 |
feedback = []
|
| 98 |
highlights = []
|
| 99 |
-
scores = []
|
| 100 |
|
| 101 |
for user_input, (line_index, _, _) in zip(user_inputs, inputs):
|
| 102 |
expected = lines[line_index][1]
|
| 103 |
-
score =
|
| 104 |
-
scores.append(score)
|
| 105 |
|
| 106 |
if score > 90:
|
| 107 |
emoji, msg, color = "✅", "Excellent!", "#2ecc71"
|
|
@@ -111,8 +108,8 @@ def transcribe_ui():
|
|
| 111 |
emoji, msg, color = "❌", "Try again", "#e74c3c"
|
| 112 |
|
| 113 |
fb_html = f"<div style='color:{color}; font-size: 14px;'>{emoji} <b>{score}%</b> — {msg}</div>"
|
|
|
|
| 114 |
feedback.append(fb_html)
|
| 115 |
-
highlights.append(highlight_fuzzy_diff(user_input, expected))
|
| 116 |
|
| 117 |
return highlights + feedback
|
| 118 |
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
from listen_transcribe.audio_files import get_audio_structure
|
| 6 |
from listen_transcribe.dialogue import load_dialogue_lines
|
|
|
|
| 95 |
def on_check(*user_inputs):
|
| 96 |
feedback = []
|
| 97 |
highlights = []
|
|
|
|
| 98 |
|
| 99 |
for user_input, (line_index, _, _) in zip(user_inputs, inputs):
|
| 100 |
expected = lines[line_index][1]
|
| 101 |
+
diff_html, score = highlight_fuzzy_diff(user_input, expected)
|
|
|
|
| 102 |
|
| 103 |
if score > 90:
|
| 104 |
emoji, msg, color = "✅", "Excellent!", "#2ecc71"
|
|
|
|
| 108 |
emoji, msg, color = "❌", "Try again", "#e74c3c"
|
| 109 |
|
| 110 |
fb_html = f"<div style='color:{color}; font-size: 14px;'>{emoji} <b>{score}%</b> — {msg}</div>"
|
| 111 |
+
highlights.append(diff_html)
|
| 112 |
feedback.append(fb_html)
|
|
|
|
| 113 |
|
| 114 |
return highlights + feedback
|
| 115 |
|