bori0824 commited on
Commit
d971390
1 Parent(s): d61536a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -65
app.py CHANGED
@@ -30,7 +30,7 @@ image_urls = {
30
  }
31
 
32
  def generate_output(word):
33
- definition = word + "." + " It means " + word_definitions[word]
34
 
35
  # Get the image
36
  image_url = image_urls[word]
@@ -46,8 +46,14 @@ def generate_output(word):
46
  return img, audio_file
47
 
48
  # Create the quiz function
49
- def quiz(word, answer):
50
- if answer == word_definitions[word]:
 
 
 
 
 
 
51
  return "Correct!"
52
  else:
53
  return "Incorrect, try again."
@@ -56,7 +62,7 @@ def quiz(word, answer):
56
  iface = gr.Blocks()
57
 
58
  with iface:
59
- gr.Markdown("# Vocabulary Builder with Quiz and Drag-and-Drop Activity")
60
 
61
  with gr.Tab("Learn Words"):
62
  word_dropdown = gr.Dropdown(choices=list(word_definitions.keys()), label="Choose a word")
@@ -66,68 +72,10 @@ with iface:
66
  learn_button.click(generate_output, inputs=word_dropdown, outputs=[img_output, audio_output])
67
 
68
  with gr.Tab("Quiz"):
69
- quiz_word = gr.Dropdown(choices=list(word_definitions.keys()), label="Choose a word to quiz")
70
- quiz_input = gr.Textbox(label="Type the definition")
71
  quiz_button = gr.Button("Submit")
72
  quiz_result = gr.Textbox(label="Result")
73
- quiz_button.click(quiz, inputs=[quiz_word, quiz_input], outputs=quiz_result)
74
-
75
- with gr.Tab("Drag-and-Drop Activity"):
76
- gr.HTML("""
77
- <div style="display: flex; flex-direction: row; justify-content: space-around;">
78
- <div style="width: 30%;">
79
- <h3>Words</h3>
80
- <div id="words" ondrop="drop(event)" ondragover="allowDrop(event)">
81
- """ + "".join([f'<div id="{word}" draggable="true" ondragstart="drag(event)">{word}</div>' for word in word_definitions.keys()]) + """
82
- </div>
83
- </div>
84
- <div style="width: 30%;">
85
- <h3>Images</h3>
86
- <div id="images" ondrop="drop(event)" ondragover="allowDrop(event)">
87
- """ + "".join([f'<img id="{word}_img" src="{url}" draggable="true" ondragstart="drag(event)" width="100"/>' for word, url in image_urls.items()]) + """
88
- </div>
89
- </div>
90
- <div style="width: 30%;">
91
- <h3>Definitions</h3>
92
- <div id="definitions" ondrop="drop(event)" ondragover="allowDrop(event)">
93
- """ + "".join([f'<div id="{word}_def" draggable="true" ondragstart="drag(event)">{definition}</div>' for word, definition in word_definitions.items()]) + """
94
- </div>
95
- </div>
96
- </div>
97
- <button onclick="checkMatch()">Check Match</button>
98
- <div id="result"></div>
99
- <script>
100
- function allowDrop(ev) {
101
- ev.preventDefault();
102
- }
103
-
104
- function drag(ev) {
105
- ev.dataTransfer.setData("text", ev.target.id);
106
- }
107
-
108
- function drop(ev) {
109
- ev.preventDefault();
110
- var data = ev.dataTransfer.getData("text");
111
- ev.target.appendChild(document.getElementById(data));
112
- }
113
-
114
- function checkMatch() {
115
- let result = '';
116
- const words = document.getElementById('words').children;
117
- const images = document.getElementById('images').children;
118
- const definitions = document.getElementById('definitions').children;
119
-
120
- for (let word of words) {
121
- if (document.getElementById(word.id).parentElement.id === 'definitions' &&
122
- document.getElementById(word.id + '_img').parentElement.id === 'images') {
123
- result += word.id + ' matched correctly!<br>';
124
- } else {
125
- result += word.id + ' did not match.<br>';
126
- }
127
- }
128
- document.getElementById('result').innerHTML = result;
129
- }
130
- </script>
131
- """)
132
 
133
  iface.launch(debug=True)
 
30
  }
31
 
32
  def generate_output(word):
33
+ definition = word + ". " + "It means: " + word_definitions[word]
34
 
35
  # Get the image
36
  image_url = image_urls[word]
 
46
  return img, audio_file
47
 
48
  # Create the quiz function
49
+ def quiz(definition, selected_word):
50
+ correct_word = None
51
+ for word, defn in word_definitions.items():
52
+ if defn == definition:
53
+ correct_word = word
54
+ break
55
+
56
+ if selected_word == correct_word:
57
  return "Correct!"
58
  else:
59
  return "Incorrect, try again."
 
62
  iface = gr.Blocks()
63
 
64
  with iface:
65
+ gr.Markdown("# Vocabulary Builder with Quiz")
66
 
67
  with gr.Tab("Learn Words"):
68
  word_dropdown = gr.Dropdown(choices=list(word_definitions.keys()), label="Choose a word")
 
72
  learn_button.click(generate_output, inputs=word_dropdown, outputs=[img_output, audio_output])
73
 
74
  with gr.Tab("Quiz"):
75
+ definition_text = gr.Dropdown(choices=list(word_definitions.values()), label="Choose the correct word for this definition")
76
+ wordbank = gr.Dropdown(choices=list(word_definitions.keys()), label="Wordbank")
77
  quiz_button = gr.Button("Submit")
78
  quiz_result = gr.Textbox(label="Result")
79
+ quiz_button.click(quiz, inputs=[definition_text, wordbank], outputs=quiz_result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  iface.launch(debug=True)