Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load mô hình nhận diện giọng nói (ASR)
|
5 |
+
asr = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
6 |
+
|
7 |
+
# Load mô 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 có 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()
|