Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
| 3 |
-
import os
|
| 4 |
import tempfile
|
| 5 |
-
|
| 6 |
-
from pydub.playback import play
|
| 7 |
import docx
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
line_index = 0
|
| 11 |
-
all_lines = []
|
| 12 |
-
|
| 13 |
-
# Function to extract text from an uploaded .docx file
|
| 14 |
def extract_text_from_docx(file):
|
| 15 |
-
doc = docx.Document(file)
|
| 16 |
return [para.text.strip() for para in doc.paragraphs if para.text.strip()]
|
| 17 |
|
| 18 |
-
# Function to
|
| 19 |
-
def
|
| 20 |
tts = gTTS(text=text, lang='ur')
|
| 21 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as
|
| 22 |
-
tts.save(
|
| 23 |
-
audio_path =
|
| 24 |
|
| 25 |
-
#
|
| 26 |
sound = AudioSegment.from_file(audio_path)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
}).set_frame_rate(sound.frame_rate)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
altered_sound.export(adjusted_path, format="mp3")
|
| 34 |
-
return adjusted_path
|
| 35 |
|
| 36 |
-
#
|
| 37 |
def start_reader(file, speed):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
all_lines = extract_text_from_docx(file)
|
| 41 |
-
if all_lines:
|
| 42 |
-
text = all_lines[0]
|
| 43 |
-
audio_path = generate_tts_audio(text, speed)
|
| 44 |
-
return text, audio_path, f"کل {len(all_lines)} لائنیں ملی ہیں۔", gr.update(visible=True)
|
| 45 |
-
else:
|
| 46 |
-
return "", None, "فائل میں کوئی اردو مواد نہیں ملا۔", gr.update(visible=False)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
line_index += 1
|
| 53 |
-
text = all_lines[line_index]
|
| 54 |
-
audio_path = generate_tts_audio(text, speed)
|
| 55 |
-
return text, audio_path, f"لائن {line_index + 1} / {len(all_lines)}", gr.update(visible=True)
|
| 56 |
else:
|
| 57 |
-
return "
|
| 58 |
|
| 59 |
-
# UI
|
| 60 |
-
with gr.Blocks(
|
| 61 |
-
gr.Markdown("##
|
| 62 |
-
gr.Markdown("`ایک Urdu .docx فائل اپلوڈ کریں، رفتار منتخب کریں، اور باری باری ہر لائن کو سنیں`")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
audio_output = gr.Audio(label="🔈 آڈیو", type="filepath", autoplay=True)
|
| 72 |
-
status = gr.Textbox(label="ℹ️ معلومات", interactive=False)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
|
|
|
| 3 |
import tempfile
|
| 4 |
+
import os
|
|
|
|
| 5 |
import docx
|
| 6 |
+
from pydub import AudioSegment
|
| 7 |
|
| 8 |
+
# Function to extract text from docx
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def extract_text_from_docx(file):
|
| 10 |
+
doc = docx.Document(file.name)
|
| 11 |
return [para.text.strip() for para in doc.paragraphs if para.text.strip()]
|
| 12 |
|
| 13 |
+
# Function to convert Urdu text to speech
|
| 14 |
+
def urdu_tts(text, speed):
|
| 15 |
tts = gTTS(text=text, lang='ur')
|
| 16 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
| 17 |
+
tts.save(tmp.name)
|
| 18 |
+
audio_path = tmp.name
|
| 19 |
|
| 20 |
+
# Change speed using pydub
|
| 21 |
sound = AudioSegment.from_file(audio_path)
|
| 22 |
+
sound = sound.speedup(playback_speed=speed)
|
| 23 |
+
final_path = audio_path.replace(".mp3", f"_x{speed}.mp3")
|
| 24 |
+
sound.export(final_path, format="mp3")
|
|
|
|
| 25 |
|
| 26 |
+
return final_path
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Gradio stateful interface
|
| 29 |
def start_reader(file, speed):
|
| 30 |
+
lines = extract_text_from_docx(file)
|
| 31 |
+
return lines, f"کل {len(lines)} لائنیں ملی ہیں۔", 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
def read_line(lines, line_idx, speed):
|
| 34 |
+
if line_idx < len(lines):
|
| 35 |
+
audio_file = urdu_tts(lines[line_idx], speed)
|
| 36 |
+
return lines[line_idx], audio_file, line_idx + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
else:
|
| 38 |
+
return "📘 تمام لائنیں پڑھ لی گئیں!", None, line_idx
|
| 39 |
|
| 40 |
+
# Gradio UI
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("## 📖 اردو لائن بائی لائن ریڈر")
|
|
|
|
| 43 |
|
| 44 |
+
with gr.Row():
|
| 45 |
+
file_input = gr.File(label="📁 .docx فائل اپ لوڈ کریں", file_types=['.docx'])
|
| 46 |
+
speed_slider = gr.Slider(0.5, 2.0, 1.0, step=0.1, label="🔊 رفتار")
|
| 47 |
|
| 48 |
+
start_button = gr.Button("🚀 فائل پڑھنا شروع کریں")
|
| 49 |
+
status = gr.Textbox(label="🔹 اسٹیٹس")
|
| 50 |
+
lines_state = gr.State()
|
| 51 |
+
index_state = gr.State()
|
| 52 |
+
|
| 53 |
+
current_line = gr.Textbox(label="📌 موجودہ لائن", lines=2)
|
| 54 |
+
audio_output = gr.Audio(label="🎧 آڈیو")
|
| 55 |
|
| 56 |
+
next_button = gr.Button("▶️ اگلی لائن پڑھیں")
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
start_button.click(fn=start_reader, inputs=[file_input, speed_slider],
|
| 59 |
+
outputs=[lines_state, status, index_state])
|
| 60 |
+
|
| 61 |
+
next_button.click(fn=read_line, inputs=[lines_state, index_state, speed_slider],
|
| 62 |
+
outputs=[current_line, audio_output, index_state])
|
| 63 |
|
| 64 |
demo.launch()
|