gerasdf commited on
Commit
fc4109c
·
1 Parent(s): a25d719

paying with audio

Browse files
Files changed (2) hide show
  1. app.py +74 -2
  2. requirements.txt +1 -1
app.py CHANGED
@@ -3,6 +3,9 @@ from time import sleep
3
  from json import loads as json_loads
4
  import gradio as gr
5
  from openai import OpenAI
 
 
 
6
  client = OpenAI()
7
 
8
  assistant_id = os_getenv("OPENAI_ASSISTANT_ID")
@@ -97,6 +100,8 @@ def new_state():
97
  return gr.State({
98
  "user": None,
99
  "thread": None,
 
 
100
  })
101
 
102
  def auth(token, state):
@@ -117,12 +122,74 @@ AUTH_JS = """function auth_js(token, state) {
117
  }
118
  """
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  with gr.Blocks(
122
  title="Je suis Le Petit Nicolas",
123
  fill_height=True,
124
  theme=gr.themes.Base()) as demo:
125
  state = new_state()
 
 
126
 
127
  gr.HTML("""
128
  <h1>Je suis Le Petit Nicolas</h1>
@@ -138,7 +205,7 @@ with gr.Blocks(
138
  label = "Tout au long de la réponse",
139
  scale=3)
140
 
141
- gr.ChatInterface(
142
  chat,
143
  additional_inputs=[state, long_or_short],
144
  examples=[
@@ -147,6 +214,11 @@ with gr.Blocks(
147
  ],
148
  )
149
 
 
 
 
 
 
150
  token = gr.Textbox(visible=False)
151
  demo.load(auth,
152
  [token,state],
@@ -155,4 +227,4 @@ with gr.Blocks(
155
 
156
  demo.launch(
157
  height=700,
158
- allowed_paths=["."])
 
3
  from json import loads as json_loads
4
  import gradio as gr
5
  from openai import OpenAI
6
+ # import assemblyai
7
+ # import queue
8
+
9
  client = OpenAI()
10
 
11
  assistant_id = os_getenv("OPENAI_ASSISTANT_ID")
 
100
  return gr.State({
101
  "user": None,
102
  "thread": None,
103
+ "text_queue": None,
104
+ "transcriber": None,
105
  })
106
 
107
  def auth(token, state):
 
122
  }
123
  """
124
 
125
+ def audio_on_load(state):
126
+ q = state["text_queue"] = queue.Queue()
127
+
128
+ transcriber = assemblyai.RealtimeTranscriber(
129
+ on_data=q.put,
130
+ on_error=q.put,
131
+ sample_rate=48000,
132
+ )
133
+ transcriber.connect()
134
+
135
+ state["transcriber"] = transcriber
136
+ return state, gr.update(interactive=True)
137
+
138
+ def audio_start(state, chati, audio):
139
+ if audio:
140
+ state["transcriber"].stream(audio[1].tobytes())
141
+ print(audio[0], len(audio[1]))
142
+
143
+ chati += [("", None)]
144
+ return state, chati
145
+
146
+ def audio_msg(msg, chati):
147
+ if msg is None:
148
+ return False
149
+ if isinstance(msg, assemblyai.RealtimeFinalTranscript):
150
+ chati[-1][0] = msg.text
151
+ chati += [("", None)]
152
+ return True
153
+ elif isinstance(msg, assemblyai.RealtimePartialTranscript):
154
+ chati[-1][0] = msg.text
155
+ elif isinstance(msg, assemblyai.RealtimeError):
156
+ print(f"-- {msg}")
157
+ gr.Info(str(msg))
158
+ else:
159
+ print(f"-- {msg}")
160
+
161
+ return False
162
+
163
+ def audio_stop(state, chati, audio):
164
+ for i in range(20):
165
+ try:
166
+ msg = state["text_queue"].get(timeout=1)
167
+ if audio_msg(msg, chati):
168
+ break
169
+ except queue.Empty:
170
+ pass
171
+
172
+ return state, chati
173
+
174
+ def audio_chunk(state, chati, audio):
175
+ state["transcriber"].stream(audio[1].tobytes())
176
+ print(audio[0], len(audio[1]))
177
+ try:
178
+ msg = state["text_queue"].get_nowait()
179
+ audio_msg(msg, chati)
180
+ except queue.Empty:
181
+ pass
182
+
183
+ return state, chati
184
+
185
 
186
  with gr.Blocks(
187
  title="Je suis Le Petit Nicolas",
188
  fill_height=True,
189
  theme=gr.themes.Base()) as demo:
190
  state = new_state()
191
+ # mic = gr.Microphone(streaming=True, interactive=False, render=False)
192
+ # demo.load(audio_on_load, inputs=state, outputs=[state, mic])
193
 
194
  gr.HTML("""
195
  <h1>Je suis Le Petit Nicolas</h1>
 
205
  label = "Tout au long de la réponse",
206
  scale=3)
207
 
208
+ chati = gr.ChatInterface(
209
  chat,
210
  additional_inputs=[state, long_or_short],
211
  examples=[
 
214
  ],
215
  )
216
 
217
+ # mic.render()
218
+ # mic.start_recording(audio_start, inputs=[state, chati.chatbot, mic], outputs=[state, chati.chatbot])
219
+ # mic.stop_recording(audio_stop, inputs=[state, chati.chatbot, mic], outputs=[state, chati.chatbot])
220
+ # mic.stream(audio_chunk, inputs=[state, chati.chatbot, mic], outputs=[state, chati.chatbot])
221
+
222
  token = gr.Textbox(visible=False)
223
  demo.load(auth,
224
  [token,state],
 
227
 
228
  demo.launch(
229
  height=700,
230
+ show_api=False)
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
  gradio
2
  openai
3
- yagmail
 
1
  gradio
2
  openai
3
+ assemblyai