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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -11
app.py CHANGED
@@ -3,6 +3,7 @@ from gtts import gTTS
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
 
6
 
7
  # Manually add definitions
8
  word_definitions = {
@@ -16,7 +17,7 @@ word_definitions = {
16
  "whisper": "To speak very softly or quietly."
17
  }
18
 
19
- # Corresponding image URLs (example URLs, replace with actual GitHub links)
20
  image_urls = {
21
  "quaint": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/001.png",
22
  "explore": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/002.png",
@@ -29,7 +30,7 @@ image_urls = {
29
  }
30
 
31
  def generate_output(word):
32
- definition = word + "." + "It means" + word_definitions[word]
33
 
34
  # Get the image
35
  image_url = image_urls[word]
@@ -44,13 +45,89 @@ def generate_output(word):
44
 
45
  return img, audio_file
46
 
 
 
 
 
 
 
 
47
  # Create the Gradio interface
48
- iface = gr.Interface(
49
- fn=generate_output,
50
- inputs=gr.Dropdown(choices=list(word_definitions.keys()), label="Choose a word"),
51
- outputs=[gr.Image(type="pil"), gr.Audio(type="filepath", autoplay=True)],
52
- title="Word Definition with Image and Audio"
53
- )
54
-
55
- # Launch the interface
56
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
6
+ import random
7
 
8
  # Manually add definitions
9
  word_definitions = {
 
17
  "whisper": "To speak very softly or quietly."
18
  }
19
 
20
+ # Corresponding image URLs
21
  image_urls = {
22
  "quaint": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/001.png",
23
  "explore": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/002.png",
 
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]
 
45
 
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."
54
+
55
  # Create the Gradio interface
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")
63
+ img_output = gr.Image(type="pil")
64
+ audio_output = gr.Audio(type="filepath", autoplay=True)
65
+ learn_button = gr.Button("Learn")
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)