Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gtts import gTTS
|
3 |
+
from pydub import AudioSegment
|
4 |
+
import pandas as pd
|
5 |
+
import io
|
6 |
+
|
7 |
+
csv_files = {"A1":"OF3KA1.csv","A2":"OF3KA2.csv","B1":"OF3KB1.csv","B2":"OF3KB2.csv","C1":"OF3KC1.csv","5K":"OF5K.csv"}
|
8 |
+
|
9 |
+
# Function to load the DataFrame based on level selection
|
10 |
+
def load_data(level):
|
11 |
+
# Use the csv_files dictionary to get the correct file name for the given level
|
12 |
+
csv_file_path = f"./{csv_files[level]}" # This correctly uses the dictionary
|
13 |
+
data = pd.read_csv(csv_file_path)
|
14 |
+
return data
|
15 |
+
|
16 |
+
def generate_speech(level, x, y):
|
17 |
+
data = load_data(level) # Load data for the selected level
|
18 |
+
|
19 |
+
# Ensure x and y are integers and within range
|
20 |
+
x, y = int(x), int(y)
|
21 |
+
filtered_df = data[(data['SID'] >= x) & (data['SID'] <= y)]
|
22 |
+
|
23 |
+
combined_audio = AudioSegment.silent(duration=1000) # Start with silence for padding
|
24 |
+
|
25 |
+
for _, row in filtered_df.iterrows():
|
26 |
+
sentence = f"Number {row['SID']}. {row['WORD']}! {row['WORD']} is {row['POS']}."
|
27 |
+
tts = gTTS(text=sentence, lang='en')
|
28 |
+
mp3_fp = io.BytesIO()
|
29 |
+
tts.write_to_fp(mp3_fp)
|
30 |
+
mp3_fp.seek(0)
|
31 |
+
sentence_audio = AudioSegment.from_file(mp3_fp, format="mp3")
|
32 |
+
combined_audio += sentence_audio + AudioSegment.silent(duration=1500)
|
33 |
+
|
34 |
+
mp3_io = io.BytesIO()
|
35 |
+
combined_audio.export(mp3_io, format='mp3')
|
36 |
+
mp3_io.seek(0)
|
37 |
+
return mp3_io.getvalue()
|
38 |
+
|
39 |
+
# Interface with updated level selection including all specified options
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=generate_speech,
|
42 |
+
inputs=[
|
43 |
+
gr.Dropdown(label="Select Level (3K: A1, A2, B1, B2, C1; 5K: additional B2 and C1)", choices=['A1', 'A2', 'B1', 'B2', 'C1', '5K']), # Updated choices
|
44 |
+
gr.Number(label="Start Number (x)"),
|
45 |
+
gr.Number(label="End Number (y)")
|
46 |
+
],
|
47 |
+
outputs=gr.Audio(label="Generated Speech"),
|
48 |
+
title="Oxford Learner Vocabulary by CEFR levels: Learn with Sound",
|
49 |
+
description="Choose a level and define the starting and ending numbers for that level. The system will create a single audio file formatted as 'Number 1. Word is a noun.' After submission, you have the option to download the audio file. Additionally, you can download the numbered lists for each level from the 'My Apps' section at https://mrkim21.github.io (web address)"
|
50 |
+
)
|
51 |
+
|
52 |
+
iface.launch(share=True, debug=True)
|