|
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" |
|
|
|
|
|
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() |
|
|