Soumen commited on
Commit
6941d8d
·
1 Parent(s): 55b4e8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -80
app.py CHANGED
@@ -26,12 +26,6 @@ import os
26
  #os.system('sudo mv -v ben.traineddata /usr/local/share/tessdata/')
27
  #os.system('pip install -q pytesseract')
28
  import streamlit as st
29
- import websockets
30
- import pyaudio
31
- from configure import api_key
32
- import json
33
- import asyncio
34
-
35
  import torch
36
  from transformers import AutoTokenizer, AutoModelWithLMHead, GPT2LMHeadModel
37
 
@@ -76,80 +70,7 @@ def main():
76
  This is a Natural Language Processing(NLP) Based App useful for basic NLP task
77
  NER,Sentiment, Spell Corrections and Summarization
78
  """)
79
- if "text" not in st.session_state:
80
- st.session_state["text"] = ""
81
- st.session_state["run"] = False
82
- def start_listening():
83
- st.session_state["run"] = True
84
- st.button("Say something", on_click=start_listening)
85
- text = st.text_input("What should I create?", value=st.session_state["text"])
86
- URL = "wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000"
87
- FRAMES_PER_BUFFER = 3200
88
- FORMAT = pyaudio.paInt16
89
- CHANNELS = 1
90
- RATE = 16000
91
- p = pyaudio.PyAudio()
92
- # starts recording
93
- stream = p.open(
94
- format=FORMAT,
95
- channels=CHANNELS,
96
- rate=RATE,
97
- input=True,
98
- frames_per_buffer=FRAMES_PER_BUFFER
99
- )
100
- async def send_receive():
101
- print(f'Connecting websocket to url ${URL}')
102
- async with websockets.connect(
103
- URL,
104
- extra_headers=(("Authorization", api_key),),
105
- ping_interval=5,
106
- ping_timeout=20
107
- ) as _ws:
108
-
109
- r = await asyncio.sleep(0.1)
110
- print("Receiving Session begins ...")
111
-
112
- session_begins = await _ws.recv()
113
-
114
- async def send():
115
- while st.session_state['run']:
116
- try:
117
- data = stream.read(FRAMES_PER_BUFFER)
118
- data = base64.b64encode(data).decode("utf-8")
119
- json_data = json.dumps({"audio_data":str(data)})
120
- r = await _ws.send(json_data)
121
- except websockets.exceptions.ConnectionClosedError as e:
122
- print(e)
123
- assert e.code == 4008
124
- break
125
- except Exception as e:
126
- print(e)
127
- assert False, "Not a websocket 4008 error"
128
-
129
- r = await asyncio.sleep(0.01)
130
-
131
-
132
- async def receive():
133
- while st.session_state['run']:
134
- try:
135
- result_str = await _ws.recv()
136
- result = json.loads(result_str)['text']
137
-
138
- if json.loads(result_str)['message_type'] == 'FinalTranscript':
139
- result = result.replace('.', '')
140
- result = result.replace('!', '')
141
- st.session_state['text'] = result
142
- st.session_state['run'] = False
143
- st.experimental_rerun()
144
- except websockets.exceptions.ConnectionClosedError as e:
145
- print(e)
146
- assert e.code == 4008
147
- break
148
- except Exception as e:
149
- print(e)
150
- assert False, "Not a websocket 4008 error"
151
-
152
- send_result, receive_result = await asyncio.gather(send(), receive())
153
  # Entity Extraction
154
  if st.checkbox("Show Named Entities"):
155
  st.subheader("Analyze Your Text")
 
26
  #os.system('sudo mv -v ben.traineddata /usr/local/share/tessdata/')
27
  #os.system('pip install -q pytesseract')
28
  import streamlit as st
 
 
 
 
 
 
29
  import torch
30
  from transformers import AutoTokenizer, AutoModelWithLMHead, GPT2LMHeadModel
31
 
 
70
  This is a Natural Language Processing(NLP) Based App useful for basic NLP task
71
  NER,Sentiment, Spell Corrections and Summarization
72
  """)
73
+ text = st.text_input("Type your text!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # Entity Extraction
75
  if st.checkbox("Show Named Entities"):
76
  st.subheader("Analyze Your Text")