File size: 1,731 Bytes
38e26b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import gradio as gr
from gtts import gTTS
import tempfile
import os

# Step 1: Read the CSV file
csv_url = "https://raw.githubusercontent.com/MK316/241214/refs/heads/main/data/plural_sample.csv"
df = pd.read_csv(csv_url)

# Initialize state
state = {"index": 0, "completed": False}

# Function to generate text and audio for the current noun
def generate_practice():
    if state["completed"]:
        return "You've completed the practice.", None

    # Get the current noun data
    current_noun = df.iloc[state["index"]]
    noun_text = (
        f"Repeat after me: the plural form of {current_noun['Singular']} is {current_noun['Plural']}."
    )

    # Generate audio using gTTS
    tts = gTTS(noun_text)
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
    tts.save(temp_file.name)

    return noun_text, temp_file.name

# Function to move to the next noun
def next_noun():
    if state["index"] < len(df) - 1:
        state["index"] += 1
    else:
        state["completed"] = True

    return generate_practice()

# Gradio interface
with gr.Blocks() as app:
    gr.Markdown("### Noun Practice")
    gr.Markdown("Click **Start** to begin practicing. Repeat after the audio and click **Next** to proceed to the next noun.")

    # Start button
    start_button = gr.Button("Start")
    text_display = gr.Textbox(label="Practice Text", interactive=False)
    audio_output = gr.Audio(label="Audio", interactive=False)

    # Next button
    next_button = gr.Button("Next")

    # Button actions
    start_button.click(fn=generate_practice, outputs=[text_display, audio_output])
    next_button.click(fn=next_noun, outputs=[text_display, audio_output])

# Launch the app
app.launch()