Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
|
| 5 |
+
def transcribe_audio(audio_file_path, input_language):
|
| 6 |
+
# Add silence padding
|
| 7 |
+
audio = AudioSegment.from_file(audio_file_path, format="wav")
|
| 8 |
+
two_sec_silence = AudioSegment.silent(duration=2500)
|
| 9 |
+
audio = two_sec_silence + audio + two_sec_silence
|
| 10 |
+
audio.export("stt_audio.wav", format="wav")
|
| 11 |
+
file_path2 = "stt_audio.wav"
|
| 12 |
+
|
| 13 |
+
recognizer = sr.Recognizer()
|
| 14 |
+
try:
|
| 15 |
+
with sr.AudioFile(file_path2) as source:
|
| 16 |
+
recognizer.adjust_for_ambient_noise(source)
|
| 17 |
+
audio_data = recognizer.record(source)
|
| 18 |
+
|
| 19 |
+
# Google API call
|
| 20 |
+
text = recognizer.recognize_google(audio_data, language=input_language)
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
except sr.UnknownValueError:
|
| 24 |
+
return "Could not understand the audio"
|
| 25 |
+
except sr.RequestError as e:
|
| 26 |
+
return f"Could not request results; {e}"
|
| 27 |
+
|
| 28 |
+
# Gradio interface
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=transcribe_audio,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Audio(source="upload", type="filepath", label="Upload Audio (.wav)"),
|
| 33 |
+
gr.Textbox(label="Language code (e.g., en-US, fa-IR)")
|
| 34 |
+
],
|
| 35 |
+
outputs="text",
|
| 36 |
+
title="Google Speech-to-Text",
|
| 37 |
+
description="Upload a WAV file and get transcription using Google STT API."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
iface.launch()
|