Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +136 -0
- words_divided.json +0 -0
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Load vocabulary words from JSON
|
| 6 |
+
def load_words(file_path):
|
| 7 |
+
with open(file_path, 'r', encoding="utf-8") as file:
|
| 8 |
+
data = json.load(file)
|
| 9 |
+
return data
|
| 10 |
+
|
| 11 |
+
# Function to shuffle and get flashcards for a specific day
|
| 12 |
+
def get_flashcards(day, word_list):
|
| 13 |
+
if day not in range(1, 101):
|
| 14 |
+
return f"Invalid day: {day}. Please select a day between 1 and 100.", []
|
| 15 |
+
shuffled_words = random.sample(word_list[day - 1], len(word_list[day - 1]))
|
| 16 |
+
return f"Day {day} loaded! Click 'Next Card' to start.", shuffled_words
|
| 17 |
+
|
| 18 |
+
# Function to show the next card
|
| 19 |
+
def show_next_card(index, word_list):
|
| 20 |
+
if word_list is None or index >= len(word_list) - 1:
|
| 21 |
+
return "<div style='font-size: 36px; color: green; text-align: center;'>You've reviewed all the words for this day!</div>", "", "", index, "Progress: Complete!"
|
| 22 |
+
index += 1
|
| 23 |
+
word_data = word_list[index]
|
| 24 |
+
word_html = (
|
| 25 |
+
f"<div style='font-size: 40px; font-weight: bold; color: #1E90FF; text-align: center;'>"
|
| 26 |
+
f"{word_data['Word']} ({word_data['Part of Speech']})</div>"
|
| 27 |
+
)
|
| 28 |
+
return word_html, "", "", index, f"Progress: {index + 1}/{len(word_list)}"
|
| 29 |
+
|
| 30 |
+
# Function to show the previous card
|
| 31 |
+
def show_previous_card(index, word_list):
|
| 32 |
+
if word_list is None or index <= 0:
|
| 33 |
+
return "<div style='font-size: 36px; text-align: center;'>This is the first card!</div>", "", "", index, "Progress: Start"
|
| 34 |
+
index -= 1
|
| 35 |
+
word_data = word_list[index]
|
| 36 |
+
word_html = (
|
| 37 |
+
f"<div style='font-size: 40px; font-weight: bold; color: #1E90FF; text-align: center;'>"
|
| 38 |
+
f"{word_data['Word']} ({word_data['Part of Speech']})</div>"
|
| 39 |
+
)
|
| 40 |
+
return word_html, "", "", index, f"Progress: {index + 1}/{len(word_list)}"
|
| 41 |
+
|
| 42 |
+
# Function to show the translation
|
| 43 |
+
def show_translation(index, word_list):
|
| 44 |
+
if word_list is None or index >= len(word_list):
|
| 45 |
+
return "<div style='font-size: 20px; text-align: center; color: #555;'>Translation not available.</div>"
|
| 46 |
+
translation = word_list[index]['Translate']
|
| 47 |
+
return f"<div style='font-size: 20px; text-align: center; color: #555;'>{translation}</div>"
|
| 48 |
+
|
| 49 |
+
# Function to show the example sentence
|
| 50 |
+
def show_example(index, word_list):
|
| 51 |
+
if word_list is None or index >= len(word_list):
|
| 52 |
+
return "<div style='font-size: 20px; text-align: center; color: #555;'>Example sentence not available.</div>"
|
| 53 |
+
example_sentence = word_list[index].get("Example Sentence", "No example available.")
|
| 54 |
+
return f"<div style='font-size: 20px; text-align: center; color: #555;'>{example_sentence}</div>"
|
| 55 |
+
|
| 56 |
+
# Load vocabulary data
|
| 57 |
+
file_path = "words_divided.json" # JSON file with divided words
|
| 58 |
+
word_data = load_words(file_path)
|
| 59 |
+
|
| 60 |
+
# Gradio interface
|
| 61 |
+
def flashcard_game(day):
|
| 62 |
+
day = int(day)
|
| 63 |
+
message, shuffled_words = get_flashcards(day, word_data)
|
| 64 |
+
current_index = 0
|
| 65 |
+
return message, "", "", "", current_index, shuffled_words, "Progress: Start"
|
| 66 |
+
|
| 67 |
+
with gr.Blocks(css=".main-container { max-width: 900px; margin: auto; }") as app:
|
| 68 |
+
gr.Markdown("""
|
| 69 |
+
<div style="text-align: center;">
|
| 70 |
+
<h1 style="color: #FF6347;">🌟 Keetawlingo 🌟</h1>
|
| 71 |
+
<p style="font-size: 18px;">Master English with the Oxford 3000 EN-TH Vocabulary!</p>
|
| 72 |
+
</div>
|
| 73 |
+
""")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column(scale=2):
|
| 77 |
+
gr.Markdown("### Word Card")
|
| 78 |
+
card_output = gr.HTML(label="Word Card")
|
| 79 |
+
translation_output = gr.HTML(label="Translation")
|
| 80 |
+
example_output = gr.HTML(label="Example Sentence")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
with gr.Column(scale=1):
|
| 84 |
+
gr.Markdown("### Select a day to start")
|
| 85 |
+
day_dropdown = gr.Dropdown(
|
| 86 |
+
label="Select Day",
|
| 87 |
+
choices=[str(i) for i in range(1, 101)],
|
| 88 |
+
value="1",
|
| 89 |
+
interactive=True
|
| 90 |
+
)
|
| 91 |
+
day_output = gr.Textbox(label="Message", interactive=False)
|
| 92 |
+
progress_output = gr.Textbox(label="Progress", interactive=False)
|
| 93 |
+
|
| 94 |
+
with gr.Column(scale=2):
|
| 95 |
+
gr.Markdown("### Controls")
|
| 96 |
+
next_card_btn = gr.Button("➡️ Next Card")
|
| 97 |
+
prev_card_btn = gr.Button("⬅️ Previous Card")
|
| 98 |
+
show_translation_btn = gr.Button("📖 Show Translation")
|
| 99 |
+
example_btn = gr.Button("📘 Show Example Sentence")
|
| 100 |
+
|
| 101 |
+
# Hidden states
|
| 102 |
+
current_index = gr.State(0)
|
| 103 |
+
word_list = gr.State(None)
|
| 104 |
+
|
| 105 |
+
# Actions
|
| 106 |
+
day_dropdown.change(
|
| 107 |
+
fn=flashcard_game,
|
| 108 |
+
inputs=[day_dropdown],
|
| 109 |
+
outputs=[day_output, card_output, translation_output, example_output, current_index, word_list, progress_output]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
next_card_btn.click(
|
| 113 |
+
fn=show_next_card,
|
| 114 |
+
inputs=[current_index, word_list],
|
| 115 |
+
outputs=[card_output, translation_output, example_output, current_index, progress_output]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
prev_card_btn.click(
|
| 119 |
+
fn=show_previous_card,
|
| 120 |
+
inputs=[current_index, word_list],
|
| 121 |
+
outputs=[card_output, translation_output, example_output, current_index, progress_output]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
show_translation_btn.click(
|
| 125 |
+
fn=show_translation,
|
| 126 |
+
inputs=[current_index, word_list],
|
| 127 |
+
outputs=[translation_output]
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
example_btn.click(
|
| 131 |
+
fn=show_example,
|
| 132 |
+
inputs=[current_index, word_list],
|
| 133 |
+
outputs=[example_output]
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
app.launch()
|
words_divided.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|