Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,77 @@ if not api_key:
|
|
| 20 |
# Initialize OpenAI client with older API syntax
|
| 21 |
openai.api_key = api_key
|
| 22 |
|
| 23 |
-
# Custom
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def create_audio_recorder():
|
| 25 |
return gr.HTML("""
|
| 26 |
<div class="audio-recorder">
|
|
@@ -28,48 +98,9 @@ def create_audio_recorder():
|
|
| 28 |
<span class="record-icon">🎤</span>
|
| 29 |
<span class="record-text">Start Recording</span>
|
| 30 |
</button>
|
| 31 |
-
<div id="recordingStatus" class="
|
| 32 |
-
<audio id="audioPlayback" controls style="display: none;"></audio>
|
| 33 |
</div>
|
| 34 |
-
<style>
|
| 35 |
-
.audio-recorder {
|
| 36 |
-
display: flex;
|
| 37 |
-
flex-direction: column;
|
| 38 |
-
align-items: center;
|
| 39 |
-
gap: 10px;
|
| 40 |
-
padding: 15px;
|
| 41 |
-
border-radius: 8px;
|
| 42 |
-
background: #f5f5f5;
|
| 43 |
-
}
|
| 44 |
-
.record-button {
|
| 45 |
-
display: flex;
|
| 46 |
-
align-items: center;
|
| 47 |
-
gap: 8px;
|
| 48 |
-
padding: 10px 20px;
|
| 49 |
-
border: none;
|
| 50 |
-
border-radius: 25px;
|
| 51 |
-
background: #4CAF50;
|
| 52 |
-
color: white;
|
| 53 |
-
cursor: pointer;
|
| 54 |
-
transition: all 0.3s ease;
|
| 55 |
-
}
|
| 56 |
-
.record-button:hover {
|
| 57 |
-
background: #45a049;
|
| 58 |
-
}
|
| 59 |
-
.record-button.recording {
|
| 60 |
-
background: #f44336;
|
| 61 |
-
animation: pulse 1.5s infinite;
|
| 62 |
-
}
|
| 63 |
-
.recording-status {
|
| 64 |
-
color: #666;
|
| 65 |
-
font-size: 14px;
|
| 66 |
-
}
|
| 67 |
-
@keyframes pulse {
|
| 68 |
-
0% { transform: scale(1); }
|
| 69 |
-
50% { transform: scale(1.05); }
|
| 70 |
-
100% { transform: scale(1); }
|
| 71 |
-
}
|
| 72 |
-
</style>
|
| 73 |
<script>
|
| 74 |
let mediaRecorder;
|
| 75 |
let audioChunks = [];
|
|
@@ -95,12 +126,10 @@ def create_audio_recorder():
|
|
| 95 |
audioPlayback.src = audioUrl;
|
| 96 |
audioPlayback.style.display = 'block';
|
| 97 |
|
| 98 |
-
// Convert blob to base64
|
| 99 |
const reader = new FileReader();
|
| 100 |
reader.readAsDataURL(audioBlob);
|
| 101 |
reader.onloadend = () => {
|
| 102 |
const base64Audio = reader.result;
|
| 103 |
-
// Send to Gradio
|
| 104 |
window.parent.postMessage({
|
| 105 |
type: 'audio_data',
|
| 106 |
data: base64Audio
|
|
@@ -335,12 +364,12 @@ with gr.Blocks(title="Document Q&A System") as demo:
|
|
| 335 |
audio_input = gr.Audio(label="Voice Input", type="filepath", visible=False)
|
| 336 |
audio_input.change(process_audio, [audio_input, chatbot], [audio_input, chatbot])
|
| 337 |
|
| 338 |
-
#
|
| 339 |
demo.load(
|
| 340 |
fn=None,
|
| 341 |
inputs=None,
|
| 342 |
outputs=None,
|
| 343 |
-
|
| 344 |
function() {
|
| 345 |
window.addEventListener('message', function(event) {
|
| 346 |
if (event.data.type === 'audio_data') {
|
|
|
|
| 20 |
# Initialize OpenAI client with older API syntax
|
| 21 |
openai.api_key = api_key
|
| 22 |
|
| 23 |
+
# Custom CSS for better styling
|
| 24 |
+
custom_css = """
|
| 25 |
+
.gradio-container {
|
| 26 |
+
max-width: 1200px !important;
|
| 27 |
+
margin: 0 auto !important;
|
| 28 |
+
}
|
| 29 |
+
.container {
|
| 30 |
+
max-width: 1200px;
|
| 31 |
+
margin: 0 auto;
|
| 32 |
+
padding: 20px;
|
| 33 |
+
}
|
| 34 |
+
.chat-container {
|
| 35 |
+
border: 1px solid #e0e0e0;
|
| 36 |
+
border-radius: 10px;
|
| 37 |
+
padding: 20px;
|
| 38 |
+
background: white;
|
| 39 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 40 |
+
}
|
| 41 |
+
.file-upload {
|
| 42 |
+
border: 2px dashed #e0e0e0;
|
| 43 |
+
border-radius: 10px;
|
| 44 |
+
padding: 20px;
|
| 45 |
+
text-align: center;
|
| 46 |
+
background: #f8f9fa;
|
| 47 |
+
transition: all 0.3s ease;
|
| 48 |
+
}
|
| 49 |
+
.file-upload:hover {
|
| 50 |
+
border-color: #4CAF50;
|
| 51 |
+
background: #f0f7f0;
|
| 52 |
+
}
|
| 53 |
+
.audio-recorder {
|
| 54 |
+
background: #f8f9fa;
|
| 55 |
+
border-radius: 10px;
|
| 56 |
+
padding: 20px;
|
| 57 |
+
text-align: center;
|
| 58 |
+
}
|
| 59 |
+
.record-button {
|
| 60 |
+
background: #4CAF50;
|
| 61 |
+
color: white;
|
| 62 |
+
border: none;
|
| 63 |
+
padding: 12px 24px;
|
| 64 |
+
border-radius: 25px;
|
| 65 |
+
cursor: pointer;
|
| 66 |
+
transition: all 0.3s ease;
|
| 67 |
+
font-size: 16px;
|
| 68 |
+
display: flex;
|
| 69 |
+
align-items: center;
|
| 70 |
+
gap: 8px;
|
| 71 |
+
margin: 0 auto;
|
| 72 |
+
}
|
| 73 |
+
.record-button:hover {
|
| 74 |
+
background: #45a049;
|
| 75 |
+
transform: scale(1.05);
|
| 76 |
+
}
|
| 77 |
+
.record-button.recording {
|
| 78 |
+
background: #f44336;
|
| 79 |
+
animation: pulse 1.5s infinite;
|
| 80 |
+
}
|
| 81 |
+
.status-text {
|
| 82 |
+
color: #666;
|
| 83 |
+
font-size: 14px;
|
| 84 |
+
margin-top: 10px;
|
| 85 |
+
}
|
| 86 |
+
@keyframes pulse {
|
| 87 |
+
0% { transform: scale(1); }
|
| 88 |
+
50% { transform: scale(1.05); }
|
| 89 |
+
100% { transform: scale(1); }
|
| 90 |
+
}
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
# Custom audio recorder component with improved styling
|
| 94 |
def create_audio_recorder():
|
| 95 |
return gr.HTML("""
|
| 96 |
<div class="audio-recorder">
|
|
|
|
| 98 |
<span class="record-icon">🎤</span>
|
| 99 |
<span class="record-text">Start Recording</span>
|
| 100 |
</button>
|
| 101 |
+
<div id="recordingStatus" class="status-text"></div>
|
| 102 |
+
<audio id="audioPlayback" controls style="display: none; margin-top: 10px;"></audio>
|
| 103 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
<script>
|
| 105 |
let mediaRecorder;
|
| 106 |
let audioChunks = [];
|
|
|
|
| 126 |
audioPlayback.src = audioUrl;
|
| 127 |
audioPlayback.style.display = 'block';
|
| 128 |
|
|
|
|
| 129 |
const reader = new FileReader();
|
| 130 |
reader.readAsDataURL(audioBlob);
|
| 131 |
reader.onloadend = () => {
|
| 132 |
const base64Audio = reader.result;
|
|
|
|
| 133 |
window.parent.postMessage({
|
| 134 |
type: 'audio_data',
|
| 135 |
data: base64Audio
|
|
|
|
| 364 |
audio_input = gr.Audio(label="Voice Input", type="filepath", visible=False)
|
| 365 |
audio_input.change(process_audio, [audio_input, chatbot], [audio_input, chatbot])
|
| 366 |
|
| 367 |
+
# Add JavaScript for audio handling
|
| 368 |
demo.load(
|
| 369 |
fn=None,
|
| 370 |
inputs=None,
|
| 371 |
outputs=None,
|
| 372 |
+
js="""
|
| 373 |
function() {
|
| 374 |
window.addEventListener('message', function(event) {
|
| 375 |
if (event.data.type === 'audio_data') {
|