Spaces:
Build error
Build error
File size: 5,012 Bytes
4359927 | 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import gradio as gr
import pandas as pd
import numpy as np
import json
import datetime
from gtts import gTTS
import os
import random
# Data storage
VOCAB_FILE = "vocabulary.json"
PROGRESS_FILE = "progress.json"
# Initialize data
if os.path.exists(VOCAB_FILE):
with open(VOCAB_FILE, 'r') as f:
vocabulary = json.load(f)
else:
vocabulary = {}
if os.path.exists(PROGRESS_FILE):
with open(PROGRESS_FILE, 'r') as f:
progress = json.load(f)
else:
progress = {
'studied_words': [],
'correct_counts': {},
'incorrect_counts': {},
'study_time': {},
'daily_goals': {},
'achievements': []
}
def save_vocab():
with open(VOCAB_FILE, 'w') as f:
json.dump(vocabulary, f)
def save_progress():
with open(PROGRESS_FILE, 'w') as f:
json.dump(progress, f)
def import_csv(file):
df = pd.read_csv(file.name)
for _, row in df.iterrows():
word = row['word']
meaning = row['meaning']
category = row.get('category', 'General')
difficulty = row.get('difficulty', 'Medium')
vocabulary[word] = {
'meaning': meaning,
'category': category,
'difficulty': difficulty,
'example': row.get('example', ''),
'date_added': datetime.datetime.now().strftime('%Y-%m-%d')
}
save_vocab()
return "Vocabulary imported successfully!"
def add_word(word, meaning, category, difficulty, example):
vocabulary[word] = {
'meaning': meaning,
'category': category,
'difficulty': difficulty,
'example': example,
'date_added': datetime.datetime.now().strftime('%Y-%m-%d')
}
save_vocab()
return f"Word '{word}' added successfully!"
def get_random_word():
if not vocabulary:
return None
return random.choice(list(vocabulary.keys()))
def check_answer(word, user_answer):
correct = vocabulary[word]['meaning'].lower() == user_answer.lower()
if correct:
progress['correct_counts'][word] = progress['correct_counts'].get(word, 0) + 1
else:
progress['incorrect_counts'][word] = progress['incorrect_counts'].get(word, 0) + 1
progress['studied_words'].append(word)
save_progress()
return "Correct!" if correct else f"Wrong! The correct answer is: {vocabulary[word]['meaning']}"
def get_stats():
total_words = len(vocabulary)
studied_words = len(set(progress['studied_words']))
if not studied_words:
return "No study data available yet."
total_correct = sum(progress['correct_counts'].values())
total_attempts = total_correct + sum(progress['incorrect_counts'].values())
accuracy = (total_correct / total_attempts * 100) if total_attempts > 0 else 0
return f"""
Total Words: {total_words}
Studied Words: {studied_words}
Accuracy: {accuracy:.1f}%
"""
def text_to_speech(word):
tts = gTTS(text=word, lang='en')
tts.save('temp.mp3')
return 'temp.mp3'
with gr.Blocks(theme=gr.themes.Soft()) as app:
gr.Markdown("# Vocabulary Learning System")
with gr.Tab("Import/Add Words"):
with gr.Row():
file_input = gr.File(label="Import CSV")
import_btn = gr.Button("Import")
with gr.Row():
word_input = gr.Textbox(label="Word")
meaning_input = gr.Textbox(label="Meaning")
category_input = gr.Dropdown(choices=["General", "Business", "Academic", "TOEFL", "IELTS"], label="Category")
difficulty_input = gr.Dropdown(choices=["Easy", "Medium", "Hard"], label="Difficulty")
example_input = gr.Textbox(label="Example Sentence")
add_btn = gr.Button("Add Word")
result_text = gr.Textbox(label="Result")
with gr.Tab("Study Flashcards"):
with gr.Row():
current_word = gr.Textbox(label="Word")
audio_btn = gr.Button("🔊 Listen")
audio_output = gr.Audio(label="Pronunciation")
answer_input = gr.Textbox(label="Enter meaning")
check_btn = gr.Button("Check Answer")
next_btn = gr.Button("Next Word")
result_study = gr.Textbox(label="Result")
with gr.Tab("Progress"):
stats_text = gr.Textbox(label="Statistics")
refresh_btn = gr.Button("Refresh Stats")
# Event handlers
import_btn.click(import_csv, inputs=[file_input], outputs=[result_text])
add_btn.click(add_word, inputs=[word_input, meaning_input, category_input, difficulty_input, example_input], outputs=[result_text])
def update_word():
word = get_random_word()
if word:
return word
return "No words available"
next_btn.click(update_word, outputs=[current_word])
check_btn.click(check_answer, inputs=[current_word, answer_input], outputs=[result_study])
audio_btn.click(text_to_speech, inputs=[current_word], outputs=[audio_output])
refresh_btn.click(get_stats, outputs=[stats_text])
app.launch()
if __name__ == '__main__':
demo.launch() |