File size: 1,792 Bytes
88fa5f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
import gradio as gr
import os
import time
from gtts import gTTS
import librosa

# 背景音樂播放
def play_background_music():
    return "background_music.mp3"  # 輕柔音樂檔案路徑

# 回應生成功能
def generate_response(input_text, mode):
    response = ""
    if mode == "文字":
        response = f"我理解您的感受:'{input_text}'。非暴力溝通建議您表達觀察、感受與需求,或許可以說:'當我看到這件事時,我感到不安,因為我需要理解與支持。'"
    elif mode == "語音":
        response = f"基於您的語音內容,我們分析出溝通時可能的誤解。建議您以更開放的語言表達,促進彼此理解。"
    return response

# 語音生成
def text_to_speech(response_text):
    tts = gTTS(text=response_text, lang="zh")
    tts.save("response_audio.mp3")
    return "response_audio.mp3"

# Gradio UI 設計
with gr.Blocks() as demo:
    gr.Markdown("# 🌟 深層情感交流輔助平台 🌟\n一個促進情感溝通與理解的互動平台")
    
    with gr.Row():
        mode = gr.Radio(["文字", "語音"], label="輸入模式", value="文字")
        input_text = gr.Textbox(label="輸入您的內容")
    
    with gr.Row():
        response_textbox = gr.Textbox(label="建議回應", interactive=False)
        response_audio = gr.Audio(label="語音回應", interactive=False)
        bg_music = gr.Audio(value=play_background_music(), label="撫慰音樂")
    
    submit_button = gr.Button("生成回應")
    
    submit_button.click(
        fn=generate_response,
        inputs=[input_text, mode],
        outputs=response_textbox
    )
    
    submit_button.click(
        fn=text_to_speech,
        inputs=response_textbox,
        outputs=response_audio
    )

demo.launch()