Lokis commited on
Commit
cb4cc22
1 Parent(s): c2735f4

Increase readability

Browse files
Files changed (1) hide show
  1. app.py +47 -35
app.py CHANGED
@@ -2,16 +2,14 @@ import gradio as gr
2
  import numpy as np
3
  import io
4
  from pydub import AudioSegment
5
- import tempfile
6
  import requests
7
- import time
8
  from dataclasses import dataclass, field
9
  from threading import Lock
10
  import base64
11
  import uuid
12
- import os
13
  import json
14
  import sseclient
 
15
 
16
  @dataclass
17
  class AppState:
@@ -142,49 +140,63 @@ def response(state: AppState):
142
 
143
  state.stream = None
144
 
145
- def set_api_key(api_key, state):
146
- state.api_key = api_key
147
- api_key_status = gr.update(value="API key set successfully!", visible=True)
148
- api_key_input = gr.update(visible=False)
149
- set_key_button = gr.update(visible=False)
150
- return api_key_status, api_key_input, set_key_button, state
151
-
152
  def initial_setup(state):
153
- if state.api_key:
154
- api_key_status = gr.update(value="Using default API key", visible=True)
155
- api_key_input = gr.update(visible=False)
156
- set_key_button = gr.update(visible=False)
157
- else:
158
- api_key_status = gr.update(visible=False)
159
- api_key_input = gr.update(visible=True)
160
- set_key_button = gr.update(visible=True)
161
- return api_key_status, api_key_input, set_key_button, state
162
 
163
  with gr.Blocks() as demo:
164
  gr.Markdown("# LLM Voice Mode")
165
- with gr.Row():
166
- with gr.Column(scale=3):
167
- api_key_input = gr.Textbox(type="password", placeholder="Enter your API Key", show_label=False, container=False)
168
- with gr.Column(scale=1):
169
- set_key_button = gr.Button("Set API Key", scale=2, variant="primary")
170
-
171
- api_key_status = gr.Textbox(show_label=False, container=False, interactive=False, visible=False)
172
 
173
  with gr.Blocks():
174
  with gr.Row():
175
- input_audio = gr.Audio(label="Input Audio", sources="microphone", type="numpy")
176
- output_audio = gr.Audio(label="Output Audio", autoplay=True, streaming=True)
177
- chatbot = gr.Chatbot(label="Conversation", type="messages")
 
 
 
 
 
 
 
 
 
 
 
178
 
179
  state = gr.State(AppState())
180
 
181
- demo.load(initial_setup, inputs=state, outputs=[api_key_status, api_key_input, set_key_button, state])
182
-
183
- set_key_button.click(set_api_key, inputs=[api_key_input, state], outputs=[api_key_status, api_key_input, set_key_button, state])
 
 
184
 
185
- stream = input_audio.stream(process_audio, [input_audio, state], [state], stream_every=0.25, time_limit=60)
 
 
 
 
 
 
186
 
187
- respond = input_audio.stop_recording(response, [state], [chatbot, output_audio, state])
188
- respond.then(lambda s: s.conversation, [state], [chatbot])
 
 
 
 
 
 
 
 
189
 
190
  demo.launch()
 
2
  import numpy as np
3
  import io
4
  from pydub import AudioSegment
 
5
  import requests
 
6
  from dataclasses import dataclass, field
7
  from threading import Lock
8
  import base64
9
  import uuid
 
10
  import json
11
  import sseclient
12
+ import os
13
 
14
  @dataclass
15
  class AppState:
 
140
 
141
  state.stream = None
142
 
 
 
 
 
 
 
 
143
  def initial_setup(state):
144
+ if not state.api_key:
145
+ raise gr.Error("API key not found in environment variables. Please set the API_KEY environment variable.")
146
+ return gr.update(value="The API key used is supported by Herm studio", visible=True)
 
 
 
 
 
 
147
 
148
  with gr.Blocks() as demo:
149
  gr.Markdown("# LLM Voice Mode")
150
+
151
+ api_key_status = gr.Textbox(
152
+ show_label=False,
153
+ container=False,
154
+ interactive=False,
155
+ visible=True
156
+ )
157
 
158
  with gr.Blocks():
159
  with gr.Row():
160
+ input_audio = gr.Audio(
161
+ label="Input Audio",
162
+ sources="microphone",
163
+ type="numpy"
164
+ )
165
+ output_audio = gr.Audio(
166
+ label="Output Audio",
167
+ autoplay=True,
168
+ streaming=True
169
+ )
170
+ chatbot = gr.Chatbot(
171
+ label="Conversation",
172
+ type="messages"
173
+ )
174
 
175
  state = gr.State(AppState())
176
 
177
+ demo.load(
178
+ fn=initial_setup,
179
+ inputs=state,
180
+ outputs=api_key_status
181
+ )
182
 
183
+ input_audio.stream(
184
+ fn=process_audio,
185
+ inputs=[input_audio, state],
186
+ outputs=[state],
187
+ stream_every=0.25,
188
+ time_limit=60
189
+ )
190
 
191
+ respond = input_audio.stop_recording(
192
+ fn=response,
193
+ inputs=[state],
194
+ outputs=[chatbot, output_audio, state]
195
+ )
196
+ respond.then(
197
+ fn=lambda s: s.conversation,
198
+ inputs=[state],
199
+ outputs=[chatbot]
200
+ )
201
 
202
  demo.launch()