File size: 2,573 Bytes
5677d03
307cb7f
5677d03
 
 
 
307cb7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from utils.helpers import load_content, check_spelling, check_translation, get_random_exercise, check_reading_answer

def intermediate_component():
    content = load_content()
    
    with gr.Column() as intermediate:
        # Vocabulary Section
        with gr.Tab("Vocabulary"):
            gr.Markdown("### Intermediate Vocabulary")
            word_display = gr.Textbox(label="Afrikaans Word")
            translation_hidden = gr.Textbox(visible=False)
            sentence_display = gr.Textbox(label="Example Sentence")
            user_translation = gr.Textbox(label="Your Translation")
            check_btn = gr.Button("Check")
            result = gr.Textbox(label="Result")
            next_btn = gr.Button("Next Word")

            def get_vocab():
                exercise = get_random_exercise("vocabulary", "intermediate")
                return exercise["word"], exercise["translation"], exercise["sentence"]

            def check_vocab(user_input, correct):
                return "Correct! βœ“" if check_translation(user_input, correct) else "Try again"

            next_btn.click(fn=get_vocab, outputs=[word_display, translation_hidden, sentence_display])
            check_btn.click(fn=check_vocab, inputs=[user_translation, translation_hidden], outputs=[result])

        # Grammar Section
        with gr.Tab("Grammar"):
            gr.Markdown("### Intermediate Grammar")
            for rule in content["grammar"]["intermediate"]:
                with gr.Accordion(rule["rule"], open=False):
                    gr.Markdown(rule["explanation"])

        # Reading Section
        with gr.Tab("Reading"):
            gr.Markdown("### Intermediate Reading")
            for text in content["reading"]["intermediate"]:
                with gr.Accordion(text["title"], open=False):
                    gr.Markdown(text["text"])
                    for i, question in enumerate(text["questions"]):
                        gr.Markdown(f"**Question {i+1}:** {question}")
                        answer = gr.Textbox(label="Your Answer")
                        result = gr.Textbox(label="Result")
                        check_btn = gr.Button(f"Check Answer {i+1}")
                        
                        check_btn.click(
                            fn=lambda ans, idx=i, txt=text: "Correct! βœ“" 
                            if check_reading_answer(ans, idx, txt) else "Try again",
                            inputs=[answer],
                            outputs=[result]
                        )

    return intermediate