Spaces:
Runtime error
Runtime error
from flask import Flask, request, render_template, jsonify | |
import json | |
import copy | |
app = Flask(__name__) | |
# Basic phoneme classification | |
VOWELS = ['a', 'e', 'i', 'o', 'u', 'æ', 'ɪ', 'ʊ', 'ə', 'ɛ', 'ɔ', 'ʌ', 'ɑ', 'ɒ', 'ɘ', 'ɚ', 'ɜ', 'ɞ', 'ɨ', 'ɵ', 'ɶ', 'ʉ', 'ʏ'] | |
CONSONANTS = ['b', 'c', 'd', 'f', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'ç', 'ð', 'ħ', 'ŋ', 'ɓ', 'ɕ', 'ɖ', 'ɗ', 'ɟ', 'ɠ', 'ɡ', 'ɢ', 'ɣ', 'ɥ', 'ɦ', 'ɧ', 'ɫ', 'ɬ', 'ɭ', 'ɮ', 'ɰ', 'ɱ', 'ɲ', 'ɳ', 'ɴ', 'ɸ', 'ɹ', 'ɺ', 'ɻ', 'ɽ', 'ɾ', 'ʀ', 'ʁ', 'ʂ', 'ʃ', 'ʄ', 'ʈ', 'ʋ', 'ʍ', 'ʎ', 'ʐ', 'ʑ', 'ʒ', 'ʔ', 'ʕ', 'ʘ', 'ʙ', 'ʛ', 'ʜ', 'ʝ', 'ʟ', 'ʡ', 'ʢ', 'ⱱ', 'θ', 'χ', 'β'] | |
STRESS_MARKERS = ['ˈ', 'ˌ'] | |
def classify_phoneme(phoneme): | |
if phoneme in VOWELS: | |
return 'vowel' | |
elif phoneme in CONSONANTS: | |
return 'consonant' | |
elif phoneme in STRESS_MARKERS: | |
return 'stress' | |
return 'special' | |
def adjust_phonemes(model_data, emotion): | |
modified_data = copy.deepcopy(model_data) | |
phoneme_id_map = modified_data['phoneme_id_map'] | |
num_symbols = modified_data['num_symbols'] | |
new_phoneme_id_map = {} | |
for phoneme, id_list in phoneme_id_map.items(): | |
base_id = id_list[0] # Assume single ID per phoneme | |
phoneme_type = classify_phoneme(phoneme) | |
if emotion == 'happy': | |
if phoneme_type == 'vowel': | |
new_id = min(num_symbols - 1, base_id + 5) | |
elif phoneme_type == 'consonant': | |
new_id = min(num_symbols - 1, base_id + 2) | |
elif phoneme_type == 'stress': | |
new_id = min(num_symbols - 1, base_id + 3) | |
else: | |
new_id = base_id | |
elif emotion == 'sad': | |
if phoneme_type == 'vowel': | |
new_id = max(0, base_id - 5) | |
elif phoneme_type == 'consonant': | |
new_id = max(0, base_id - 2) | |
elif phoneme_type == 'stress': | |
new_id = max(0, base_id - 1) | |
else: | |
new_id = base_id | |
elif emotion == 'angry': | |
if phoneme_type == 'vowel': | |
new_id = min(num_symbols - 1, base_id + 3) | |
elif phoneme_type == 'consonant': | |
new_id = min(num_symbols - 1, base_id + 5) | |
elif phoneme_type == 'stress': | |
new_id = min(num_symbols - 1, base_id + 10) | |
else: | |
new_id = base_id | |
new_phoneme_id_map[phoneme] = [new_id] | |
modified_data['phoneme_id_map'] = new_phoneme_id_map | |
return modified_data | |
def index(): | |
return render_template('index.html') | |
def process_json(): | |
try: | |
data = request.get_json() | |
model_json = data.get('model_json', '') | |
# Parse the input JSON | |
model_data = json.loads(model_json) | |
# Define emotions to process | |
emotions = ['happy', 'sad', 'angry'] | |
results = {} | |
# Generate modified JSON for each emotion | |
for emotion in emotions: | |
modified_data = adjust_phonemes(model_data, emotion) | |
results[emotion] = json.dumps(modified_data, indent=2) | |
return jsonify({'status': 'success', 'results': results}) | |
except Exception as e: | |
return jsonify({'status': 'error', 'message': str(e)}), 400 | |
if __name__ == '__main__': | |
app.run(host="0.0.0.0",port=7850, debug=True) |