Spaces:
Sleeping
Sleeping
fix: google speech_recognition
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@
|
|
2 |
import streamlit as st
|
3 |
import whisper
|
4 |
import speech_recognition as sr
|
|
|
5 |
import os
|
6 |
|
7 |
# Function to transcribe audio using OpenAI Whisper
|
@@ -19,6 +20,13 @@ def transcribe_speech_recognition(file_path):
|
|
19 |
result = r.recognize_google(audio)
|
20 |
return result
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Streamlit App
|
23 |
st.title('Transcriptor de Audio')
|
24 |
|
@@ -27,12 +35,10 @@ uploaded_file = st.file_uploader("Sube tu archivo de audio para transcribir", ty
|
|
27 |
if uploaded_file is not None:
|
28 |
file_details = {"FileName":uploaded_file.name, "FileType":uploaded_file.type, "FileSize":uploaded_file.size}
|
29 |
st.write(file_details)
|
30 |
-
|
31 |
-
# Make sure the temp directory exists
|
32 |
-
if not os.path.exists('temp'):
|
33 |
-
os.makedirs('temp')
|
34 |
|
35 |
-
|
|
|
|
|
36 |
f.write(uploaded_file.getbuffer())
|
37 |
|
38 |
st.write("Archivo de audio cargado correctamente. Por favor, selecciona el método de transcripción.")
|
@@ -40,11 +46,14 @@ if uploaded_file is not None:
|
|
40 |
|
41 |
if transcription_method == 'OpenAI Whisper':
|
42 |
model_name = st.selectbox('Escoge el modelo de Whisper', ('base', 'small', 'medium', 'large', 'tiny'))
|
|
|
|
|
|
|
43 |
|
44 |
if st.button('Transcribir'):
|
45 |
if transcription_method == 'OpenAI Whisper':
|
46 |
-
transcript = transcribe_whisper(model_name,
|
47 |
else:
|
48 |
-
transcript = transcribe_speech_recognition(
|
49 |
|
50 |
st.write(transcript)
|
|
|
2 |
import streamlit as st
|
3 |
import whisper
|
4 |
import speech_recognition as sr
|
5 |
+
from pydub import AudioSegment
|
6 |
import os
|
7 |
|
8 |
# Function to transcribe audio using OpenAI Whisper
|
|
|
20 |
result = r.recognize_google(audio)
|
21 |
return result
|
22 |
|
23 |
+
# Function to convert mp3 file to wav
|
24 |
+
def convert_mp3_to_wav(mp3_path):
|
25 |
+
audio = AudioSegment.from_mp3(mp3_path)
|
26 |
+
wav_path = mp3_path.replace('.mp3', '.wav')
|
27 |
+
audio.export(wav_path, format="wav")
|
28 |
+
return wav_path
|
29 |
+
|
30 |
# Streamlit App
|
31 |
st.title('Transcriptor de Audio')
|
32 |
|
|
|
35 |
if uploaded_file is not None:
|
36 |
file_details = {"FileName":uploaded_file.name, "FileType":uploaded_file.type, "FileSize":uploaded_file.size}
|
37 |
st.write(file_details)
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Save uploaded file to temp directory
|
40 |
+
file_path = os.path.join("temp", uploaded_file.name)
|
41 |
+
with open(file_path, "wb") as f:
|
42 |
f.write(uploaded_file.getbuffer())
|
43 |
|
44 |
st.write("Archivo de audio cargado correctamente. Por favor, selecciona el método de transcripción.")
|
|
|
46 |
|
47 |
if transcription_method == 'OpenAI Whisper':
|
48 |
model_name = st.selectbox('Escoge el modelo de Whisper', ('base', 'small', 'medium', 'large', 'tiny'))
|
49 |
+
elif transcription_method == 'Google Speech API' and file_path.endswith('.mp3'):
|
50 |
+
# Convert mp3 to wav if Google Speech API is selected and file is in mp3 format
|
51 |
+
file_path = convert_mp3_to_wav(file_path)
|
52 |
|
53 |
if st.button('Transcribir'):
|
54 |
if transcription_method == 'OpenAI Whisper':
|
55 |
+
transcript = transcribe_whisper(model_name, file_path)
|
56 |
else:
|
57 |
+
transcript = transcribe_speech_recognition(file_path)
|
58 |
|
59 |
st.write(transcript)
|