Spaces:
Runtime error
Runtime error
Update modules/beginner.py
Browse files- modules/beginner.py +23 -48
modules/beginner.py
CHANGED
@@ -1,55 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
-
from utils.helpers import load_content
|
|
|
3 |
|
4 |
def beginner_component():
|
5 |
content = load_content()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
with gr.Column() as beginner:
|
8 |
-
|
9 |
-
|
10 |
-
gr.Markdown("### Vocabulary Practice")
|
11 |
-
word_display = gr.Textbox(label="Afrikaans Word")
|
12 |
-
translation_hidden = gr.Textbox(visible=False)
|
13 |
-
sentence_display = gr.Textbox(label="Example Sentence")
|
14 |
-
user_translation = gr.Textbox(label="Your Translation")
|
15 |
-
check_btn = gr.Button("Check")
|
16 |
-
result = gr.Textbox(label="Result")
|
17 |
-
next_btn = gr.Button("Next Word")
|
18 |
-
|
19 |
-
def get_vocab():
|
20 |
-
exercise = get_random_exercise("vocabulary", "beginner", content)
|
21 |
-
return exercise["word"], exercise["translation"], exercise["sentence"]
|
22 |
-
|
23 |
-
def check_vocab(user_input, correct):
|
24 |
-
return "Correct! β" if check_translation(user_input, correct) else "Try again"
|
25 |
-
|
26 |
-
next_btn.click(fn=get_vocab, outputs=[word_display, translation_hidden, sentence_display])
|
27 |
-
check_btn.click(fn=check_vocab, inputs=[user_translation, translation_hidden], outputs=[result])
|
28 |
-
|
29 |
-
# Grammar Section
|
30 |
-
with gr.Tab("Grammar"):
|
31 |
-
gr.Markdown("### Grammar Rules")
|
32 |
-
for rule in content["grammar"]["beginner"]:
|
33 |
-
with gr.Accordion(rule["rule"], open=False):
|
34 |
-
gr.Markdown(rule["explanation"])
|
35 |
-
|
36 |
-
# Reading Section
|
37 |
-
with gr.Tab("Reading"):
|
38 |
-
gr.Markdown("### Reading Practice")
|
39 |
-
for text in content["reading"]["beginner"]:
|
40 |
-
with gr.Accordion(text["title"], open=False):
|
41 |
-
gr.Markdown(text["text"])
|
42 |
-
for i, question in enumerate(text["questions"]):
|
43 |
-
gr.Markdown(f"**Question {i+1}:** {question}")
|
44 |
-
answer = gr.Textbox(label="Your Answer")
|
45 |
-
result = gr.Textbox(label="Result")
|
46 |
-
check_btn = gr.Button(f"Check Answer {i+1}")
|
47 |
-
|
48 |
-
check_btn.click(
|
49 |
-
fn=lambda ans, idx=i, txt=text: "Correct! β"
|
50 |
-
if check_reading_answer(ans, idx, txt) else "Try again",
|
51 |
-
inputs=[answer],
|
52 |
-
outputs=[result]
|
53 |
-
)
|
54 |
-
|
55 |
return beginner
|
|
|
1 |
import gradio as gr
|
2 |
+
from utils.helpers import load_content
|
3 |
+
from .translation_model import TranslationModel
|
4 |
|
5 |
def beginner_component():
|
6 |
content = load_content()
|
7 |
+
translator = TranslationModel()
|
8 |
+
|
9 |
+
def check_answer(question, user_answer):
|
10 |
+
if not user_answer.strip():
|
11 |
+
return "Please enter an answer"
|
12 |
+
|
13 |
+
# Translate user's answer to English
|
14 |
+
translation = translator.translate(user_answer.lower(), "af", "en")
|
15 |
+
|
16 |
+
# Get expected answer
|
17 |
+
expected = content["vocabulary"]["beginner"].get(question, "")
|
18 |
+
|
19 |
+
# Translate expected answer to English for comparison
|
20 |
+
expected_translation = translator.translate(expected, "af", "en")
|
21 |
+
|
22 |
+
if translation.lower().strip() == expected_translation.lower().strip():
|
23 |
+
return f"Correct! β '{user_answer}' means '{translation}'"
|
24 |
+
else:
|
25 |
+
return f"Not quite. '{user_answer}' means '{translation}'. The correct answer is '{expected}'"
|
26 |
|
27 |
with gr.Column() as beginner:
|
28 |
+
pass
|
29 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
return beginner
|