balaramas's picture
Upload 16 files
af54c89
"""
Script to translate given single english audio file to corresponding hindi text
Usage : python s2t_en2hi.py <audio_file_path> <averaged_checkpoints_file_path>
"""
import gradio as gr
import sys
import os
import subprocess
from huggingface_hub import snapshot_download
def install_fairseq():
try:
# Run pip install command to install fairseq
subprocess.check_call(["pip", "install", "fairseq"])
subprocess.check_call(["pip", "install", "sentencepiece"])
return "fairseq successfully installed!"
except subprocess.CalledProcessError as e:
return f"An error occurred while installing fairseq: {str(e)}"
huggingface_model_dir = snapshot_download(repo_id="balaramas/en_hi_s2t")
print(huggingface_model_dir)
os.system("cd fairseq_mustc_single_inference")
def run_my_code(input_text):
# TODO better argument handling
hi_wav = input_text
en2hi_model_checkpoint = "st_avg_last_10_checkpoints.pt"
os.system(f"cp {hi_wav} ./MUSTC_ROOT/en-hi/data/tst-COMMON/wav/test.wav")
print("------Starting data prepration...")
subprocess.run(["python", "prep_mustc_data_hindi_single.py", "--data-root", "MUSTC_ROOT/", "--task", "st", "--vocab-type", "unigram", "--vocab-size", "8000"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("------Performing translation...")
translation_result = subprocess.run(["fairseq-generate", "./MUSTC_ROOT/en-hi/", "--config-yaml", "config_st.yaml", "--gen-subset", "tst-COMMON_st", "--task", "speech_to_text", "--path", en2hi_model_checkpoint, "--max-tokens", "50000", "--beam", "5", "--scoring", "sacrebleu"], capture_output=True, text=True)
translation_result_text = translation_result.stdout
lines = translation_result_text.split("\n")
output_text=""
print("\n\n------Translation results are:")
for i in lines:
if (i.startswith("D-0")):
print(i.split("\t")[2])
output_text=i.split("\t")[2]
break
os.system("rm ./MUSTC_ROOT/en-hi/data/tst-COMMON/wav/test.wav")
return output_text
install_fairseq()
# Define the input and output interfaces for Gradio
input_textbox = gr.inputs.Textbox(label="Input Text")
output_textbox = gr.outputs.Textbox(label="Output Text")
# Create a Gradio interface
iface = gr.Interface(fn=run_my_code, inputs=input_textbox, outputs=output_textbox, title="My Code Runner")
# Launch the interface
iface.launch()