Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
|
| 4 |
+
def recognize_speech(audio):
|
| 5 |
+
recognizer = sr.Recognizer()
|
| 6 |
+
with sr.AudioFile(audio) as source:
|
| 7 |
+
audio_data = recognizer.record(source)
|
| 8 |
+
try:
|
| 9 |
+
text = recognizer.recognize_google(audio_data, language='zh-TW') # 使用 Google 語音識別
|
| 10 |
+
return text
|
| 11 |
+
except sr.UnknownValueError:
|
| 12 |
+
return "無法識別語音"
|
| 13 |
+
except sr.RequestError:
|
| 14 |
+
return "無法連接到語音識別服務"
|
| 15 |
+
|
| 16 |
+
iface = gr.Interface(fn=recognize_speech, inputs=gr.Audio(source="microphone", type="filepath"), outputs="text", title="語音轉文字應用", description="請說話,然後按下停止按鈕進行文字轉換。")
|
| 17 |
+
iface.launch()
|