Download scripts/speak.py from chenbhao/codev: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/chenbhao/codev/resolve/afbc7edaae4e6bb0102ba024b49ce8be89d2264c/scripts/speak.py
- Command line
-
hf download hf://chenbhao/codev@afbc7edaae4e6bb0102ba024b49ce8be89d2264c/scripts/speak.py
-
curl -L -o speak.py https://huggingface.co/chenbhao/codev/resolve/afbc7edaae4e6bb0102ba024b49ce8be89d2264c/scripts/speak.py
1.33 kB
| import json, os, sys, tempfile, asyncio | |
| async def speak(text: str, voice: str = "en-US-JennyNeural", output_path: str | None = None) -> dict: | |
| try: | |
| import edge_tts | |
| communicate = edge_tts.Communicate(text, voice) | |
| if output_path: | |
| await communicate.save(output_path) | |
| return {"success": True, "audio_path": output_path} | |
| tmp = tempfile.mktemp(suffix=".mp3") | |
| await communicate.save(tmp) | |
| return {"success": True, "audio_path": tmp} | |
| except ImportError: | |
| pass | |
| return {"success": False, "error": "edge-tts is not installed. Run: pip install edge-tts"} | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print(json.dumps({"success": False, "error": "Usage: speak.py <text> [--voice <name>] [--output <path>]"})) | |
| sys.exit(1) | |
| text = sys.argv[1] | |
| voice = "en-US-JennyNeural" | |
| output_path = None | |
| i = 2 | |
| while i < len(sys.argv): | |
| if sys.argv[i] == "--voice" and i + 1 < len(sys.argv): | |
| voice = sys.argv[i + 1] | |
| i += 2 | |
| elif sys.argv[i] == "--output" and i + 1 < len(sys.argv): | |
| output_path = sys.argv[i + 1] | |
| i += 2 | |
| else: | |
| i += 1 | |
| result = asyncio.run(speak(text, voice, output_path)) | |
| print(json.dumps(result)) | |
| if __name__ == "__main__": | |
| main() |