Spaces:
Sleeping
Sleeping
Shifted to Inference API
Browse files
app.py
CHANGED
|
@@ -1,47 +1,34 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import librosa
|
| 4 |
-
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 5 |
import Levenshtein
|
| 6 |
from io import BytesIO
|
| 7 |
from audio_recorder_streamlit import audio_recorder
|
| 8 |
|
| 9 |
-
#
|
| 10 |
@st.cache_resource
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
|
| 14 |
-
model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
|
| 15 |
-
return processor, model
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def transcribe_audio(audio_bytes):
|
| 20 |
"""
|
| 21 |
-
Transcribes speech from an audio file using
|
| 22 |
-
|
| 23 |
Args:
|
| 24 |
audio_bytes (bytes): Audio data in bytes.
|
| 25 |
-
|
| 26 |
Returns:
|
| 27 |
str: The transcription of the speech in the audio file.
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
predicted_ids = torch.argmax(logits, dim=-1)
|
| 34 |
-
transcription = processor.batch_decode(predicted_ids)[0].strip()
|
| 35 |
-
return transcription
|
| 36 |
|
| 37 |
def levenshtein_similarity(transcription1, transcription2):
|
| 38 |
"""
|
| 39 |
Calculate the Levenshtein similarity between two transcriptions.
|
| 40 |
-
|
| 41 |
Args:
|
| 42 |
transcription1 (str): The first transcription.
|
| 43 |
transcription2 (str): The second transcription.
|
| 44 |
-
|
| 45 |
Returns:
|
| 46 |
float: A normalized similarity score between 0 and 1, where 1 indicates identical transcriptions.
|
| 47 |
"""
|
|
@@ -52,16 +39,14 @@ def levenshtein_similarity(transcription1, transcription2):
|
|
| 52 |
def evaluate_audio_similarity(original_audio_bytes, user_audio_bytes):
|
| 53 |
"""
|
| 54 |
Compares the similarity between the transcription of an original audio file and a user's audio file.
|
| 55 |
-
|
| 56 |
Args:
|
| 57 |
original_audio_bytes (bytes): Bytes of the original audio file.
|
| 58 |
user_audio_bytes (bytes): Bytes of the user's audio file.
|
| 59 |
-
|
| 60 |
Returns:
|
| 61 |
tuple: Transcriptions and Levenshtein similarity score.
|
| 62 |
"""
|
| 63 |
-
transcription_original =
|
| 64 |
-
transcription_user =
|
| 65 |
similarity_score_levenshtein = levenshtein_similarity(transcription_original, transcription_user)
|
| 66 |
return transcription_original, transcription_user, similarity_score_levenshtein
|
| 67 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
|
|
|
|
|
|
| 3 |
import Levenshtein
|
| 4 |
from io import BytesIO
|
| 5 |
from audio_recorder_streamlit import audio_recorder
|
| 6 |
|
| 7 |
+
# Function to securely load the Hugging Face API token
|
| 8 |
@st.cache_resource
|
| 9 |
+
def load_hf_token():
|
| 10 |
+
return st.secrets["HF_API_KEY"]
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Function to query the Hugging Face Inference API
|
| 13 |
+
def transcribe_audio_hf(audio_bytes):
|
|
|
|
| 14 |
"""
|
| 15 |
+
Transcribes speech from an audio file using the Hugging Face Inference API.
|
|
|
|
| 16 |
Args:
|
| 17 |
audio_bytes (bytes): Audio data in bytes.
|
|
|
|
| 18 |
Returns:
|
| 19 |
str: The transcription of the speech in the audio file.
|
| 20 |
"""
|
| 21 |
+
API_URL = "https://api-inference.huggingface.co/models/jonatasgrosman/wav2vec2-large-xlsr-53-arabic"
|
| 22 |
+
headers = {"Authorization": f"Bearer {load_hf_token()}"}
|
| 23 |
+
response = requests.post(API_URL, headers=headers, data=audio_bytes)
|
| 24 |
+
return response.json().get("text", "").strip()
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def levenshtein_similarity(transcription1, transcription2):
|
| 27 |
"""
|
| 28 |
Calculate the Levenshtein similarity between two transcriptions.
|
|
|
|
| 29 |
Args:
|
| 30 |
transcription1 (str): The first transcription.
|
| 31 |
transcription2 (str): The second transcription.
|
|
|
|
| 32 |
Returns:
|
| 33 |
float: A normalized similarity score between 0 and 1, where 1 indicates identical transcriptions.
|
| 34 |
"""
|
|
|
|
| 39 |
def evaluate_audio_similarity(original_audio_bytes, user_audio_bytes):
|
| 40 |
"""
|
| 41 |
Compares the similarity between the transcription of an original audio file and a user's audio file.
|
|
|
|
| 42 |
Args:
|
| 43 |
original_audio_bytes (bytes): Bytes of the original audio file.
|
| 44 |
user_audio_bytes (bytes): Bytes of the user's audio file.
|
|
|
|
| 45 |
Returns:
|
| 46 |
tuple: Transcriptions and Levenshtein similarity score.
|
| 47 |
"""
|
| 48 |
+
transcription_original = transcribe_audio_hf(original_audio_bytes)
|
| 49 |
+
transcription_user = transcribe_audio_hf(user_audio_bytes)
|
| 50 |
similarity_score_levenshtein = levenshtein_similarity(transcription_original, transcription_user)
|
| 51 |
return transcription_original, transcription_user, similarity_score_levenshtein
|
| 52 |
|