Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
# Define your IPA data with additional attributes
|
| 5 |
+
ipa_data = {
|
| 6 |
+
'p': {'Voicing': 'voiceless', 'Place': 'bilabial', 'Manner': 'stop','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 7 |
+
'b': {'Voicing': 'voiced', 'Place': 'bilabial', 'Manner': 'stop','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 8 |
+
't': {'Voicing': 'voiceless', 'Place': 'alveolar', 'Manner': 'stop','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 9 |
+
'd': {'Voicing': 'voiced', 'Place': 'alveolar', 'Manner': 'stop','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 10 |
+
'k': {'Voicing': 'voiceless', 'Place': 'velar', 'Manner': 'stop','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 11 |
+
'g': {'Voicing': 'voiced', 'Place': 'velar', 'Manner': 'stop','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 12 |
+
'f': {'Voicing': 'voiceless', 'Place': 'labio-dental', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 13 |
+
'v': {'Voicing': 'voiced', 'Place': 'labio-dental', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 14 |
+
'θ': {'Voicing': 'voiceless', 'Place': 'dental', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 15 |
+
'ð': {'Voicing': 'voiced', 'Place': 'dental', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 16 |
+
's': {'Voicing': 'voiceless', 'Place': 'alveolar', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 17 |
+
'z': {'Voicing': 'voiced', 'Place': 'alveolar', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 18 |
+
'ʃ': {'Voicing': 'voiceless', 'Place': 'palato-alveolar', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 19 |
+
'ʒ': {'Voicing': 'voiced', 'Place': 'palato-alveolar', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 20 |
+
'tʃ': {'Voicing': 'voiceless', 'Place': 'palato-alveolar', 'Manner': 'affricate','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 21 |
+
'dʒ': {'Voicing': 'voiced', 'Place': 'palato-alveolar', 'Manner': 'affricate','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 22 |
+
'h': {'Voicing': 'voiceless', 'Place': 'glottal', 'Manner': 'fricative','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 23 |
+
'm': {'Voicing': 'voiced', 'Place': 'bilabial', 'Manner': 'stop','Oro-nasal': 'nasal','Centrality':'(not applicable)'},
|
| 24 |
+
'n': {'Voicing': 'voiced', 'Place': 'alveolar', 'Manner': 'stop','Oro-nasal': 'nasal','Centrality':'(not applicable)'},
|
| 25 |
+
'ŋ': {'Voicing': 'voiced', 'Place': 'velar', 'Manner': 'stop','Oro-nasal': 'nasal','Centrality':'(not applicable)'},
|
| 26 |
+
'ɹ': {'Voicing': 'voiced', 'Place': 'alveolar', 'Manner': 'approximant','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 27 |
+
'l': {'Voicing': 'voiced', 'Place': 'alveolar', 'Manner': 'approximant','Oro-nasal': '(oral)','Centrality':'lateral'},
|
| 28 |
+
'j': {'Voicing': 'voiced', 'Place': 'palatal', 'Manner': 'approximant','Oro-nasal': '(oral)','Centrality':'(central)'},
|
| 29 |
+
'w': {'Voicing': 'voiced', 'Place': 'labio-velar', 'Manner': 'approximant','Oro-nasal': '(oral)','Centrality':'(central)'}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
def select_random_symbol():
|
| 33 |
+
""" Select a random IPA symbol """
|
| 34 |
+
symbol = random.choice(list(ipa_data.keys()))
|
| 35 |
+
return symbol, ipa_data[symbol]
|
| 36 |
+
|
| 37 |
+
def validate_selections(ipa_symbol, user_voicing, user_place, user_manner, user_oronasal, user_centrality, correct_count, attempts):
|
| 38 |
+
""" Check user's selections against the actual IPA symbol properties """
|
| 39 |
+
correct_data = ipa_data[ipa_symbol]
|
| 40 |
+
correct = (correct_data['Voicing'] == user_voicing and
|
| 41 |
+
correct_data['Place'] == user_place and
|
| 42 |
+
correct_data['Manner'] == user_manner and
|
| 43 |
+
correct_data['Oro-nasal'] == user_oronasal and
|
| 44 |
+
correct_data['Centrality'] == user_centrality)
|
| 45 |
+
attempts += 1
|
| 46 |
+
if correct:
|
| 47 |
+
correct_count += 1
|
| 48 |
+
return "Correct!", correct_count, attempts
|
| 49 |
+
else:
|
| 50 |
+
return "Incorrect! Correct values are: \n- Voicing: {}, \n- Place: {}, \n- Manner: {}, \n- Oro-nasal: {}, \n- Centrality: {}"\
|
| 51 |
+
.format(correct_data['Voicing'], correct_data['Place'], correct_data['Manner'], correct_data['Oro-nasal'], correct_data['Centrality']), correct_count, attempts
|
| 52 |
+
|
| 53 |
+
def setup_interface():
|
| 54 |
+
with gr.Blocks() as app:
|
| 55 |
+
gr.Markdown("### IPA Practice App")
|
| 56 |
+
|
| 57 |
+
new_symbol_button = gr.Button("Display a New Symbol")
|
| 58 |
+
symbol_display = gr.Textbox(label="IPA Symbol", interactive=False)
|
| 59 |
+
|
| 60 |
+
voicing_radio = gr.Radio(label="Voicing", choices=['voiceless', 'voiced'])
|
| 61 |
+
place_radio = gr.Radio(label="Place", choices=['bilabial', 'labio-dental', 'dental', 'alveolar', 'palato-alveolar', 'palatal', 'velar', 'glottal'])
|
| 62 |
+
manner_radio = gr.Radio(label="Manner", choices=['stop', 'fricative', 'affricate', 'approximant'])
|
| 63 |
+
oronasal_radio = gr.Radio(label="Oro-nasal", choices=['(oral)', 'nasal'])
|
| 64 |
+
centrality_radio = gr.Radio(label="Centrality", choices=['(central)', 'lateral', '(not applicable)'])
|
| 65 |
+
|
| 66 |
+
submit_button = gr.Button("Submit")
|
| 67 |
+
result_display = gr.Textbox(label="Result", interactive=False)
|
| 68 |
+
correct_count = gr.State(0)
|
| 69 |
+
attempts = gr.State(0)
|
| 70 |
+
|
| 71 |
+
quit_button = gr.Button("See the total score")
|
| 72 |
+
score_display = gr.Textbox(label="Score", interactive=False)
|
| 73 |
+
|
| 74 |
+
def update_display():
|
| 75 |
+
symbol, _ = select_random_symbol()
|
| 76 |
+
symbol_display.value = symbol
|
| 77 |
+
return symbol
|
| 78 |
+
|
| 79 |
+
def submit_results(ipa_symbol, voicing, place, manner, oronasal, centrality, correct_count, attempts):
|
| 80 |
+
result, updated_correct_count, updated_attempts = validate_selections(ipa_symbol, voicing, place, manner, oronasal, centrality, correct_count, attempts)
|
| 81 |
+
return result, updated_correct_count, updated_attempts
|
| 82 |
+
|
| 83 |
+
def quit_session(correct_count, attempts):
|
| 84 |
+
final_score = f"Final Score: {correct_count}/{attempts}"
|
| 85 |
+
return final_score
|
| 86 |
+
|
| 87 |
+
new_symbol_button.click(
|
| 88 |
+
fn=update_display,
|
| 89 |
+
inputs=[],
|
| 90 |
+
outputs=[symbol_display]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
submit_button.click(
|
| 94 |
+
fn=submit_results,
|
| 95 |
+
inputs=[symbol_display, voicing_radio, place_radio, manner_radio, oronasal_radio, centrality_radio, correct_count, attempts],
|
| 96 |
+
outputs=[result_display, correct_count, attempts]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
quit_button.click(
|
| 100 |
+
fn=quit_session,
|
| 101 |
+
inputs=[correct_count, attempts],
|
| 102 |
+
outputs=[score_display]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
gr.Row(new_symbol_button, symbol_display)
|
| 106 |
+
gr.Row(submit_button, quit_button)
|
| 107 |
+
gr.Row(result_display, score_display)
|
| 108 |
+
|
| 109 |
+
return app
|
| 110 |
+
|
| 111 |
+
app = setup_interface()
|
| 112 |
+
app.launch(debug=True)
|