Spaces:
Runtime error
Runtime error
chore: add default secret
Browse files
app.py
CHANGED
@@ -11,6 +11,8 @@ import base64
|
|
11 |
import uuid
|
12 |
import os
|
13 |
|
|
|
|
|
14 |
|
15 |
@dataclass
|
16 |
class AppState:
|
@@ -33,6 +35,15 @@ def create_client(api_key):
|
|
33 |
)
|
34 |
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
def determine_pause(audio, sampling_rate, state):
|
37 |
# Take the last 1 second of audio
|
38 |
pause_length = int(sampling_rate * 1) # 1 second
|
@@ -164,16 +175,50 @@ def response(state: AppState):
|
|
164 |
|
165 |
def set_api_key(api_key, state):
|
166 |
try:
|
167 |
-
state.client = create_client(
|
168 |
-
|
|
|
|
|
|
|
|
|
169 |
except Exception as e:
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
|
173 |
with gr.Blocks() as demo:
|
174 |
gr.Markdown("# Lepton AI LLM Voice Mode")
|
175 |
gr.Markdown(
|
176 |
-
"You can find Lepton AI
|
177 |
)
|
178 |
with gr.Row():
|
179 |
with gr.Column(scale=3):
|
@@ -201,10 +246,17 @@ with gr.Blocks() as demo:
|
|
201 |
|
202 |
state = gr.State(AppState())
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
set_key_button.click(
|
205 |
set_api_key,
|
206 |
inputs=[api_key_input, state],
|
207 |
-
outputs=[api_key_status, state],
|
208 |
)
|
209 |
|
210 |
stream = input_audio.stream(
|
|
|
11 |
import uuid
|
12 |
import os
|
13 |
|
14 |
+
print(os.getenv("API_KEY"))
|
15 |
+
|
16 |
|
17 |
@dataclass
|
18 |
class AppState:
|
|
|
35 |
)
|
36 |
|
37 |
|
38 |
+
def test_api_key(client):
|
39 |
+
# Try making a simple request to check if the API key works
|
40 |
+
try:
|
41 |
+
# Attempt to retrieve available models as a test
|
42 |
+
client.models.list()
|
43 |
+
except Exception as e:
|
44 |
+
raise e
|
45 |
+
|
46 |
+
|
47 |
def determine_pause(audio, sampling_rate, state):
|
48 |
# Take the last 1 second of audio
|
49 |
pause_length = int(sampling_rate * 1) # 1 second
|
|
|
175 |
|
176 |
def set_api_key(api_key, state):
|
177 |
try:
|
178 |
+
state.client = create_client(api_key)
|
179 |
+
test_api_key(state.client) # Test the provided API key
|
180 |
+
api_key_status = gr.update(value="API key set successfully!", visible=True)
|
181 |
+
api_key_input = gr.update(visible=False)
|
182 |
+
set_key_button = gr.update(visible=False)
|
183 |
+
return api_key_status, api_key_input, set_key_button, state
|
184 |
except Exception as e:
|
185 |
+
api_key_status = gr.update(
|
186 |
+
value="Invalid API key. Please try again.", visible=True
|
187 |
+
)
|
188 |
+
return api_key_status, None, None, state
|
189 |
+
|
190 |
+
|
191 |
+
def initial_setup(state):
|
192 |
+
api_key = os.getenv("API_KEY")
|
193 |
+
if api_key:
|
194 |
+
try:
|
195 |
+
state.client = create_client(api_key)
|
196 |
+
test_api_key(state.client) # Test the API key from the environment variable
|
197 |
+
api_key_status = gr.update(value="Use default API key", visible=True)
|
198 |
+
api_key_input = gr.update(visible=False)
|
199 |
+
set_key_button = gr.update(visible=False)
|
200 |
+
return api_key_status, api_key_input, set_key_button, state
|
201 |
+
except Exception as e:
|
202 |
+
# Failed to use the api_key, show input box
|
203 |
+
api_key_status = gr.update(
|
204 |
+
value="Failed to use API key from environment variable. Please enter a valid API key.",
|
205 |
+
visible=True,
|
206 |
+
)
|
207 |
+
api_key_input = gr.update(visible=True)
|
208 |
+
set_key_button = gr.update(visible=True)
|
209 |
+
return api_key_status, api_key_input, set_key_button, state
|
210 |
+
else:
|
211 |
+
# No API key in environment variable
|
212 |
+
api_key_status = gr.update(visible=False)
|
213 |
+
api_key_input = gr.update(visible=True)
|
214 |
+
set_key_button = gr.update(visible=True)
|
215 |
+
return api_key_status, api_key_input, set_key_button, state
|
216 |
|
217 |
|
218 |
with gr.Blocks() as demo:
|
219 |
gr.Markdown("# Lepton AI LLM Voice Mode")
|
220 |
gr.Markdown(
|
221 |
+
"You can find Lepton AI LLM voice doc [here](https://www.lepton.ai/playground/chat/llama-3.2-3b)"
|
222 |
)
|
223 |
with gr.Row():
|
224 |
with gr.Column(scale=3):
|
|
|
246 |
|
247 |
state = gr.State(AppState())
|
248 |
|
249 |
+
# Initial setup to set API key from environment variable
|
250 |
+
demo.load(
|
251 |
+
initial_setup,
|
252 |
+
inputs=state,
|
253 |
+
outputs=[api_key_status, api_key_input, set_key_button, state],
|
254 |
+
)
|
255 |
+
|
256 |
set_key_button.click(
|
257 |
set_api_key,
|
258 |
inputs=[api_key_input, state],
|
259 |
+
outputs=[api_key_status, api_key_input, set_key_button, state],
|
260 |
)
|
261 |
|
262 |
stream = input_audio.stream(
|