vankienemk commited on
Commit
901f41e
·
verified ·
1 Parent(s): 42c8dff

Update app.py

Browse files

Clean app using pipeline only

Files changed (1) hide show
  1. app.py +13 -35
app.py CHANGED
@@ -1,45 +1,23 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load hình nhận diện giọng nói (ASR)
5
  asr = pipeline("automatic-speech-recognition", model="openai/whisper-base")
6
 
7
- # Load hình ngôn ngữ (tùy chọn)
8
- llm = pipeline("text-generation", model="mrm8488/t5-base-finetuned-question-generation-ap", max_new_tokens=100)
9
-
10
- # Xử lý âm thanh: Nhận dạng giọng nói
11
  def transcribe(audio_file):
12
  if audio_file is None:
13
- return "Không có file âm thanh."
14
  result = asr(audio_file)
15
- text = result["text"]
16
- return text
17
-
18
- # Gửi văn bản vào LLM (nếu muốn)
19
- def ask_llm(text):
20
- if not text:
21
- return "Chưa văn bản đầu vào"
22
- response = llm(text)
23
- return response[0]["generated_text"]
24
-
25
- # Gradio UI
26
- with gr.Blocks() as demo:
27
- gr.Markdown("# Voice Assistant Demo")
28
- gr.Markdown("### Gửi file âm thanh để nhận diện giọng nói bằng Whisper")
29
-
30
- with gr.Row():
31
- audio_input = gr.Audio(label="Chọn file âm thanh", type="filepath")
32
- btn_transcribe = gr.Button("Nhận diện giọng nói")
33
-
34
- transcript_output = gr.Textbox(label="Kết quả nhận diện")
35
-
36
- btn_transcribe.click(fn=transcribe, inputs=audio_input, outputs=transcript_output)
37
-
38
- gr.Markdown("### Gửi văn bản vào mô hình LLM")
39
- llm_input = gr.Textbox(label="Văn bản đầu vào")
40
- btn_llm = gr.Button("Gửi vào LLM")
41
- llm_output = gr.Textbox(label="Kết quả LLM")
42
-
43
- btn_llm.click(fn=ask_llm, inputs=llm_input, outputs=llm_output)
44
 
45
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Tạo pipeline nhận diện giọng nói
5
  asr = pipeline("automatic-speech-recognition", model="openai/whisper-base")
6
 
7
+ # Hàm xử âm thanh
 
 
 
8
  def transcribe(audio_file):
9
  if audio_file is None:
10
+ return "Chưa có file âm thanh."
11
  result = asr(audio_file)
12
+ return result["text"]
13
+
14
+ # Tạo giao diện
15
+ demo = gr.Interface(
16
+ fn=transcribe,
17
+ inputs=gr.Audio(source="upload", type="filepath", label="Tải lên file âm thanh (.wav, .mp3...)"),
18
+ outputs=gr.Textbox(label="Kết quả chuyển văn bản"),
19
+ title="Nhận diện giọng nói bằng Whisper",
20
+ description="Tải file âm thanh và hệ thống sẽ nhận diện nội dung giọng nói bằng mô hình Whisper của OpenAI."
21
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  demo.launch()