File size: 2,724 Bytes
c712701
 
 
 
 
 
 
 
 
c0897c3
c712701
 
 
 
 
 
 
 
 
af07e65
1d2f8ee
 
 
 
c712701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
012a776
 
c712701
012a776
 
 
c712701
012a776
 
 
 
c712701
 
 
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
import requests
import pytz
import streamlit as st
import os

from datetime import datetime
from audio_recorder_streamlit import audio_recorder

# Filepath for saving the text
file_path = 'text_output.txt'

API_URL = 'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
headers = {
    "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "Content-Type": "audio/wav"
}

def query(filename):
    with open(filename, "rb") as f:
        data = f.read
    #try:
    response = requests.post(API_URL, headers=headers, data=data)
    #except:
    #    st.write('Whisper Voice Speech to Text Model is asleep. Starting up now on T4 - please give 3 minutes then retry as KEDA scales up from zero to activate running container(s).')
    return response.json()

def generate_filename(prompt, file_type):
    central = pytz.timezone('US/Central')
    safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
    replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
    safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
    return f"{safe_date_time}_{safe_prompt}.{file_type}"

def save_and_play_audio(audio_recorder):
    audio_bytes = audio_recorder()
    if audio_bytes:
        filename = generate_filename("Recording", "wav")
        with open(filename, 'wb') as f:
            f.write(audio_bytes)
        st.audio(audio_bytes, format="audio/wav")
        return filename

def transcribe_audio(filename):
    output = query(filename)
    return output

def save_transcription(transcription):
    with open(file_path, 'a') as f:
        f.write(f"{transcription}\n")

def load_previous_transcriptions():
    if os.path.exists(file_path):
        with open(file_path, 'r') as f:
            return f.read()
    return ""

def main():
    st.title("Speech to Text πŸŽ€πŸ“")
    st.write("Record your speech and get the text. πŸ—¨οΈ")

    previous_transcriptions = load_previous_transcriptions()
    text_area = st.text_area("Transcriptions:", previous_transcriptions, height=400)

    filename = save_and_play_audio(audio_recorder)
    if filename is not None:
        try:
            transcription = transcribe_audio(filename)
        
            # Update the text area with new transcription
            updated_transcriptions = f"{previous_transcriptions}\n{transcription}"
            st.text_area("Transcriptions:", updated_transcriptions, height=400)
        
            # Save the new transcription to file
            save_transcription(transcription)
        except:
            st.write('Whisperer loading..')

if __name__ == "__main__":
    main()