Repository: xorbitsai/inference
Pull request: BUG: fix TTS error bug :No such file or directory
Fix: TTS error bug, No such file or directory

Fixes #3579 .

Problem statement:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/file
### System Info / 系統信息

Windows11
Conda python 3.10.16

### Running Xinference with Docker? / 是否使用 Docker 运行 Xinfernece？

- [ ] docker / docker
- [x] pip install / 通过 pip install 安装
- [ ] installation from source / 从源码安装

### Version info / 版本信息

latest

### The command used to start Xinference / 用以启动 xinference 的命令

xinference-local

### Reproduction / 复现过程

1. Install ChatTTS in xinference.
2. Open ChatTTS's frond end.
3. Input text and click generate button.

### Expected behavior / 期待表现

Windows does not exist tmp in global env, I think shoud use os.getenv() instead of f"/tmp/{uuid.uuid4()}.mp3"

Hints from issue discussion:
Can you paste the error stack?
In xinference\core\media_interface line 667, /tmp/uuid.mp3 is not a file or directory. # @qinxuye 
Oh, I see, this path should be processed with `tempfile` module. Are you interested in contributing?
This is not solved, I will reopen it.

I mean, it's welcome for you to send a PR to solve this problem.
我也遇到这个问题了，请问目前修复了吗？
在项目源代码中xinference\core\media_interface.py第786行

```
# Write to a temp .mp3 file and return its path
# audio_path = f"/tmp/{uuid.uuid4()}.mp3"

```
在linux系统可以，在windows中则失效，改为：

```
#获取当前用户的临时目录
temp_dir = os.environ.get('TEMP')
audio_path = os.path.join(temp_dir, f"{uuid.uuid4()}.mp3")
```
可以正常运行

另外，ASR语音识别报错： Permission denied
解决方案如下：
conda依赖中的tempfile文件
conda\Lib\tempfile.py 函数NamedTemporaryFile delete参数默认值设置为False，可以正常进行语音识别

Commit message: 修复TTS报错问题：No such file or directory

Patch to review:
diff --git a/xinference/core/media_interface.py b/xinference/core/media_interface.py
index c539912065..a96eb8f7f4 100644
--- a/xinference/core/media_interface.py
+++ b/xinference/core/media_interface.py
@@ -784,9 +784,10 @@ def tts_generate(
             )
 
             # Write to a temp .mp3 file and return its path
-            audio_path = f"/tmp/{uuid.uuid4()}.mp3"
-            with open(audio_path, "wb") as f:
-                f.write(response)
+            # Get the current TEMP file and its path according to os
+            temp_dir = os.environ.get('TEMP')
+            audio_path = os.path.join(temp_dir, f"{uuid.uuid4()}.mp3")
+
 
             return audio_path
 


Code review comments:
diff_hunk: @@ -784,9 +784,10 @@ def tts_generate(
             )
 
             # Write to a temp .mp3 file and return its path
-            audio_path = f"/tmp/{uuid.uuid4()}.mp3"
-            with open(audio_path, "wb") as f:
-                f.write(response)
+            # Get the current TEMP file and its path according to os
+            temp_dir = os.environ.get('TEMP')
path: xinference/core/media_interface.py
text: @user1:
And I found that you accidentally deleted the writing audio bytes, the file would be empty.

@author:
Oh I have forgotten one line below it, it is the same as before  to write audio bytes into temp file.

@user1:
Yeah, you can modify the code and just commit, after testing.

diff_hunk: @@ -784,9 +784,10 @@ def tts_generate(
             )
 
             # Write to a temp .mp3 file and return its path
-            audio_path = f"/tmp/{uuid.uuid4()}.mp3"
-            with open(audio_path, "wb") as f:
-                f.write(response)
+            # Get the current TEMP file and its path according to os
path: xinference/core/media_interface.py
text: @user1:
This should be workable only for windows. I suggest to use the below code:

```python
temp_dir = tempfile.gettempdir()
audio_path = os.path.join(temp_dir, f"{uuid.uuid4()}.mp3")
```

Could you please modify it and test this PR?