Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,51 @@
|
|
| 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 |
-
with gr.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
lesson.change(get_vocab_table, inputs=lesson, outputs=vocab_table)
|
| 53 |
-
lesson.change(get_grammar_notes, inputs=lesson, outputs=grammar_notes)
|
| 54 |
-
pronounce_btn.click(play_pronunciation, inputs=word_input, outputs=[word_output, audio_output])
|
| 55 |
-
flash_btn.click(random_flashcard, inputs=lesson, outputs=[flash_text, flash_audio])
|
| 56 |
-
|
| 57 |
-
with gr.Tab("Quiz"):
|
| 58 |
-
start_btn = gr.Button("Start Quiz")
|
| 59 |
-
question = gr.Textbox(label="Question")
|
| 60 |
-
options = gr.Radio(label="Options")
|
| 61 |
-
submit_btn = gr.Button("Submit Answer")
|
| 62 |
-
result_box = gr.Textbox(label="Result")
|
| 63 |
-
|
| 64 |
-
qidx = gr.State(0)
|
| 65 |
-
score = gr.State(0)
|
| 66 |
-
answers = gr.State([])
|
| 67 |
-
|
| 68 |
-
start_btn.click(start_quiz, outputs=[question, options, qidx, score, answers])
|
| 69 |
-
submit_btn.click(next_question, inputs=[qidx, score, answers, options], outputs=[question, options, result_box, qidx, score, answers])
|
| 70 |
-
|
| 71 |
-
with gr.Tab("Listening Practice"):
|
| 72 |
-
sentence = gr.Dropdown(["Ich heiße Hidayat.", "Ich komme aus Pakistan.", "Wie geht es dir?"], label="Select a sentence")
|
| 73 |
-
listen_btn = gr.Button("Play")
|
| 74 |
-
audio_sent = gr.Audio(label="Audio", type="filepath")
|
| 75 |
-
|
| 76 |
-
def play_sentence(sent):
|
| 77 |
-
audio = tts_audio_bytes(sent)
|
| 78 |
-
return audio
|
| 79 |
-
|
| 80 |
-
listen_btn.click(play_sentence, inputs=sentence, outputs=audio_sent)
|
| 81 |
-
|
| 82 |
-
gr.Markdown("---")
|
| 83 |
-
gr.Markdown("Made by Dr. Hidayatullah Mahar — German Learning Hub on Hugging Face 💡")
|
| 84 |
-
|
| 85 |
-
if name == "main": demo.launch()
|
| 86 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Simple German-English learning dataset
|
| 6 |
+
phrases = {
|
| 7 |
+
"Hello": "Hallo",
|
| 8 |
+
"Good Morning": "Guten Morgen",
|
| 9 |
+
"Thank you": "Danke",
|
| 10 |
+
"Good Night": "Gute Nacht",
|
| 11 |
+
"How are you?": "Wie geht es dir?",
|
| 12 |
+
"I love Germany": "Ich liebe Deutschland",
|
| 13 |
+
"What is your name?": "Wie heißt du?",
|
| 14 |
+
"Goodbye": "Auf Wiedersehen",
|
| 15 |
+
"Yes": "Ja",
|
| 16 |
+
"No": "Nein"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Function to get translation
|
| 20 |
+
def translate_to_german(english_text):
|
| 21 |
+
if english_text in phrases:
|
| 22 |
+
german = phrases[english_text]
|
| 23 |
+
return german
|
| 24 |
+
else:
|
| 25 |
+
return "Not found in dictionary."
|
| 26 |
+
|
| 27 |
+
# Function to speak German
|
| 28 |
+
def speak_german(text):
|
| 29 |
+
german = translate_to_german(text)
|
| 30 |
+
if german != "Not found in dictionary.":
|
| 31 |
+
tts = gTTS(german, lang="de")
|
| 32 |
+
audio_file = "german_audio.mp3"
|
| 33 |
+
tts.save(audio_file)
|
| 34 |
+
return german, audio_file
|
| 35 |
+
else:
|
| 36 |
+
return german, None
|
| 37 |
+
|
| 38 |
+
# UI Layout
|
| 39 |
+
with gr.Blocks(title="🇩🇪 Learn German with Dr. Hidayatullah Mahar") as demo:
|
| 40 |
+
gr.Markdown("## 🇩🇪 Learn German\n### Translate common English phrases into German and hear pronunciation.")
|
| 41 |
+
|
| 42 |
+
english_input = gr.Dropdown(choices=list(phrases.keys()), label="Select English Phrase")
|
| 43 |
+
translate_btn = gr.Button("Translate & Speak")
|
| 44 |
+
output_text = gr.Textbox(label="German Translation")
|
| 45 |
+
output_audio = gr.Audio(label="German Pronunciation")
|
| 46 |
+
|
| 47 |
+
translate_btn.click(fn=speak_german, inputs=english_input, outputs=[output_text, output_audio])
|
| 48 |
+
|
| 49 |
+
gr.Markdown("**Developed by Dr. Hidayatullah Mahar**")
|
| 50 |
+
|
| 51 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|