InterfaceDonna / app.py
jseema's picture
Update app.py
2e0603a verified
Raw History Blame
2.34 kB
import gradio as gr
from transformers import pipeline
import numpy as np
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
def transcribe(stream, new_chunk):
sr, y = new_chunk
y = y.astype(np.float32)
y /= np.max(np.abs(y))
# # Check if the audio is stereo and convert to mono by averaging the channels
# if y.ndim > 1 and y.shape[1] > 1: # More than one channel
# y = np.mean(y, axis=1, keepdims=True) # Convert to mono
# Convert to mono if not already (select the first channel if multi-channel)
if y.ndim > 1 and y.shape[1] > 1:
y = y[:, 0] # Select only the first channel
if stream is not None:
stream = np.concatenate([stream, y])
else:
stream = y
return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"]
# , streaming=True ---- for live stream transcribe
demo = gr.Interface(
transcribe,
["state", gr.Audio(sources=["microphone"], streaming=True)],
["state", "text"],
live=True,
)
demo.launch(inline = False)
#demo.launch()
# import gradio as gr
# from transformers import pipeline
# import numpy as np
# from gtts import gTTS
# import tempfile
# import os
# # Initialize the ASR model
# transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
# def transcribe(stream, new_chunk):
# sr, y = new_chunk
# y = y.astype(np.float32)
# y /= np.max(np.abs(y))
# if stream is not None:
# stream = np.concatenate([stream, y])
# else:
# stream = y
# return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"]
# def text_to_speech(text):
# tts = gTTS(text=text, lang='en')
# # Save the output to a temporary file
# temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
# tts.save(temp_file.name)
# return temp_file.name
# # Create a parallel function for TTS conversion
# def process_text(text):
# audio_file = text_to_speech(text)
# return audio_file, text
# # Define the Gradio Interface
# demo = gr.Interface(
# fn=process_text, # Updated to the new function that includes TTS
# inputs=[gr.Textbox(label="Enter text")],
# outputs=[gr.Audio(label="Audio"), gr.Textbox(label="Transcribed Text")],
# live=True,
# )
# demo.launch(inline=False)