Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import soundfile as sf # Import soundfile for audio file handling
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
|
| 6 |
+
|
| 7 |
+
# Choose a suitable Kannada speech-to-text model from Hugging Face
|
| 8 |
+
model_name = "vasista22/whisper-kannada-tiny" # Replace with your preferred model
|
| 9 |
+
|
| 10 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name)
|
| 11 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def transcribe_kannada(audio_data):
|
| 18 |
+
"""
|
| 19 |
+
Transcribes recorded Kannada audio using the specified Hugging Face model.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
audio_data: A NumPy array representing the recorded audio data.
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
The transcribed text in Kannada.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
sampling_rate = 16000 # Assuming common speech sampling rate (adjust if needed)
|
| 29 |
+
audio_input = processor(audio_data, sampling_rate=sampling_rate, return_tensors="pt")
|
| 30 |
+
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
logits = model(**audio_input).logits
|
| 33 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 34 |
+
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
| 35 |
+
|
| 36 |
+
return transcription
|
| 37 |
+
|
| 38 |
+
def record_and_transcribe(audio):
|
| 39 |
+
"""
|
| 40 |
+
Records audio from the microphone, processes each channel independently (if applicable),
|
| 41 |
+
converts them to speech-to-text, and plays reversed audio.
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
audio: A tuple containing recorded audio information (multiple audio channels).
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
A list of transcriptions (one for each channel), or a tuple with transcriptions and reversed audio.
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
transcriptions = []
|
| 51 |
+
for channel in audio:
|
| 52 |
+
# Process each audio channel (replace with your actual conversion logic)
|
| 53 |
+
audio_data = channel # Assuming no processing needed for individual channels
|
| 54 |
+
transcription = transcribe_kannada(audio_data)
|
| 55 |
+
transcriptions.append(transcription)
|
| 56 |
+
|
| 57 |
+
# ... (handle reversed audio if needed)
|
| 58 |
+
|
| 59 |
+
return transcriptions # Or a tuple with transcriptions and reversed audio
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# input_audio = gr.Audio(
|
| 64 |
+
# sources=["microphone"],
|
| 65 |
+
# type="numpy", # Specify audio format as NumPy array
|
| 66 |
+
# normalization=" [-1, 1]", # Normalize audio data to -1 to 1 range for model compatibility
|
| 67 |
+
# label="Record Kannada Audio",
|
| 68 |
+
# )
|
| 69 |
+
|
| 70 |
+
input_audio = gr.Audio(
|
| 71 |
+
sources=["microphone"],
|
| 72 |
+
type="numpy", # Specify audio format as NumPy array
|
| 73 |
+
label="Record Kannada Audio",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
text_output = gr.Textbox(label="Transcription (ಕನ್ನಡ)")
|
| 77 |
+
audio_output = gr.Audio(label="Reversed Audio (Optional)", type="numpy")
|
| 78 |
+
|
| 79 |
+
demo = gr.Interface(
|
| 80 |
+
fn=record_and_transcribe,
|
| 81 |
+
inputs=input_audio,
|
| 82 |
+
outputs=[text_output, audio_output],
|
| 83 |
+
description="Kannada Speech-to-Text and Reverse Audio",
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch(share=True)
|