File size: 2,117 Bytes
6a3a59b 3f24c8c ffa8116 3f24c8c 6a3a59b ffa8116 3f24c8c ffa8116 6a3a59b ffa8116 6a3a59b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import gradio as gr
# from transformers import pipeline
# # 1. Whisper ASR
# asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
# # 2. Emotion classifier (Vietnamese)
# emo_clf = pipeline("text-classification", model="bkai-foundation-models/vietnamese-emotion", top_k=None)
from transformers import pipeline
# ASR bằng Whisper
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
# Emotion classification (Vietnamese text)
emo_clf = pipeline("text-classification", model="visolex/bartpho-emotion", top_k=None)
# 3. Function to process audio
def predict_emotion(audio_file):
# Step 1: Speech to Text
transcript = asr(audio_file)["text"]
# Step 2: Emotion classification
emotions = emo_clf(transcript)[0]
emotions_sorted = sorted(emotions, key=lambda x: x["score"], reverse=True)
# Step 3: Bot reply based on top emotion
top_emotion = emotions_sorted[0]["label"]
bot_reply = {
"vui": "Mình cảm nhận được sự vui vẻ của bạn! ❤️",
"buồn": "Nghe như bạn đang buồn. Mình sẵn sàng lắng nghe bạn chia sẻ. 💙",
"giận": "Có vẻ bạn đang tức giận. Hít thở sâu và chúng ta cùng nói chuyện nhé. 😌",
"sợ": "Mình cảm nhận được sự lo lắng từ bạn. Mọi chuyện sẽ ổn thôi. 🤗",
"trung tính": "Mình đang nghe bạn. Hãy nói thêm cho mình biết nhé. 🙂"
}.get(top_emotion.lower(), "Mình đã nghe bạn rồi!")
return transcript, emotions_sorted, bot_reply
# 4. Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## 🎤 Chatbot dự đoán cảm xúc qua lời nói")
audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="Ghi âm hoặc tải file")
transcript_out = gr.Textbox(label="Transcript")
emotion_out = gr.JSON(label="Dự đoán cảm xúc")
bot_reply_out = gr.Textbox(label="Phản hồi Chatbot")
audio_input.change(fn=predict_emotion, inputs=audio_input, outputs=[transcript_out, emotion_out, bot_reply_out])
demo.launch()
|