File size: 1,649 Bytes
ee32aa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# This Gradio app allows users to upload an audio file and get a transcription.
import gradio as gr

# Define the function to transcribe the audio file.
def transcribe_audio(audio_file):
    # Check if the file exists and is not too large
    if not os.path.exists(audio_file):
        raise gr.Error(f"Error: File {audio_file} not found")
    
    file_size = os.path.getsize(audio_file) / (1024 * 1024)  # Convert to MB
    print(f"Processing file: {audio_file} ({file_size:.2f} MB)")
    
    try:
        start_time = time.time()
        
        with open(audio_file, "rb") as file:
            # Simulate the transcription process
            transcription = {
                "text": "This is a simulated transcription of the uploaded audio file."
            }
        
        elapsed = time.time() - start_time
        print(f"Transcription completed in {elapsed:.2f} seconds")
        print("\nTranscribed text:")
        print(transcription["text"])
        
        return transcription["text"]
    
    except Exception as e:
        raise gr.Error(f"Error during transcription: {str(e)}")

# Create a Gradio interface that takes an audio file as input and returns the transcription as output.
with gr.Blocks() as demo:
    audio_input = gr.Audio(label="Upload Audio File", type="filepath")
    transcription_output = gr.Textbox(label="Transcribed Text")
    
    transcribe_button = gr.Button("Transcribe")
    
    # Attach the transcribe_audio function to the button click event.
    transcribe_button.click(fn=transcribe_audio, inputs=audio_input, outputs=transcription_output)

# Launch the interface.
demo.launch(show_error=True)