import gradio as gr import os import subprocess from urllib.request import urlretrieve import traceback # --- Create a directory for the model files --- os.makedirs("tts_model", exist_ok=True) # --- Model and Config URLs --- MODEL_URL = "https://huggingface.co/SYSPIN/vits_Chhattisgarhi_Female/resolve/main/best_model.pth?download=true" CONFIG_URL = "https://huggingface.co/SYSPIN/vits_Chhattisgarhi_Female/resolve/main/config.json?download=true" # --- Define local paths for the model and config --- model_path = os.path.join("tts_model", "best_model.pth") config_path = os.path.join("tts_model", "config.json") # --- Download Model and Config if they don't exist --- def download_file(url, destination): if not os.path.exists(destination): print(f"Downloading {os.path.basename(destination)}...") try: urlretrieve(url, destination) print("Download complete.") except Exception as e: print(f"FATAL: Error downloading {url}: {e}") raise try: download_file(MODEL_URL, model_path) download_file(CONFIG_URL, config_path) except Exception as e: # If downloads fail, we cannot proceed. print("Application cannot start because model files could not be downloaded.") # This will cause the app to crash, which is appropriate here. raise def chhattisgarhi_tts_cli(text): """ Converts Chhattisgarhi text to speech using the TTS command-line interface. Returns a tuple: (status_message, audio_filepath) """ if not text.strip(): return ("Warning: Please enter some text to convert.", None) try: output_wav_path = "output.wav" # Construct the command-line instruction command = [ "tts", "--text", text, "--model_path", model_path, "--config_path", config_path, "--out_path", output_wav_path ] print(f"Running command: {' '.join(command)}") # Execute the command. Capture stderr for detailed error reporting. result = subprocess.run( command, check=True, # Raise an exception if the command fails capture_output=True, text=True ) print(f"TTS command successful. Output saved to {output_wav_path}") return ("Success!", output_wav_path) except subprocess.CalledProcessError as e: # This block is executed if the 'tts' command returns a non-zero exit code (i.e., an error) error_message = f"TTS command failed." # The actual error from the TTS tool is in stderr stderr_output = e.stderr.strip() print(f"ERROR: {error_message}") print(f"STDERR from TTS tool: {stderr_output}") # Return a user-friendly error message to the Gradio interface return (f"Error during audio generation. Details: {stderr_output}", None) except Exception as e: # Catch any other unexpected errors (e.g., file not found if downloads failed silently) error_message = f"An unexpected error occurred: {str(e)}" print(error_message) traceback.print_exc() return (error_message, None) # --- Gradio Interface --- title = "Chhattisgarhi TTS Conversion" description = """ ### Chhattisgarhi Text-to-Speech Enter text in Chhattisgarhi to generate audio. This app uses the `tts` command-line tool to run a VITS model provided by [SYSPIN on Hugging Face](https://huggingface.co/SYSPIN/vits_Chhattisgarhi_Female). """ examples = [ ["राजस्थान के नामी ब्यंजन चूरमालाड़ू गुड़ के पाग म गहूँ के दरदरहा पिसान के लाड़ू म तिली अउ नरियल के सुवाद म सजथे"], ["दुग्ध क्रान्ति भारत के योजना हे जेखर ले भारत म दूध के कमी ला दुरिहा करे जा सकथे एला श्वेत क्रांति घलोक कहिथे"], ["जम्मू कश्मीर म पर्यटन उद्योग ला बढ़ावा देना उहाँ के अर्थबेवस्था ला सुचारू रूप ले चलाय बर जरुरी हे"], ["फोरेन्सिक विज्ञान ल कानूनी प्रश्न के उत्तर दिए बर अउ अपराध ल सिद्ध करे बर बउरे जाथे"] ] iface = gr.Interface( fn=chhattisgarhi_tts_cli, inputs=gr.Textbox(lines=3, label="Enter Chhattisgarhi Text", placeholder="यहाँ छत्तीसगढ़ी पाठ लिखें..."), outputs=[ gr.Textbox(label="Status", interactive=False), gr.Audio(type="filepath", label="Generated Speech") ], title=title, description=description, examples=examples, allow_flagging="never", cache_examples=True ) if __name__ == "__main__": iface.launch()