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()