Chandani777 commited on
Commit
19c4bab
1 Parent(s): 99f69b6

first commit

Browse files
Files changed (1) hide show
  1. Streamlit_app.py +135 -0
Streamlit_app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import websockets
3
+ import asyncio
4
+ import base64
5
+ import json
6
+ import pyaudio
7
+ import os
8
+ from pathlib import Path
9
+
10
+ # Session state
11
+ if 'text' not in st.session_state:
12
+ st.session_state['text'] = 'Listening...'
13
+ st.session_state['run'] = False
14
+
15
+ # Audio parameters
16
+ st.sidebar.header('Audio Parameters')
17
+
18
+ FRAMES_PER_BUFFER = int(st.sidebar.text_input('Frames per buffer', 3200))
19
+ FORMAT = pyaudio.paInt16
20
+ CHANNELS = 1
21
+ RATE = int(st.sidebar.text_input('Rate', 16000))
22
+ p = pyaudio.PyAudio()
23
+
24
+ # Open an audio stream with above parameter settings
25
+ stream = p.open(
26
+ format=FORMAT,
27
+ channels=CHANNELS,
28
+ rate=RATE,
29
+ input=True,
30
+ frames_per_buffer=FRAMES_PER_BUFFER
31
+ )
32
+
33
+ # Start/stop audio transmission
34
+ def start_listening():
35
+ st.session_state['run'] = True
36
+
37
+ def download_transcription():
38
+ read_txt = open('transcription.txt', 'r')
39
+ st.download_button(
40
+ label="Download transcription",
41
+ data=read_txt,
42
+ file_name='transcription_output.txt',
43
+ mime='text/plain')
44
+
45
+ def stop_listening():
46
+ st.session_state['run'] = False
47
+
48
+ # Web user interface
49
+ st.title('🎙️ Real-Time Transcription App')
50
+
51
+ with st.expander('About this App'):
52
+ st.markdown('''
53
+ This Streamlit app uses the AssemblyAI API to perform real-time transcription.''')
54
+
55
+ col1, col2 = st.columns(2)
56
+
57
+ col1.button('Start', on_click=start_listening)
58
+ col2.button('Stop', on_click=stop_listening)
59
+
60
+ # Send audio (Input) / Receive transcription (Output)
61
+ async def send_receive():
62
+ URL = f"wss://api.assemblyai.com/v2/realtime/ws?sample_rate={RATE}"
63
+
64
+ print(f'Connecting websocket to url ${URL}')
65
+
66
+ async with websockets.connect(
67
+ URL,
68
+ extra_headers=(("Authorization", st.secrets['api_key']),),
69
+ ping_interval=5,
70
+ ping_timeout=20
71
+ ) as _ws:
72
+
73
+ r = await asyncio.sleep(0.1)
74
+ print("Receiving messages ...")
75
+
76
+ session_begins = await _ws.recv()
77
+ print(session_begins)
78
+ print("Sending messages ...")
79
+
80
+
81
+ async def send():
82
+ while st.session_state['run']:
83
+ try:
84
+ data = stream.read(FRAMES_PER_BUFFER)
85
+ data = base64.b64encode(data).decode("utf-8")
86
+ json_data = json.dumps({"audio_data":str(data)})
87
+ r = await _ws.send(json_data)
88
+
89
+ except websockets.exceptions.ConnectionClosedError as e:
90
+ print(e)
91
+ assert e.code == 4008
92
+ break
93
+
94
+ except Exception as e:
95
+ print(e)
96
+ assert False, "Not a websocket 4008 error"
97
+
98
+ r = await asyncio.sleep(0.01)
99
+
100
+
101
+ async def receive():
102
+ while st.session_state['run']:
103
+ try:
104
+ result_str = await _ws.recv()
105
+ result = json.loads(result_str)['text']
106
+
107
+ if json.loads(result_str)['message_type']=='FinalTranscript':
108
+ print(result)
109
+ st.session_state['text'] = result
110
+ st.write(st.session_state['text'])
111
+
112
+ transcription_txt = open('transcription.txt', 'a')
113
+ transcription_txt.write(st.session_state['text'])
114
+ transcription_txt.write(' ')
115
+ transcription_txt.close()
116
+
117
+
118
+ except websockets.exceptions.ConnectionClosedError as e:
119
+ print(e)
120
+ assert e.code == 4008
121
+ break
122
+
123
+ except Exception as e:
124
+ print(e)
125
+ assert False, "Not a websocket 4008 error"
126
+
127
+ send_result, receive_result = await asyncio.gather(send(), receive())
128
+
129
+
130
+ asyncio.run(send_receive())
131
+
132
+ if Path('transcription.txt').is_file():
133
+ st.markdown('### Download')
134
+ download_transcription()
135
+ os.remove('transcription.txt')