awacke1 commited on
Commit
c712701
Β·
1 Parent(s): 84c2b81

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import pytz
3
+ import streamlit as st
4
+ import os
5
+
6
+ from datetime import datetime
7
+ from audio_recorder_streamlit import audio_recorder
8
+
9
+ # Filepath for saving the text
10
+ file_path = '/mnt/data/text_output.txt'
11
+
12
+ API_URL = 'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
13
+ headers = {
14
+ "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
15
+ "Content-Type": "audio/wav"
16
+ }
17
+
18
+ def query(filename):
19
+ with open(filename, "rb") as f:
20
+ data = f.read()
21
+ response = requests.post(API_URL, headers=headers, data=data)
22
+ return response.json()
23
+
24
+ def generate_filename(prompt, file_type):
25
+ central = pytz.timezone('US/Central')
26
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
27
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
28
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
29
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
30
+
31
+ def save_and_play_audio(audio_recorder):
32
+ audio_bytes = audio_recorder()
33
+ if audio_bytes:
34
+ filename = generate_filename("Recording", "wav")
35
+ with open(filename, 'wb') as f:
36
+ f.write(audio_bytes)
37
+ st.audio(audio_bytes, format="audio/wav")
38
+ return filename
39
+
40
+ def transcribe_audio(filename):
41
+ output = query(filename)
42
+ return output
43
+
44
+ def save_transcription(transcription):
45
+ with open(file_path, 'a') as f:
46
+ f.write(f"{transcription}\n")
47
+
48
+ def load_previous_transcriptions():
49
+ if os.path.exists(file_path):
50
+ with open(file_path, 'r') as f:
51
+ return f.read()
52
+ return ""
53
+
54
+ def main():
55
+ st.title("Speech to Text πŸŽ€πŸ“")
56
+ st.write("Record your speech and get the text. πŸ—¨οΈ")
57
+
58
+ previous_transcriptions = load_previous_transcriptions()
59
+ text_area = st.text_area("Transcriptions:", previous_transcriptions, height=400)
60
+
61
+ filename = save_and_play_audio(audio_recorder)
62
+ if filename is not None:
63
+ transcription = transcribe_audio(filename)
64
+
65
+ # Update the text area with new transcription
66
+ updated_transcriptions = f"{previous_transcriptions}\n{transcription}"
67
+ st.text_area("Transcriptions:", updated_transcriptions, height=400)
68
+
69
+ # Save the new transcription to file
70
+ save_transcription(transcription)
71
+
72
+ if __name__ == "__main__":
73
+ main()