MK-316's picture
Create app.py
38e26b7 verified
raw
history blame
1.73 kB
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()