joseph7251 commited on
Commit
79c0e33
·
verified ·
1 Parent(s): db88ac3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -1,26 +1,25 @@
1
  import gradio as gr
 
2
 
3
- def draw_number(max_number):
4
- """
5
- 抽取一個隨機號碼
 
 
 
 
 
 
 
 
 
 
6
 
7
- Args:
8
- max_number: 班級座號的最大值
 
 
 
 
9
 
10
- Returns:
11
- 抽出的隨機號碼
12
- """
13
- import random
14
- return random.randint(1, max_number)
15
-
16
- # 建立使用者介面
17
- demo = gr.Interface(
18
- fn=draw_number,
19
- inputs="number",
20
- outputs="text",
21
- title="隨機抽號",
22
- description="輸入班級座號的最大值,點擊抽籤"
23
- )
24
-
25
- # 啟動伺服器
26
- demo.launch()
 
1
  import gradio as gr
2
+ import speech_recognition as sr
3
 
4
+ def speech_to_text(audio):
5
+ recognizer = sr.Recognizer()
6
+
7
+ with sr.AudioFile(audio.name) as source:
8
+ audio_data = recognizer.record(source) # 讀取音頻文件
9
+ try:
10
+ # 使用 Google 的語音識別服務進行轉換
11
+ text = recognizer.recognize_google(audio_data, language="zh-TW") # 中文
12
+ return text
13
+ except sr.UnknownValueError:
14
+ return "語音無法識別"
15
+ except sr.RequestError:
16
+ return "無法連接到服務"
17
 
18
+ # 建立 Gradio 介面
19
+ iface = gr.Interface(fn=speech_to_text,
20
+ inputs=gr.Audio(source="upload", type="file"),
21
+ outputs="text",
22
+ title="語音轉文字系統",
23
+ description="上傳音頻文件以將語音轉換為文字。")
24
 
25
+ iface.launch()