File size: 2,431 Bytes
af54c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
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()