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)