|
import os |
|
import random |
|
import zipfile |
|
import subprocess |
|
from transformers import pipeline |
|
import gradio as gr |
|
|
|
|
|
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en") |
|
|
|
|
|
classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base") |
|
|
|
|
|
emotion_labels = ["joy", "surprise", "sadness", "anger", "anxiety", "fear"] |
|
|
|
|
|
base_dir = '/home/user/app/huggingface_models' |
|
|
|
|
|
for emotion_label in emotion_labels: |
|
os.makedirs(f'{base_dir}/{emotion_label}', exist_ok=True) |
|
|
|
|
|
subprocess.run([ |
|
"wget", |
|
"--no-check-certificate", |
|
"https://github.com/AlanTFK/Cat/releases/download/NewAgeMusic/Music.zip", |
|
"-O", |
|
f'{base_dir}/Music.zip' |
|
], check=True) |
|
|
|
local_zip = f'{base_dir}/Music.zip' |
|
zip_ref = zipfile.ZipFile(local_zip, 'r') |
|
zip_ref.extractall(base_dir) |
|
zip_ref.close() |
|
|
|
|
|
def classify_emotions(text): |
|
translated_text = translator(text, max_length=512)[0]['translation_text'] |
|
print(f"Translated text: {translated_text}") |
|
|
|
results = classifier(translated_text, return_all_scores=True) |
|
|
|
emotion_scores = {emotion: 0.0 for emotion in emotion_labels} |
|
for result in results: |
|
for score in result: |
|
emotion = score['label'] |
|
if emotion in emotion_labels: |
|
emotion_scores[emotion] = score['score'] |
|
|
|
return emotion_scores |
|
|
|
|
|
def get_main_emotion(emotion_scores): |
|
main_emotion = max(emotion_scores, key=emotion_scores.get) |
|
return main_emotion |
|
|
|
|
|
def select_random_song(emotion_folder): |
|
files = os.listdir(emotion_folder) |
|
audio_files = [file for file in files if file.endswith('.mp3')] |
|
if not audio_files: |
|
raise FileNotFoundError(f"No audio files found in {emotion_folder}") |
|
|
|
random_song = random.choice(audio_files) |
|
return os.path.join(emotion_folder, random_song) |
|
|
|
|
|
def emotion_music_recommendation(text): |
|
emotion_scores = classify_emotions(text) |
|
main_emotion = get_main_emotion(emotion_scores) |
|
emotion_folder = os.path.join(base_dir, main_emotion) |
|
|
|
try: |
|
selected_song = select_random_song(emotion_folder) |
|
return selected_song, f"Detected emotion: {main_emotion}" |
|
except FileNotFoundError as e: |
|
return str(e), "error" |
|
|
|
|
|
def play_music_interface(text): |
|
audio_file, emotion = emotion_music_recommendation(text) |
|
return audio_file, emotion |
|
|
|
|
|
iface = gr.Interface( |
|
fn=play_music_interface, |
|
inputs="text", |
|
outputs=["audio", "text"], |
|
title="π΅ New Age AI π΅", |
|
description="How are you feeling today?", |
|
) |
|
|
|
iface.launch() |
|
|