File size: 1,349 Bytes
1c2fdca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st 
from gtts import gTTS
from pydub import AudioSegment
import tempfile
import os
import speech_recognition as sr



def text_to_speech(text, lang='en', c=0):
    tts = gTTS(text=text, lang=lang)
    audio_file = f"output{c}.mp3"
    tts.save(audio_file)
    return audio_file

def transcribe(uploaded_file):
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
        file_path = tmpfile.name
        tmpfile.write(uploaded_file.read())

    audio = AudioSegment.from_file(file_path)
    audio = audio.set_frame_rate(16000).set_channels(1)
    audio.export(file_path, format="wav")

    recognizer = sr.Recognizer()
    with sr.AudioFile(file_path) as source:
        trans=True
        with st.spinner("Transcribing... Please wait!", show_time=True):
            if trans:
                audio_data = recognizer.record(source)
                try:
                    text = recognizer.recognize_google(audio_data)
                    trans=False
                    return text
                except sr.UnknownValueError:
                    st.error("❌ Could not understand the audio.")
                except sr.RequestError:
                    st.error("❌ API error. Check internet connection.")
            
    os.remove(file_path)
    return ""