Update app.py
Browse files
app.py
CHANGED
|
@@ -9,16 +9,17 @@ import openai
|
|
| 9 |
from dataclasses import dataclass, field
|
| 10 |
from threading import Lock
|
| 11 |
|
| 12 |
-
# Lepton API setup
|
| 13 |
-
client = openai.OpenAI(
|
| 14 |
-
base_url="https://llama3-1-8b.lepton.run/api/v1/",
|
| 15 |
-
api_key=os.environ.get('LEPTON_API_TOKEN')
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
@dataclass
|
| 19 |
class AppState:
|
| 20 |
conversation: list = field(default_factory=list)
|
| 21 |
lock: Lock = field(default_factory=Lock)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def transcribe_audio(audio):
|
| 24 |
# This is a placeholder function. In a real-world scenario, you'd use a
|
|
@@ -26,40 +27,46 @@ def transcribe_audio(audio):
|
|
| 26 |
return "This is a dummy transcript. Please implement actual speech-to-text functionality."
|
| 27 |
|
| 28 |
def generate_response_and_audio(message, state):
|
|
|
|
|
|
|
|
|
|
| 29 |
with state.lock:
|
| 30 |
state.conversation.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def chat(message, state):
|
| 65 |
if not message:
|
|
@@ -87,9 +94,21 @@ def process_audio(audio, state):
|
|
| 87 |
|
| 88 |
return transcript, state
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
state = gr.State(AppState())
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column(scale=1):
|
| 95 |
audio_input = gr.Audio(source="microphone", type="numpy")
|
|
@@ -99,7 +118,8 @@ with gr.Blocks() as demo:
|
|
| 99 |
with gr.Column(scale=1):
|
| 100 |
audio_output = gr.Audio(label="Generated Audio")
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
|
| 105 |
demo.launch()
|
|
|
|
| 9 |
from dataclasses import dataclass, field
|
| 10 |
from threading import Lock
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@dataclass
|
| 13 |
class AppState:
|
| 14 |
conversation: list = field(default_factory=list)
|
| 15 |
lock: Lock = field(default_factory=Lock)
|
| 16 |
+
client: openai.OpenAI = None
|
| 17 |
+
|
| 18 |
+
def create_client(api_key):
|
| 19 |
+
return openai.OpenAI(
|
| 20 |
+
base_url="https://llama3-1-8b.lepton.run/api/v1/",
|
| 21 |
+
api_key=api_key
|
| 22 |
+
)
|
| 23 |
|
| 24 |
def transcribe_audio(audio):
|
| 25 |
# This is a placeholder function. In a real-world scenario, you'd use a
|
|
|
|
| 27 |
return "This is a dummy transcript. Please implement actual speech-to-text functionality."
|
| 28 |
|
| 29 |
def generate_response_and_audio(message, state):
|
| 30 |
+
if state.client is None:
|
| 31 |
+
raise gr.Error("Please enter a valid API key first.")
|
| 32 |
+
|
| 33 |
with state.lock:
|
| 34 |
state.conversation.append({"role": "user", "content": message})
|
| 35 |
|
| 36 |
+
try:
|
| 37 |
+
completion = state.client.chat.completions.create(
|
| 38 |
+
model="llama3-1-8b",
|
| 39 |
+
messages=state.conversation,
|
| 40 |
+
max_tokens=128,
|
| 41 |
+
stream=True,
|
| 42 |
+
extra_body={
|
| 43 |
+
"require_audio": "true",
|
| 44 |
+
"tts_preset_id": "jessica",
|
| 45 |
+
}
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
full_response = ""
|
| 49 |
+
audio_chunks = []
|
| 50 |
|
| 51 |
+
for chunk in completion:
|
| 52 |
+
if not chunk.choices:
|
| 53 |
+
continue
|
| 54 |
+
|
| 55 |
+
content = chunk.choices[0].delta.content
|
| 56 |
+
audio = getattr(chunk.choices[0], 'audio', [])
|
| 57 |
+
|
| 58 |
+
if content:
|
| 59 |
+
full_response += content
|
| 60 |
+
yield full_response, None, state
|
| 61 |
+
|
| 62 |
+
if audio:
|
| 63 |
+
audio_chunks.extend(audio)
|
| 64 |
+
audio_data = b''.join([base64.b64decode(a) for a in audio_chunks])
|
| 65 |
+
yield full_response, audio_data, state
|
| 66 |
|
| 67 |
+
state.conversation.append({"role": "assistant", "content": full_response})
|
| 68 |
+
except Exception as e:
|
| 69 |
+
raise gr.Error(f"Error generating response: {str(e)}")
|
| 70 |
|
| 71 |
def chat(message, state):
|
| 72 |
if not message:
|
|
|
|
| 94 |
|
| 95 |
return transcript, state
|
| 96 |
|
| 97 |
+
def set_api_key(api_key, state):
|
| 98 |
+
if not api_key:
|
| 99 |
+
raise gr.Error("Please enter a valid API key.")
|
| 100 |
+
state.client = create_client(api_key)
|
| 101 |
+
return "API key set successfully!", state
|
| 102 |
+
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
state = gr.State(AppState())
|
| 105 |
|
| 106 |
+
with gr.Row():
|
| 107 |
+
api_key_input = gr.Textbox(type="password", label="Enter your Lepton API Key")
|
| 108 |
+
set_key_button = gr.Button("Set API Key")
|
| 109 |
+
|
| 110 |
+
api_key_status = gr.Textbox(label="API Key Status", interactive=False)
|
| 111 |
+
|
| 112 |
with gr.Row():
|
| 113 |
with gr.Column(scale=1):
|
| 114 |
audio_input = gr.Audio(source="microphone", type="numpy")
|
|
|
|
| 118 |
with gr.Column(scale=1):
|
| 119 |
audio_output = gr.Audio(label="Generated Audio")
|
| 120 |
|
| 121 |
+
set_key_button.click(set_api_key, inputs=[api_key_input, state], outputs=[api_key_status, state])
|
| 122 |
+
audio_input.change(process_audio, inputs=[audio_input, state], outputs=[text_input, state])
|
| 123 |
+
text_input.submit(chat, inputs=[text_input, state], outputs=[chatbot, audio_output, state])
|
| 124 |
|
| 125 |
demo.launch()
|