broadfield-dev commited on
Commit
fe383bb
·
verified ·
1 Parent(s): c0c9687

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template, jsonify
2
+ import json
3
+ import copy
4
+
5
+ app = Flask(__name__)
6
+
7
+ # Basic phoneme classification
8
+ VOWELS = {'a', 'e', 'i', 'o', 'u', 'æ', 'ɪ', 'ʊ', 'ə', 'ɛ', 'ɔ', 'ʌ', 'ɑ', 'ɒ', 'ɘ', 'ɚ', 'ɜ', 'ɞ', 'ɨ', 'ɵ', 'ɶ', 'ʉ', 'ʏ'}
9
+ CONSONANTS = {'b', 'c', 'd', 'f', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'ç', 'ð', 'ħ', 'ŋ', 'ɓ', 'ɕ', 'ɖ', 'ɗ', 'ɟ', 'ɠ', 'ɡ', 'ɢ', 'ɣ', 'ɥ', 'ɦ', 'ɧ', 'ɫ', 'ɬ', 'ɭ', 'ɮ', 'ɰ', 'ɱ', 'ɲ', 'ɳ', 'ɴ', 'ɸ', 'ɹ', 'ɺ', 'ɻ', 'ɽ', 'ɾ', 'ʀ', 'ʁ', 'ʂ', 'ʃ', 'ʄ', 'ʈ', 'ʋ', 'ʍ', 'ʎ', 'ʐ', 'ʑ', 'ʒ', 'ʔ', 'ʕ', 'ʘ', 'ʙ', 'ʛ', 'ʜ', 'ʝ', 'ʟ', 'ʡ', 'ʢ', 'ⱱ', 'θ', 'χ', 'β'}
10
+ STRESS_MARKERS = {'ˈ', 'ˌ'}
11
+
12
+ def classify_phoneme(phoneme):
13
+ if phoneme in VOWELS:
14
+ return 'vowel'
15
+ elif phoneme in CONSONANTS:
16
+ return 'consonant'
17
+ elif phoneme in STRESS_MARKERS:
18
+ return 'stress'
19
+ return 'special'
20
+
21
+ def adjust_phonemes(model_data, emotion):
22
+ modified_data = copy.deepcopy(model_data)
23
+ phoneme_id_map = modified_data['phoneme_id_map']
24
+ num_symbols = modified_data['num_symbols']
25
+
26
+ new_phoneme_id_map = {}
27
+
28
+ for phoneme, id_list in phoneme_id_map.items():
29
+ base_id = id_list[0] # Assume single ID per phoneme
30
+ phoneme_type = classify_phoneme(phoneme)
31
+
32
+ if emotion == 'happy':
33
+ if phoneme_type == 'vowel':
34
+ new_id = min(num_symbols - 1, base_id + 5)
35
+ elif phoneme_type == 'consonant':
36
+ new_id = min(num_symbols - 1, base_id + 2)
37
+ elif phoneme_type == 'stress':
38
+ new_id = min(num_symbols - 1, base_id + 3)
39
+ else:
40
+ new_id = base_id
41
+ elif emotion == 'sad':
42
+ if phoneme_type == 'vowel':
43
+ new_id = max(0, base_id - 5)
44
+ elif phoneme_type == 'consonant':
45
+ new_id = max(0, base_id - 2)
46
+ elif phoneme_type == 'stress':
47
+ new_id = max(0, base_id - 1)
48
+ else:
49
+ new_id = base_id
50
+ elif emotion == 'angry':
51
+ if phoneme_type == 'vowel':
52
+ new_id = min(num_symbols - 1, base_id + 3)
53
+ elif phoneme_type == 'consonant':
54
+ new_id = min(num_symbols - 1, base_id + 5)
55
+ elif phoneme_type == 'stress':
56
+ new_id = min(num_symbols - 1, base_id + 10)
57
+ else:
58
+ new_id = base_id
59
+
60
+ new_phoneme_id_map[phoneme] = [new_id]
61
+
62
+ modified_data['phoneme_id_map'] = new_phoneme_id_map
63
+ return modified_data
64
+
65
+ @app.route('/')
66
+ def index():
67
+ return render_template('index.html')
68
+
69
+ @app.route('/process_json', methods=['POST'])
70
+ def process_json():
71
+ try:
72
+ data = request.get_json()
73
+ model_json = data.get('model_json', '')
74
+
75
+ # Parse the input JSON
76
+ model_data = json.loads(model_json)
77
+
78
+ # Define emotions to process
79
+ emotions = ['happy', 'sad', 'angry']
80
+ results = {}
81
+
82
+ # Generate modified JSON for each emotion
83
+ for emotion in emotions:
84
+ modified_data = adjust_phonemes(model_data, emotion)
85
+ results[emotion] = json.dumps(modified_data, indent=2)
86
+
87
+ return jsonify({'status': 'success', 'results': results})
88
+ except Exception as e:
89
+ return jsonify({'status': 'error', 'message': str(e)}), 400
90
+
91
+ if __name__ == '__main__':
92
+ app.run(host="0.0.0.0",port=7850, debug=True)