Mudassir-75 commited on
Commit
5487b58
1 Parent(s): fd42012

Shifted to Inference API

Browse files
Files changed (1) hide show
  1. app.py +13 -28
app.py CHANGED
@@ -1,47 +1,34 @@
1
  import streamlit as st
2
- import torch
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
- # Load the processor and model for Wav2Vec2 once
10
  @st.cache_resource
11
- def load_model():
12
- MODEL_ID = "jonatasgrosman/wav2vec2-large-xlsr-53-arabic"
13
- processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
14
- model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
15
- return processor, model
16
 
17
- processor, model = load_model()
18
-
19
- def transcribe_audio(audio_bytes):
20
  """
21
- Transcribes speech from an audio file using a pretrained Wav2Vec2 model.
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
- speech_array, sampling_rate = librosa.load(BytesIO(audio_bytes), sr=16000)
30
- input_values = processor(speech_array, sampling_rate=sampling_rate, return_tensors="pt", padding=True).input_values
31
- with torch.no_grad():
32
- logits = model(input_values).logits
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 = transcribe_audio(original_audio_bytes)
64
- transcription_user = transcribe_audio(user_audio_bytes)
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