Spaces:
Runtime error
Runtime error
algo update
Browse files- app/matcher.py +3 -23
- app/routers/V1/voice/voice_router.py +13 -11
- app/string_processor.py +18 -0
app/matcher.py
CHANGED
@@ -18,29 +18,9 @@ def sequence_match(a, b):
|
|
18 |
return difflib.SequenceMatcher(None, a, b).ratio()
|
19 |
|
20 |
|
21 |
-
# Main function to compare texts with percentage match
|
22 |
-
def compare_texts(text1, text2):
|
23 |
-
"""
|
24 |
-
Compares two texts using phonetic matching and sequence matching,
|
25 |
-
returning a percentage match score.
|
26 |
-
"""
|
27 |
-
words1 = text1.lower().split()
|
28 |
-
words2 = text2.lower().split()
|
29 |
-
|
30 |
-
total_matches = len(words1)
|
31 |
-
mismatches = 0
|
32 |
-
|
33 |
-
for word1, word2 in zip(words1, words2):
|
34 |
-
if word1 != word2:
|
35 |
-
mismatches += 1
|
36 |
-
if phonetic_match(word1, word2) < 80:
|
37 |
-
# Use sequence matching only if phonetic is low
|
38 |
-
if sequence_match(word1, word2) < 0.8:
|
39 |
-
mismatches += 1 # Penalty for bad sequence match
|
40 |
-
|
41 |
-
accuracy = 1 - (mismatches / total_matches)
|
42 |
-
return accuracy * 100 # Convert to percentage
|
43 |
|
44 |
|
45 |
def match(original, transcription):
|
46 |
-
|
|
|
|
|
|
18 |
return difflib.SequenceMatcher(None, a, b).ratio()
|
19 |
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
def match(original, transcription):
|
24 |
+
sequence = sequence_match(original, transcription)
|
25 |
+
phonetic = phonetic_match(original, transcription)
|
26 |
+
return sequence, phonetic
|
app/routers/V1/voice/voice_router.py
CHANGED
@@ -6,6 +6,7 @@ import os
|
|
6 |
from app.transcriber import get_transcription
|
7 |
from app.matcher import match
|
8 |
from app.mfcc import mfcc_similarty_check
|
|
|
9 |
|
10 |
|
11 |
""" initialize the router """
|
@@ -40,7 +41,6 @@ async def transcribe_audio(
|
|
40 |
with open(filename_original, "wb") as buffer:
|
41 |
buffer.write(original_bytes)
|
42 |
|
43 |
-
|
44 |
# Read file bytes
|
45 |
recorded_bytes = await recorded.read()
|
46 |
filename_recorded = f"audio_{int(time.time())}_recorded.wav"
|
@@ -49,19 +49,21 @@ async def transcribe_audio(
|
|
49 |
with open(filename_recorded, "wb") as buffer:
|
50 |
buffer.write(recorded_bytes)
|
51 |
|
52 |
-
|
53 |
try:
|
54 |
text = get_transcription(filename_recorded)
|
55 |
-
|
56 |
-
Euclidean, Cosine = mfcc_similarty_check(
|
|
|
|
|
57 |
return JSONResponse(
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
65 |
finally:
|
66 |
# Clean up the temporary file
|
67 |
os.remove(filename_original)
|
|
|
6 |
from app.transcriber import get_transcription
|
7 |
from app.matcher import match
|
8 |
from app.mfcc import mfcc_similarty_check
|
9 |
+
from app.string_processor import process_text
|
10 |
|
11 |
|
12 |
""" initialize the router """
|
|
|
41 |
with open(filename_original, "wb") as buffer:
|
42 |
buffer.write(original_bytes)
|
43 |
|
|
|
44 |
# Read file bytes
|
45 |
recorded_bytes = await recorded.read()
|
46 |
filename_recorded = f"audio_{int(time.time())}_recorded.wav"
|
|
|
49 |
with open(filename_recorded, "wb") as buffer:
|
50 |
buffer.write(recorded_bytes)
|
51 |
|
|
|
52 |
try:
|
53 |
text = get_transcription(filename_recorded)
|
54 |
+
sequence, phonetic = match(matcher_text, process_text(text))
|
55 |
+
Euclidean, Cosine = mfcc_similarty_check(
|
56 |
+
filename_original, filename_recorded
|
57 |
+
)
|
58 |
return JSONResponse(
|
59 |
+
{
|
60 |
+
"transcription": text,
|
61 |
+
"sequence": sequence,
|
62 |
+
"phonetic": phonetic,
|
63 |
+
"Cosine": Cosine,
|
64 |
+
"Euclidean": Euclidean,
|
65 |
+
}
|
66 |
+
)
|
67 |
finally:
|
68 |
# Clean up the temporary file
|
69 |
os.remove(filename_original)
|
app/string_processor.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import string
|
2 |
+
import re
|
3 |
+
|
4 |
+
|
5 |
+
def process_text(text):
|
6 |
+
# Step 1: Strip whitespace from both ends
|
7 |
+
text = text.strip()
|
8 |
+
|
9 |
+
# Step 2: Remove all punctuation (including full stops and commas)
|
10 |
+
text = text.translate(str.maketrans("", "", string.punctuation))
|
11 |
+
|
12 |
+
# Step 3: Extract sentences (assuming you want to keep the text as a whole sentence)
|
13 |
+
sentences = re.split(r"(?<=[.!?]) +", text)
|
14 |
+
|
15 |
+
# Combine the sentences back into a single string without punctuation
|
16 |
+
processed_text = " ".join(sentences)
|
17 |
+
|
18 |
+
return processed_text
|