MK-316 commited on
Commit
38e26b7
·
verified ·
1 Parent(s): af49d56

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+ from gtts import gTTS
4
+ import tempfile
5
+ import os
6
+
7
+ # Step 1: Read the CSV file
8
+ csv_url = "https://raw.githubusercontent.com/MK316/241214/refs/heads/main/data/plural_sample.csv"
9
+ df = pd.read_csv(csv_url)
10
+
11
+ # Initialize state
12
+ state = {"index": 0, "completed": False}
13
+
14
+ # Function to generate text and audio for the current noun
15
+ def generate_practice():
16
+ if state["completed"]:
17
+ return "You've completed the practice.", None
18
+
19
+ # Get the current noun data
20
+ current_noun = df.iloc[state["index"]]
21
+ noun_text = (
22
+ f"Repeat after me: the plural form of {current_noun['Singular']} is {current_noun['Plural']}."
23
+ )
24
+
25
+ # Generate audio using gTTS
26
+ tts = gTTS(noun_text)
27
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
28
+ tts.save(temp_file.name)
29
+
30
+ return noun_text, temp_file.name
31
+
32
+ # Function to move to the next noun
33
+ def next_noun():
34
+ if state["index"] < len(df) - 1:
35
+ state["index"] += 1
36
+ else:
37
+ state["completed"] = True
38
+
39
+ return generate_practice()
40
+
41
+ # Gradio interface
42
+ with gr.Blocks() as app:
43
+ gr.Markdown("### Noun Practice")
44
+ gr.Markdown("Click **Start** to begin practicing. Repeat after the audio and click **Next** to proceed to the next noun.")
45
+
46
+ # Start button
47
+ start_button = gr.Button("Start")
48
+ text_display = gr.Textbox(label="Practice Text", interactive=False)
49
+ audio_output = gr.Audio(label="Audio", interactive=False)
50
+
51
+ # Next button
52
+ next_button = gr.Button("Next")
53
+
54
+ # Button actions
55
+ start_button.click(fn=generate_practice, outputs=[text_display, audio_output])
56
+ next_button.click(fn=next_noun, outputs=[text_display, audio_output])
57
+
58
+ # Launch the app
59
+ app.launch()