Spaces:
Runtime error
Runtime error
Increase readability
Browse files
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 |
-
|
155 |
-
|
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 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
|
173 |
with gr.Blocks():
|
174 |
with gr.Row():
|
175 |
-
input_audio = gr.Audio(
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
state = gr.State(AppState())
|
180 |
|
181 |
-
demo.load(
|
182 |
-
|
183 |
-
|
|
|
|
|
184 |
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
-
respond = input_audio.stop_recording(
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|