import gradio as gr import subprocess import os def find_espeak_ng_data_folder(): espeak_path = "espeak-ng" # Change this to the full path if needed try: data_folder = subprocess.check_output([espeak_path, '--version']).decode().split('Data at: ')[1].strip() return data_folder except Exception as e: print(f"Error: {str(e)}") return None def phonemize(text): """Convert text to phonemes using espeak-ng""" espeak_path = "espeak-ng" script_path = os.path.dirname(os.path.abspath(__file__)) input_file_path = os.path.join(script_path, "input_text.txt") # Write the input text to a file with open(input_file_path, 'w', encoding='utf-8') as file: file.write(text) # Phonemize the text using espeak-ng phonemes_command = [espeak_path, "-v", 'fa', "-x", "-q", "-f", input_file_path] phonemes_result = subprocess.run(phonemes_command, capture_output=True, text=True) phonemized_text = phonemes_result.stdout.strip() return { "text": text, "phonemes": phonemized_text } # Create Gradio interface def process_text(text): result = phonemize(text) return result["phonemes"] # Create the interface with both GUI and API iface = gr.Interface( theme='Ocean', fn=process_text, inputs=gr.Textbox(label="Input Text", placeholder="Enter text to convert to phonemes"), outputs=gr.Textbox(label="Phonemes"), title="Text to Phoneme Converter", description="Convert Persian text to phonemes using espeak-ng", examples=[ ["سلام"], ["خوش آمدید"], ], allow_flagging="never" ) # Launch the app if __name__ == "__main__": iface.launch(server_name="0.0.0.0", server_port=7860, share=True)