Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
# Transform an audio to text script with language detection.
|
| 2 |
# Author: Pratiksha Patel
|
| 3 |
-
|
| 4 |
# Description: This script record the audio, transform it to text, detect the language of the file and save it to a txt file.
|
| 5 |
# import required modules
|
| 6 |
import os
|
|
@@ -9,17 +8,12 @@ import streamlit as st
|
|
| 9 |
from audio_recorder_streamlit import audio_recorder
|
| 10 |
from langdetect import detect
|
| 11 |
import numpy as np
|
| 12 |
-
# Use a pipeline as a high-level helper
|
| 13 |
-
#from transformers import pipeline
|
| 14 |
-
#pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large")
|
| 15 |
-
# Load model directly
|
| 16 |
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 17 |
|
| 18 |
def transcribe_audio(audio_bytes):
|
| 19 |
processor = AutoProcessor.from_pretrained("openai/whisper-large")
|
| 20 |
model = AutoModelForSpeechSeq2Seq.from_pretrained("openai/whisper-large")
|
| 21 |
audio_array = np.frombuffer(audio_bytes, dtype=np.int16)
|
| 22 |
-
# Cast audio array to double precision and normalize
|
| 23 |
audio_tensor = torch.tensor(audio_array, dtype=torch.float64) / 32768.0
|
| 24 |
input_values = processor(audio_tensor, return_tensors="pt", sampling_rate=16000).input_values
|
| 25 |
logits = model(input_values).logits
|
|
@@ -27,17 +21,6 @@ def transcribe_audio(audio_bytes):
|
|
| 27 |
transcription = processor.decode(predicted_ids[0])
|
| 28 |
return transcription
|
| 29 |
|
| 30 |
-
# Function to open a file
|
| 31 |
-
#def startfile(fn):
|
| 32 |
-
# os.system('open %s' % fn)
|
| 33 |
-
|
| 34 |
-
# Function to create and open a txt file
|
| 35 |
-
#def create_and_open_txt(text, filename):
|
| 36 |
-
# Create and write the text to a txt file
|
| 37 |
-
# with open(filename, "w") as file:
|
| 38 |
-
# file.write(text)
|
| 39 |
-
# startfile(filename)
|
| 40 |
-
|
| 41 |
# Streamlit app
|
| 42 |
st.title("Audio to Text Transcription..")
|
| 43 |
|
|
@@ -55,9 +38,3 @@ if audio_bytes:
|
|
| 55 |
st.write("Error: Failed to transcribe audio.")
|
| 56 |
else:
|
| 57 |
st.write("No audio recorded.")
|
| 58 |
-
# Detect the language
|
| 59 |
-
#language = detect(transcription)
|
| 60 |
-
#st.write(f"Detected language: {language}")
|
| 61 |
-
|
| 62 |
-
# Create and open a txt file with the text
|
| 63 |
-
#create_and_open_txt(transcription, f"output_{language}.txt")
|
|
|
|
| 1 |
# Transform an audio to text script with language detection.
|
| 2 |
# Author: Pratiksha Patel
|
|
|
|
| 3 |
# Description: This script record the audio, transform it to text, detect the language of the file and save it to a txt file.
|
| 4 |
# import required modules
|
| 5 |
import os
|
|
|
|
| 8 |
from audio_recorder_streamlit import audio_recorder
|
| 9 |
from langdetect import detect
|
| 10 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 12 |
|
| 13 |
def transcribe_audio(audio_bytes):
|
| 14 |
processor = AutoProcessor.from_pretrained("openai/whisper-large")
|
| 15 |
model = AutoModelForSpeechSeq2Seq.from_pretrained("openai/whisper-large")
|
| 16 |
audio_array = np.frombuffer(audio_bytes, dtype=np.int16)
|
|
|
|
| 17 |
audio_tensor = torch.tensor(audio_array, dtype=torch.float64) / 32768.0
|
| 18 |
input_values = processor(audio_tensor, return_tensors="pt", sampling_rate=16000).input_values
|
| 19 |
logits = model(input_values).logits
|
|
|
|
| 21 |
transcription = processor.decode(predicted_ids[0])
|
| 22 |
return transcription
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Streamlit app
|
| 25 |
st.title("Audio to Text Transcription..")
|
| 26 |
|
|
|
|
| 38 |
st.write("Error: Failed to transcribe audio.")
|
| 39 |
else:
|
| 40 |
st.write("No audio recorded.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|