Spaces:
Runtime error
Runtime error
File size: 3,023 Bytes
7eb17c5 074e545 961947b 7eb17c5 074e545 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 65 66 67 68 69 70 71 72 73 74 75 76 77 | import gradio as gr
import numpy as np
from scipy.io.wavfile import write
import os
import requests # Import requests library
# from backend.ai_processing.process_audio import process_audio # Import the process_audio function
# Define constants
SAMPLE_RATE = 44100 # Sample rate in Hz
AUDIO_DIR = 'audio' # Directory to save audio files
# Ensure the audio directory exists
if not os.path.exists(AUDIO_DIR):
os.makedirs(AUDIO_DIR)
def save_and_transcribe_audio(recording, filename='recording.wav'):
"""
Saves the recorded audio to a file and sends it to an external API for transcription and language detection.
- Extracts sample rate and audio data from the recording.
- Scales the audio data to 16-bit integers.
- Saves the audio data as a WAV file.
- Sends the saved audio file to an external API for transcription and language detection.
- Returns the response from the API.
"""
# Ensure recording is not None
try:
# Attempt to extract sample rate and audio data
sample_rate, audio_data = recording
except TypeError:
raise ValueError("Invalid recording: Expected a tuple with sample rate and audio data, got None.")
except ValueError:
raise ValueError("Invalid recording: Expected a tuple with sample rate and audio data.")
# Ensure audio_data is a NumPy array
audio_data = np.array(audio_data)
# Scale the audio data to 16-bit integers
scaled = np.int16(audio_data/np.max(np.abs(audio_data)) * 32767)
# Save as WAV file
filepath = os.path.join(AUDIO_DIR, filename)
write(filepath, sample_rate, scaled)
# Send the audio file to the external API
with open(filepath, 'rb') as f:
files = {'file': (filename, f)}
response = requests.post('https://detect-language-tuwk5nvu4a-uc.a.run.app', files=files)
if response.status_code == 200:
# Process successful response
return response.text
else:
# Handle error response
raise ValueError(f"API call failed with status code {response.status_code}: {response.text}")
# Create Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# AI Language Detector")
gr.Markdown("## Description\n - This Demo works by Transcribing audio with OpenAI's Whisper model and then use GPT model to detect the language from the transcription text.")
gr.Markdown("## Instructions\n1. Click the 'Record Audio' button to start recording.\n2. Speak a sentence or two, the more the better.\n3. Click the 'Detect Language' button")
with gr.Row():
record_btn = gr.Audio(sources=['microphone'], type='numpy', label='Record Audio')
transcribe_btn = gr.Button('Detect Language')
# Output message
output = gr.Textbox(label='Detected Language')
# Define button action
transcribe_btn.click(fn=save_and_transcribe_audio, inputs=[record_btn], outputs=[output])
# Launch the Gradio app
if __name__ == "__main__":
demo.launch(share=True) |