mirror of
https://github.com/HumanAIGC-Engineering/gradio-webrtc.git
synced 2026-02-05 09:59:22 +08:00
Fix openai demo (#279)
This commit is contained in:
@@ -50,15 +50,32 @@ class OpenAIHandler(AsyncStreamHandler):
|
||||
model="gpt-4o-mini-realtime-preview-2024-12-17"
|
||||
) as conn:
|
||||
await conn.session.update(
|
||||
session={"turn_detection": {"type": "server_vad"}}
|
||||
session={
|
||||
"turn_detection": {"type": "server_vad"},
|
||||
"input_audio_transcription": {
|
||||
"model": "whisper-1",
|
||||
"language": "en",
|
||||
},
|
||||
}
|
||||
)
|
||||
self.connection = conn
|
||||
async for event in self.connection:
|
||||
# Handle interruptions
|
||||
if event.type == "input_audio_buffer.speech_started":
|
||||
self.clear_queue()
|
||||
if (
|
||||
event.type
|
||||
== "conversation.item.input_audio_transcription.completed"
|
||||
):
|
||||
await self.output_queue.put(
|
||||
AdditionalOutputs({"role": "user", "content": event.transcript})
|
||||
)
|
||||
if event.type == "response.audio_transcript.done":
|
||||
await self.output_queue.put(AdditionalOutputs(event))
|
||||
await self.output_queue.put(
|
||||
AdditionalOutputs(
|
||||
{"role": "assistant", "content": event.transcript}
|
||||
)
|
||||
)
|
||||
if event.type == "response.audio.delta":
|
||||
await self.output_queue.put(
|
||||
(
|
||||
@@ -124,7 +141,7 @@ def _(webrtc_id: str):
|
||||
import json
|
||||
|
||||
async for output in stream.output_stream(webrtc_id):
|
||||
s = json.dumps({"role": "assistant", "content": output.args[0].transcript})
|
||||
s = json.dumps(output.args[0])
|
||||
yield f"event: output\ndata: {s}\n\n"
|
||||
|
||||
return StreamingResponse(output_stream(), media_type="text/event-stream")
|
||||
|
||||
@@ -45,20 +45,26 @@
|
||||
|
||||
.message {
|
||||
margin-bottom: 20px;
|
||||
padding: 12px;
|
||||
border-radius: 4px;
|
||||
padding: 12px 16px;
|
||||
border-radius: 8px;
|
||||
font-size: 16px;
|
||||
line-height: 1.5;
|
||||
max-width: 70%;
|
||||
clear: both;
|
||||
}
|
||||
|
||||
.message.user {
|
||||
background-color: #1a1a1a;
|
||||
margin-left: 20%;
|
||||
background-color: #2c2c2c;
|
||||
float: right;
|
||||
border-bottom-right-radius: 2px;
|
||||
border: 1px solid #404040;
|
||||
}
|
||||
|
||||
.message.assistant {
|
||||
background-color: #262626;
|
||||
margin-right: 20%;
|
||||
float: left;
|
||||
border-bottom-left-radius: 2px;
|
||||
border: 1px solid #333;
|
||||
}
|
||||
|
||||
.controls {
|
||||
@@ -435,7 +441,7 @@
|
||||
const eventSource = new EventSource('/outputs?webrtc_id=' + webrtc_id);
|
||||
eventSource.addEventListener("output", (event) => {
|
||||
const eventJson = JSON.parse(event.data);
|
||||
addMessage("assistant", eventJson.content);
|
||||
addMessage(eventJson.role, eventJson.content);
|
||||
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user