File size: 2,669 Bytes
fb652e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import streamlit as st
from st_audiorec import st_audiorec
# import whisper
import numpy as np

# model = whisper.load_model("base")

# def transcribe(audio):
    
#     #time.sleep(3)
#     # load audio and pad/trim it to fit 30 seconds
#     audio = whisper.load_audio(audio)
#     audio = whisper.pad_or_trim(audio)

#     # make log-Mel spectrogram and move to the same device as the model
#     mel = whisper.log_mel_spectrogram(audio).to(model.device)

#     # detect the spoken language
#     _, probs = model.detect_language(mel)
#     print(f"Detected language: {max(probs, key=probs.get)}")

#     # decode the audio
#     options = whisper.DecodingOptions(fp16 = False)
#     result = whisper.decode(model, mel, options)
#     return result.text

import torch
from transformers import pipeline

# start model
device = "cuda:0" if torch.cuda.is_available() else "cpu"

translation_pipe = pipeline(
        "automatic-speech-recognition", model="openai/whisper-base", device=device
    )

def translate(audio, lang = "en"):
    outputs = translation_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": lang})
    return outputs["text"]
    
    
# Customizing the Streamlit page layout
st.set_page_config(
    page_title="tłumacz",
    page_icon="🎤",
    layout="wide",
)

# Center the app title and adjust padding
st.markdown(
    """
    <style>
    .stApp {
        max-width: 1000px;
        margin: 0 auto;
        padding: 2rem;
    }
    .stTitle {
        text-align: center;
    }
    </style>
    """,
    unsafe_allow_html=True
)

def audiorec_demo_app():

    st.title('Audio Recorder App')
    st.subheader('Record and Playback Audio')

    # Add a little info on how to use the app
    st.markdown(
        "🎤 To record audio, simply click the 'Start Recording' button. "
        "Once you're done, click 'Stop Recording' and 'Save Recording' to play it back. "
        "You can also reset the recording if needed."
    )

    wav_audio_data = st_audiorec()

    filename = "temp.wav"

    if wav_audio_data is not None:
        # Display the recorded audio
        st.subheader('Transcribed text:')
        with st.spinner("Transcribing..."):

            with open(filename, "wb") as f:
                f.write(wav_audio_data)
            with open(filename, "rb") as f:
                audio = f.read()

            output_text = translate(audio, lang="pl")
            translated_text = translate(audio, lang="en")
            st.markdown(f"**Orginalny tekst [PL]:** {output_text}")
            st.markdown(f"**Tłumaczenie [EN]:** {translated_text}")

if __name__ == '__main__':
    audiorec_demo_app()