Spaces:
Sleeping
Sleeping
Tymoteusz commited on
Commit ·
fb652e4
1
Parent(s): 3a9a0e3
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from st_audiorec import st_audiorec
|
| 3 |
+
# import whisper
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# model = whisper.load_model("base")
|
| 7 |
+
|
| 8 |
+
# def transcribe(audio):
|
| 9 |
+
|
| 10 |
+
# #time.sleep(3)
|
| 11 |
+
# # load audio and pad/trim it to fit 30 seconds
|
| 12 |
+
# audio = whisper.load_audio(audio)
|
| 13 |
+
# audio = whisper.pad_or_trim(audio)
|
| 14 |
+
|
| 15 |
+
# # make log-Mel spectrogram and move to the same device as the model
|
| 16 |
+
# mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
| 17 |
+
|
| 18 |
+
# # detect the spoken language
|
| 19 |
+
# _, probs = model.detect_language(mel)
|
| 20 |
+
# print(f"Detected language: {max(probs, key=probs.get)}")
|
| 21 |
+
|
| 22 |
+
# # decode the audio
|
| 23 |
+
# options = whisper.DecodingOptions(fp16 = False)
|
| 24 |
+
# result = whisper.decode(model, mel, options)
|
| 25 |
+
# return result.text
|
| 26 |
+
|
| 27 |
+
import torch
|
| 28 |
+
from transformers import pipeline
|
| 29 |
+
|
| 30 |
+
# start model
|
| 31 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 32 |
+
|
| 33 |
+
translation_pipe = pipeline(
|
| 34 |
+
"automatic-speech-recognition", model="openai/whisper-base", device=device
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
def translate(audio, lang = "en"):
|
| 38 |
+
outputs = translation_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": lang})
|
| 39 |
+
return outputs["text"]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Customizing the Streamlit page layout
|
| 43 |
+
st.set_page_config(
|
| 44 |
+
page_title="tłumacz",
|
| 45 |
+
page_icon="🎤",
|
| 46 |
+
layout="wide",
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Center the app title and adjust padding
|
| 50 |
+
st.markdown(
|
| 51 |
+
"""
|
| 52 |
+
<style>
|
| 53 |
+
.stApp {
|
| 54 |
+
max-width: 1000px;
|
| 55 |
+
margin: 0 auto;
|
| 56 |
+
padding: 2rem;
|
| 57 |
+
}
|
| 58 |
+
.stTitle {
|
| 59 |
+
text-align: center;
|
| 60 |
+
}
|
| 61 |
+
</style>
|
| 62 |
+
""",
|
| 63 |
+
unsafe_allow_html=True
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
def audiorec_demo_app():
|
| 67 |
+
|
| 68 |
+
st.title('Audio Recorder App')
|
| 69 |
+
st.subheader('Record and Playback Audio')
|
| 70 |
+
|
| 71 |
+
# Add a little info on how to use the app
|
| 72 |
+
st.markdown(
|
| 73 |
+
"🎤 To record audio, simply click the 'Start Recording' button. "
|
| 74 |
+
"Once you're done, click 'Stop Recording' and 'Save Recording' to play it back. "
|
| 75 |
+
"You can also reset the recording if needed."
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
wav_audio_data = st_audiorec()
|
| 79 |
+
|
| 80 |
+
filename = "temp.wav"
|
| 81 |
+
|
| 82 |
+
if wav_audio_data is not None:
|
| 83 |
+
# Display the recorded audio
|
| 84 |
+
st.subheader('Transcribed text:')
|
| 85 |
+
with st.spinner("Transcribing..."):
|
| 86 |
+
|
| 87 |
+
with open(filename, "wb") as f:
|
| 88 |
+
f.write(wav_audio_data)
|
| 89 |
+
with open(filename, "rb") as f:
|
| 90 |
+
audio = f.read()
|
| 91 |
+
|
| 92 |
+
output_text = translate(audio, lang="pl")
|
| 93 |
+
translated_text = translate(audio, lang="en")
|
| 94 |
+
st.markdown(f"**Orginalny tekst [PL]:** {output_text}")
|
| 95 |
+
st.markdown(f"**Tłumaczenie [EN]:** {translated_text}")
|
| 96 |
+
|
| 97 |
+
if __name__ == '__main__':
|
| 98 |
+
audiorec_demo_app()
|