Peiiiiiiiiru
commited on
Commit
•
88fa5f2
1
Parent(s):
c215e71
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
from gtts import gTTS
|
5 |
+
import librosa
|
6 |
+
|
7 |
+
# 背景音樂播放
|
8 |
+
def play_background_music():
|
9 |
+
return "background_music.mp3" # 輕柔音樂檔案路徑
|
10 |
+
|
11 |
+
# 回應生成功能
|
12 |
+
def generate_response(input_text, mode):
|
13 |
+
response = ""
|
14 |
+
if mode == "文字":
|
15 |
+
response = f"我理解您的感受:'{input_text}'。非暴力溝通建議您表達觀察、感受與需求,或許可以說:'當我看到這件事時,我感到不安,因為我需要理解與支持。'"
|
16 |
+
elif mode == "語音":
|
17 |
+
response = f"基於您的語音內容,我們分析出溝通時可能的誤解。建議您以更開放的語言表達,促進彼此理解。"
|
18 |
+
return response
|
19 |
+
|
20 |
+
# 語音生成
|
21 |
+
def text_to_speech(response_text):
|
22 |
+
tts = gTTS(text=response_text, lang="zh")
|
23 |
+
tts.save("response_audio.mp3")
|
24 |
+
return "response_audio.mp3"
|
25 |
+
|
26 |
+
# Gradio UI 設計
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
gr.Markdown("# 🌟 深層情感交流輔助平台 🌟\n一個促進情感溝通與理解的互動平台")
|
29 |
+
|
30 |
+
with gr.Row():
|
31 |
+
mode = gr.Radio(["文字", "語音"], label="輸入模式", value="文字")
|
32 |
+
input_text = gr.Textbox(label="輸入您的內容")
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
response_textbox = gr.Textbox(label="建議回應", interactive=False)
|
36 |
+
response_audio = gr.Audio(label="語音回應", interactive=False)
|
37 |
+
bg_music = gr.Audio(value=play_background_music(), label="撫慰音樂")
|
38 |
+
|
39 |
+
submit_button = gr.Button("生成回應")
|
40 |
+
|
41 |
+
submit_button.click(
|
42 |
+
fn=generate_response,
|
43 |
+
inputs=[input_text, mode],
|
44 |
+
outputs=response_textbox
|
45 |
+
)
|
46 |
+
|
47 |
+
submit_button.click(
|
48 |
+
fn=text_to_speech,
|
49 |
+
inputs=response_textbox,
|
50 |
+
outputs=response_audio
|
51 |
+
)
|
52 |
+
|
53 |
+
demo.launch()
|