File size: 3,550 Bytes
fe383bb
 
 
 
 
 
f89381d
81cc7f6
f89381d
fe383bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/process_json', methods=['POST'])
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)