import gradio as gr from deep_translator import GoogleTranslator from gtts import gTTS import tempfile import random # ========================= # TRANSLATOR # ========================= def translate_and_speak(text, target_language): if not text or not text.strip(): return "", None try: translated = GoogleTranslator( source="auto", target=target_language ).translate(text) audio_file = tempfile.NamedTemporaryFile( delete=False, suffix=".mp3" ) audio_path = audio_file.name audio_file.close() gTTS( text=translated, lang=target_language ).save(audio_path) return translated, audio_path except Exception as e: return f"Error: {e}", None # ========================= # QUIZ GENERATOR # ========================= def generate_quiz(topic, difficulty, num_questions): if not topic or not topic.strip(): return "Please enter a topic.", "" try: num_questions = int(num_questions) # Sample question bank quiz_bank = { "Science": [ { "question": "Which planet is known as the Red Planet?", "options": ["Earth", "Mars", "Jupiter", "Venus"], "answer": "Mars", "explanation": "Mars appears reddish because of iron oxide on its surface." }, { "question": "What gas do plants mainly absorb during photosynthesis?", "options": ["Oxygen", "Nitrogen", "Carbon dioxide", "Hydrogen"], "answer": "Carbon dioxide", "explanation": "Plants use carbon dioxide, water and sunlight to make food." }, { "question": "What is H2O commonly known as?", "options": ["Oxygen", "Water", "Hydrogen", "Salt"], "answer": "Water", "explanation": "H2O consists of two hydrogen atoms and one oxygen atom." }, { "question": "Which organ pumps blood around the human body?", "options": ["Brain", "Lungs", "Heart", "Kidney"], "answer": "Heart", "explanation": "The heart pumps blood through the circulatory system." } ], "Mathematics": [ { "question": "What is 12 × 8?", "options": ["86", "96", "108", "88"], "answer": "96", "explanation": "12 multiplied by 8 equals 96." }, { "question": "What is the square root of 144?", "options": ["10", "11", "12", "14"], "answer": "12", "explanation": "12 × 12 = 144." }, { "question": "What is 25% of 200?", "options": ["25", "40", "50", "75"], "answer": "50", "explanation": "25% means one quarter, and one quarter of 200 is 50." }, { "question": "What is the value of 7²?", "options": ["14", "21", "49", "77"], "answer": "49", "explanation": "7² means 7 × 7, which equals 49." } ], "History": [ { "question": "Who was known as the Father of the Indian Constitution?", "options": [ "Mahatma Gandhi", "B. R. Ambedkar", "Jawaharlal Nehru", "Sardar Patel" ], "answer": "B. R. Ambedkar", "explanation": "Dr. B. R. Ambedkar chaired the Drafting Committee of the Indian Constitution." }, { "question": "Who was the first Prime Minister of India?", "options": [ "Mahatma Gandhi", "Jawaharlal Nehru", "Rajendra Prasad", "Sardar Patel" ], "answer": "Jawaharlal Nehru", "explanation": "Jawaharlal Nehru served as India's first Prime Minister." }, { "question": "India became independent in which year?", "options": ["1945", "1946", "1947", "1950"], "answer": "1947", "explanation": "India became independent on 15 August 1947." } ], "Geography": [ { "question": "What is the largest continent?", "options": ["Africa", "Asia", "Europe", "Australia"], "answer": "Asia", "explanation": "Asia is the largest continent by both area and population." }, { "question": "Which is the longest river in India?", "options": ["Yamuna", "Ganga", "Godavari", "Narmada"], "answer": "Ganga", "explanation": "The Ganga is the longest river system within India." }, { "question": "Which ocean lies south of India?", "options": [ "Atlantic Ocean", "Pacific Ocean", "Indian Ocean", "Arctic Ocean" ], "answer": "Indian Ocean", "explanation": "India is bordered by the Indian Ocean to the south." } ] } # Find a matching topic selected_topic = None for key in quiz_bank: if key.lower() in topic.lower() or topic.lower() in key.lower(): selected_topic = key break # If topic doesn't match, use Science as fallback if selected_topic is None: selected_topic = "Science" questions = quiz_bank[selected_topic].copy() # Shuffle questions random.shuffle(questions) # Limit number of questions questions = questions[:min(num_questions, len(questions))] quiz_output = f"# 📝 {selected_topic} Quiz\n" quiz_output += f"**Difficulty:** {difficulty}\n\n" answer_output = "# ✅ Answer Key\n\n" for i, q in enumerate(questions, 1): quiz_output += f"### Question {i}\n" quiz_output += f"**{q['question']}**\n\n" for letter, option in zip( ["A", "B", "C", "D"], q["options"] ): quiz_output += f"- **{letter}.** {option}\n" quiz_output += "\n" answer_output += ( f"**{i}. {q['answer']}**\n" f"Explanation: {q['explanation']}\n\n" ) return quiz_output, answer_output except Exception as e: return f"Error: {e}", "" # ========================= # GRADIO APP # ========================= with gr.Blocks(title="EduVoice Translator & Quiz") as app: gr.Markdown(""" # 🎓 EduVoice Learning App ### 🌐 Translate → 🔊 Listen → 📝 Generate Quizzes """) # ========================= # TRANSLATOR TAB # ========================= with gr.Tab("🌐 Translator"): text = gr.Textbox( label="Write your sentence", placeholder="Start typing...", lines=4 ) # LANGUAGE LIST # English = en # German = de language = gr.Dropdown( choices=[ ("English", "en"), ("German", "de"), ("Tamil", "ta"), ("Hindi", "hi"), ("Malayalam", "ml"), ("Telugu", "te"), ("Kannada", "kn"), ("French", "fr"), ("Spanish", "es") ], value="en", label="Translate to" ) translated = gr.Textbox( label="Translation", lines=4 ) voice = gr.Audio( autoplay=True, visible=True, label="🔊 Voice" ) text.input( fn=translate_and_speak, inputs=[text, language], outputs=[translated, voice] ) language.change( fn=translate_and_speak, inputs=[text, language], outputs=[translated, voice] ) # ========================= # QUIZ GENERATOR TAB # ========================= with gr.Tab("📝 Quiz Generator"): gr.Markdown(""" ## 🧠 Create a Quiz Enter a subject and generate multiple-choice questions. """) topic = gr.Textbox( label="📚 Topic", placeholder="Example: Science, Mathematics, History, Geography" ) difficulty = gr.Dropdown( choices=[ "Easy", "Medium", "Hard" ], value="Medium", label="🎯 Difficulty" ) num_questions = gr.Slider( minimum=1, maximum=10, value=4, step=1, label="🔢 Number of Questions" ) generate_button = gr.Button( "✨ Generate Quiz", variant="primary" ) quiz = gr.Markdown( label="📝 Quiz" ) answers = gr.Markdown( label="✅ Answers" ) generate_button.click( fn=generate_quiz, inputs=[ topic, difficulty, num_questions ], outputs=[ quiz, answers ] ) # ========================= # LAUNCH # ========================= app.launch()