Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,21 +7,17 @@ import os
|
|
| 7 |
|
| 8 |
# Process audio and transcribe
|
| 9 |
def process_audio(audio_input):
|
| 10 |
-
#
|
| 11 |
-
recognizer = sr.Recognizer()
|
| 12 |
|
| 13 |
-
# Handle Gradio audio input
|
| 14 |
if isinstance(audio_input, tuple): # Recorded audio (sample_rate, numpy_array)
|
| 15 |
-
|
| 16 |
-
# Convert numpy array to WAV file using pydub
|
| 17 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
|
| 18 |
-
AudioSegment(audio_data, sample_rate=
|
| 19 |
audio_file_path = temp_file.name
|
| 20 |
else: # Uploaded audio file
|
| 21 |
audio_file_path = audio_input
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
with sr.AudioFile(audio_file_path) as source:
|
| 25 |
audio = recognizer.record(source)
|
| 26 |
try:
|
| 27 |
transcription = recognizer.recognize_google(audio)
|
|
@@ -58,6 +54,47 @@ def audio_transcriptor(audio_file, audio_record):
|
|
| 58 |
|
| 59 |
return language, transcription, text_file
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Gradio interface
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
gr.Markdown("# Audio Transcriptor")
|
|
@@ -72,6 +109,9 @@ with gr.Blocks() as demo:
|
|
| 72 |
transcription_output = gr.Textbox(label="Transcription")
|
| 73 |
text_file_output = gr.File(label="Download Transcription as Text File")
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
with gr.Row():
|
| 76 |
submit = gr.Button("Transcribe")
|
| 77 |
clear = gr.Button("Clear")
|
|
|
|
| 7 |
|
| 8 |
# Process audio and transcribe
|
| 9 |
def process_audio(audio_input):
|
| 10 |
+
recognizer = sr.Recognizer() # Correct usage of 'sr' as the module alias
|
|
|
|
| 11 |
|
|
|
|
| 12 |
if isinstance(audio_input, tuple): # Recorded audio (sample_rate, numpy_array)
|
| 13 |
+
sample_rate, audio_data = audio_input # Rename 'sr' to 'sample_rate'
|
|
|
|
| 14 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
|
| 15 |
+
AudioSegment(audio_data, sample_rate=sample_rate, frame_rate=sample_rate, channels=1).export(temp_file.name, format="wav")
|
| 16 |
audio_file_path = temp_file.name
|
| 17 |
else: # Uploaded audio file
|
| 18 |
audio_file_path = audio_input
|
| 19 |
|
| 20 |
+
with sr.AudioFile(audio_file_path) as source: # Now 'sr' is correctly defined
|
|
|
|
| 21 |
audio = recognizer.record(source)
|
| 22 |
try:
|
| 23 |
transcription = recognizer.recognize_google(audio)
|
|
|
|
| 54 |
|
| 55 |
return language, transcription, text_file
|
| 56 |
|
| 57 |
+
# Custom HTML for styled transcription display
|
| 58 |
+
transcription_html = """
|
| 59 |
+
<div class="transcription-container" id="transcriptionContainer">
|
| 60 |
+
<h2>Transcription Results</h2>
|
| 61 |
+
<div class="language" id="languageOutput">Detected Language: Waiting...</div>
|
| 62 |
+
<div class="transcription" id="transcriptionOutput">Transcription: Waiting...</div>
|
| 63 |
+
</div>
|
| 64 |
+
|
| 65 |
+
<style>
|
| 66 |
+
.transcription-container {
|
| 67 |
+
max-width: 600px;
|
| 68 |
+
margin: 20px auto;
|
| 69 |
+
padding: 20px;
|
| 70 |
+
background: #16213e;
|
| 71 |
+
border-radius: 10px;
|
| 72 |
+
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
|
| 73 |
+
color: #fff;
|
| 74 |
+
text-align: center;
|
| 75 |
+
}
|
| 76 |
+
.language, .transcription {
|
| 77 |
+
margin: 10px 0;
|
| 78 |
+
padding: 10px;
|
| 79 |
+
background: #0f172a;
|
| 80 |
+
border-radius: 5px;
|
| 81 |
+
}
|
| 82 |
+
</style>
|
| 83 |
+
|
| 84 |
+
<script>
|
| 85 |
+
setInterval(() => {
|
| 86 |
+
const languageOutput = document.querySelector('div[label="Detected Language"] textarea');
|
| 87 |
+
const transcriptionOutput = document.querySelector('div[label="Transcription"] textarea');
|
| 88 |
+
if (languageOutput && languageOutput.value) {
|
| 89 |
+
document.getElementById('languageOutput').textContent = `Detected Language: ${languageOutput.value}`;
|
| 90 |
+
}
|
| 91 |
+
if (transcriptionOutput && transcriptionOutput.value) {
|
| 92 |
+
document.getElementById('transcriptionOutput').textContent = `Transcription: ${transcriptionOutput.value}`;
|
| 93 |
+
}
|
| 94 |
+
}, 1000);
|
| 95 |
+
</script>
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
# Gradio interface
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
gr.Markdown("# Audio Transcriptor")
|
|
|
|
| 109 |
transcription_output = gr.Textbox(label="Transcription")
|
| 110 |
text_file_output = gr.File(label="Download Transcription as Text File")
|
| 111 |
|
| 112 |
+
# Add styled HTML section
|
| 113 |
+
gr.HTML(transcription_html)
|
| 114 |
+
|
| 115 |
with gr.Row():
|
| 116 |
submit = gr.Button("Transcribe")
|
| 117 |
clear = gr.Button("Clear")
|