joseph / app.py
joseph7251's picture
Update app.py
79c0e33 verified
raw
history blame
894 Bytes
import gradio as gr
import speech_recognition as sr
def speech_to_text(audio):
recognizer = sr.Recognizer()
with sr.AudioFile(audio.name) as source:
audio_data = recognizer.record(source) # 讀取音頻文件
try:
# 使用 Google 的語音識別服務進行轉換
text = recognizer.recognize_google(audio_data, language="zh-TW") # 中文
return text
except sr.UnknownValueError:
return "語音無法識別"
except sr.RequestError:
return "無法連接到服務"
# 建立 Gradio 介面
iface = gr.Interface(fn=speech_to_text,
inputs=gr.Audio(source="upload", type="file"),
outputs="text",
title="語音轉文字系統",
description="上傳音頻文件以將語音轉換為文字。")
iface.launch()