kanra208 commited on
Commit
bf3b77e
·
verified ·
1 Parent(s): 3ec00d7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import zipfile
4
+ from transformers import pipeline
5
+ import gradio as gr
6
+
7
+ # 使用 Hugging Face 的翻譯管道
8
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
9
+
10
+ # 使用 Hugging Face 的英文情緒分類管道
11
+ classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
12
+
13
+ # 定義情緒標籤的映射
14
+ emotion_labels = ["joy", "surprise", "sadness", "anger", "anxiety", "fear"]
15
+
16
+ base_dir = '/content/'
17
+
18
+ for emotion_label in emotion_labels:
19
+ os.makedirs(f'{base_dir}/{emotion_label}', exist_ok=True)
20
+
21
+ !wget --no-check-certificate \
22
+ https://github.com/AlanTFK/Cat/releases/download/NewAgeMusic/Music.zip \
23
+ -O /content/Music.zip
24
+
25
+ local_zip = '/content/Music.zip'
26
+ zip_ref = zipfile.ZipFile(local_zip, 'r')
27
+ zip_ref.extractall('/content')
28
+ zip_ref.close()
29
+
30
+ def classify_emotions(text):
31
+ # 首先將中文文本翻譯成英文
32
+ translated_text = translator(text, max_length=512)[0]['translation_text']
33
+ print(f"翻譯後的英文文本:{translated_text}")
34
+
35
+ # 然後對翻譯後的英文文本進行情緒分類
36
+ results = classifier(translated_text, return_all_scores=True)
37
+
38
+ # 提取各個情緒的分數
39
+ emotion_scores = {emotion: 0.0 for emotion in emotion_labels}
40
+ for result in results:
41
+ for score in result:
42
+ emotion = score['label']
43
+ if emotion in emotion_labels:
44
+ emotion_scores[emotion] = score['score']
45
+
46
+ return emotion_scores
47
+
48
+ def get_main_emotion(emotion_scores):
49
+ # 找出分數最高的情緒
50
+ main_emotion = max(emotion_scores, key=emotion_scores.get)
51
+ return main_emotion
52
+
53
+ def select_random_song(emotion_folder):
54
+ # 列出文件夾中的所有文件
55
+ files = os.listdir(emotion_folder)
56
+ # 過濾出音頻文件(例如 .mp3 文件)
57
+ audio_files = [file for file in files if file.endswith('.mp3')]
58
+ if not audio_files:
59
+ raise FileNotFoundError(f"No audio files found in {emotion_folder}")
60
+
61
+ # 隨機選擇一個音頻文件
62
+ random_song = random.choice(audio_files)
63
+ return os.path.join(emotion_folder, random_song)
64
+
65
+ def emotion_music_recommendation(text):
66
+ emotion_scores = classify_emotions(text)
67
+ main_emotion = get_main_emotion(emotion_scores)
68
+ emotion_folder = os.path.join(base_dir, main_emotion)
69
+
70
+ try:
71
+ selected_song = select_random_song(emotion_folder)
72
+ return selected_song, f"檢測到的情緒:{main_emotion}"
73
+ except FileNotFoundError as e:
74
+ return str(e), "error"
75
+
76
+ def play_music_interface(text):
77
+ audio_file, emotion = emotion_music_recommendation(text)
78
+ return audio_file, emotion
79
+
80
+ # 創建 Gradio 介面
81
+ iface = gr.Interface(
82
+ fn=play_music_interface,
83
+ inputs="text",
84
+ outputs=["audio", "text"],
85
+ title="🎵 New Age AI 🎵",
86
+ description="今天還好嗎?",
87
+ )
88
+
89
+ iface.launch(inline=False)