tbdavid2019 commited on
Commit
15f7e66
·
1 Parent(s): 6503cb3

更新 README.md,新增專案說明、快速開始、使用方式及注意事項;改善 app.py 中的錯誤處理;新增 .gitignore 檔案以排除不必要的檔案

Browse files
Files changed (4) hide show
  1. .gitignore +22 -0
  2. README.md +23 -1
  3. __pycache__/app.cpython-313.pyc +0 -0
  4. app.py +15 -5
.gitignore ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / cache
2
+ __pycache__/
3
+ **/__pycache__/
4
+ *.py[cod]
5
+ *.pyc
6
+
7
+ # Virtual environments
8
+ .venv/
9
+ venv/
10
+ env/
11
+
12
+ # OS / editor
13
+ .DS_Store
14
+ *.swp
15
+ *.swo
16
+
17
+ # Python tooling
18
+ .pytest_cache/
19
+ .mypy_cache/
20
+
21
+ # Local app artifacts
22
+ data/history.json
README.md CHANGED
@@ -1,4 +1,3 @@
1
- ---
2
  title: Taiwanese Tts
3
  emoji: 👁
4
  colorFrom: gray
@@ -11,3 +10,26 @@ short_description: 台語TTS
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  title: Taiwanese Tts
2
  emoji: 👁
3
  colorFrom: gray
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+ ## 專案說明
15
+ 這是一個台語文字轉語音介面,使用 Gradio 建立。介面會呼叫外部 TTS API(`https://learn-language.tokyo/taigiTTS/taigi-text-to-speech`),將使用者輸入的台語文字轉成語音並播放,同時保留最近 50 筆歷史紀錄(儲存在 `data/history.json`)。在 Hugging Face Spaces 可設定持久儲存以保存歷史。
16
+
17
+ ## 快速開始
18
+ 1) 安裝套件:
19
+ ```
20
+ pip install -r requirements.txt
21
+ ```
22
+ 2) 啟動介面:
23
+ ```
24
+ python3 app.py
25
+ ```
26
+ 開啟終端顯示的網址(預設 7860)操作。
27
+
28
+ ## 使用方式
29
+ - 輸入台語文字並選擇模型(目前預設 `model6`),點擊「產生語音」。
30
+ - 轉換後可直接播放音檔;同時會顯示 API 回傳的狀態訊息、白話字(Tailo)與 IPA。
31
+ - 下方歷史區域可重播任一紀錄或重新載入最新紀錄。最多保留 50 筆,超出會自動覆蓋最舊項目。
32
+
33
+ ## 注意事項
34
+ - 介面直接呼叫外部 API,若 API 502/429 等錯誤會在 UI 顯示友善訊息,不會造成程式崩潰。
35
+ - `data/history.json` 為本地紀錄檔,可視需要刪除或加入持久化設定。
__pycache__/app.cpython-313.pyc CHANGED
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
 
app.py CHANGED
@@ -66,9 +66,19 @@ def _history_table(entries: List[Dict]) -> List[Dict]:
66
 
67
  def fetch_tts(text: str, model: str) -> Tuple[str, Dict]:
68
  payload = {"text": text, "model": model}
69
- response = requests.post(API_URL, json=payload, headers=API_HEADERS, timeout=60)
70
- response.raise_for_status()
71
- data = response.json()
 
 
 
 
 
 
 
 
 
 
72
  audio_url = data.get("converted_audio_url") or data.get("audio_url")
73
  if not audio_url:
74
  raise gr.Error("TTS API 回傳內容缺少音檔網址 (audio_url)。")
@@ -153,7 +163,7 @@ css = """
153
  }
154
  """
155
 
156
- with gr.Blocks(title="台語 TTS", css=css) as demo:
157
  gr.Markdown(
158
  textwrap.dedent(
159
  """
@@ -235,4 +245,4 @@ with gr.Blocks(title="台語 TTS", css=css) as demo:
235
  )
236
 
237
  if __name__ == "__main__":
238
- demo.launch()
 
66
 
67
  def fetch_tts(text: str, model: str) -> Tuple[str, Dict]:
68
  payload = {"text": text, "model": model}
69
+ try:
70
+ response = requests.post(API_URL, json=payload, headers=API_HEADERS, timeout=60)
71
+ response.raise_for_status()
72
+ data = response.json()
73
+ except requests.HTTPError as exc:
74
+ status = exc.response.status_code if exc.response is not None else "N/A"
75
+ detail = exc.response.text[:300] if exc.response is not None else str(exc)
76
+ raise gr.Error(f"TTS API 呼叫失敗 (HTTP {status}): {detail}")
77
+ except requests.RequestException as exc:
78
+ raise gr.Error(f"TTS API 連線失敗:{exc}")
79
+ except ValueError:
80
+ raise gr.Error("TTS API 回傳非 JSON 內容。")
81
+
82
  audio_url = data.get("converted_audio_url") or data.get("audio_url")
83
  if not audio_url:
84
  raise gr.Error("TTS API 回傳內容缺少音檔網址 (audio_url)。")
 
163
  }
164
  """
165
 
166
+ with gr.Blocks(title="台語 TTS") as demo:
167
  gr.Markdown(
168
  textwrap.dedent(
169
  """
 
245
  )
246
 
247
  if __name__ == "__main__":
248
+ demo.launch(css=css)