Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,106 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from gtts import gTTS
|
| 3 |
-
import
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
import docx
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
# Function to
|
| 14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
tts = gTTS(text=text, lang='ur')
|
| 16 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as
|
| 17 |
-
tts.save(
|
| 18 |
-
audio_path =
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
final_path = audio_path.replace(".mp3", f"_x{speed}.mp3")
|
| 24 |
-
sound.export(final_path, format="mp3")
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
else:
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 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 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install required packages
|
| 2 |
+
!pip install gTTS python-docx
|
| 3 |
+
!apt-get install -y ffmpeg
|
| 4 |
+
|
| 5 |
+
# Imports
|
| 6 |
from gtts import gTTS
|
| 7 |
+
from IPython.display import Audio, display, HTML, clear_output
|
| 8 |
import os
|
| 9 |
+
from google.colab import files
|
| 10 |
+
import tempfile
|
| 11 |
import docx
|
| 12 |
+
import ipywidgets as widgets
|
| 13 |
|
| 14 |
+
# Global variables
|
| 15 |
+
lines = []
|
| 16 |
+
current_speed = 1.0
|
| 17 |
+
current_index = 0
|
| 18 |
|
| 19 |
+
# Function to extract text from .docx
|
| 20 |
+
def get_text_from_docx(file_path):
|
| 21 |
+
doc = docx.Document(file_path)
|
| 22 |
+
text = []
|
| 23 |
+
for paragraph in doc.paragraphs:
|
| 24 |
+
text.append(paragraph.text)
|
| 25 |
+
return '\n'.join(text)
|
| 26 |
+
|
| 27 |
+
# Function to speak Urdu using gTTS and adjust speed
|
| 28 |
+
def speak_urdu(text, speed=1.0):
|
| 29 |
tts = gTTS(text=text, lang='ur')
|
| 30 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 31 |
+
tts.save(tmp_file.name)
|
| 32 |
+
audio_path = tmp_file.name
|
| 33 |
|
| 34 |
+
speed_str = str(speed)
|
| 35 |
+
adjusted_audio = audio_path.replace(".mp3", "_fast.mp3")
|
| 36 |
+
os.system(f"ffmpeg -i {audio_path} -filter:a \"atempo={speed_str}\" -vn {adjusted_audio} -y -loglevel quiet")
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
display(Audio(adjusted_audio, autoplay=True))
|
| 39 |
|
| 40 |
+
# Function to display Urdu text in Nastaleeq font
|
| 41 |
+
def display_line(line, idx):
|
| 42 |
+
font_css = """
|
| 43 |
+
<style>
|
| 44 |
+
@import url('https://fonts.googleapis.com/earlyaccess/notonastaliqurdu.css');
|
| 45 |
+
.nastaleeq {
|
| 46 |
+
font-family: 'Noto Nastaliq Urdu', serif;
|
| 47 |
+
font-size: 24px;
|
| 48 |
+
color: #222;
|
| 49 |
+
direction: rtl;
|
| 50 |
+
text-align: right;
|
| 51 |
+
line-height: 1.8;
|
| 52 |
+
margin: 10px 0;
|
| 53 |
+
}
|
| 54 |
+
</style>
|
| 55 |
+
"""
|
| 56 |
+
styled_line = f"{font_css}<div class='nastaleeq'>📖 لائن {idx + 1}: {line}</div>"
|
| 57 |
+
display(HTML(styled_line))
|
| 58 |
|
| 59 |
+
# Callback to read next line
|
| 60 |
+
def read_next_line(_):
|
| 61 |
+
global current_index
|
| 62 |
+
clear_output(wait=True)
|
| 63 |
+
display(speed_slider)
|
| 64 |
+
display(read_button)
|
| 65 |
+
if current_index < len(lines):
|
| 66 |
+
display_line(lines[current_index], current_index)
|
| 67 |
+
speak_urdu(lines[current_index], speed=current_speed)
|
| 68 |
+
current_index += 1
|
| 69 |
else:
|
| 70 |
+
display(HTML("<h3 style='color: green;'>🎉 تمام لائنیں پڑھ لی گئیں!</h3>"))
|
| 71 |
+
|
| 72 |
+
# Handle speed change
|
| 73 |
+
def on_speed_change(change):
|
| 74 |
+
global current_speed
|
| 75 |
+
current_speed = change['new']
|
| 76 |
|
| 77 |
+
# Upload .docx file using Colab
|
| 78 |
+
uploaded = files.upload()
|
| 79 |
+
filename = next(iter(uploaded))
|
| 80 |
|
| 81 |
+
# Save to temp and process
|
| 82 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp_file:
|
| 83 |
+
tmp_file.write(uploaded[filename])
|
| 84 |
+
tmp_file_path = tmp_file.name
|
| 85 |
|
| 86 |
+
text = get_text_from_docx(tmp_file_path)
|
| 87 |
+
lines = [line.strip() for line in text.split('\n') if line.strip()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
print("✅ فائل کامیابی سے اپ لوڈ اور پروسیس ہو گئی!")
|
| 90 |
|
| 91 |
+
# Speed control slider
|
| 92 |
+
speed_slider = widgets.FloatSlider(
|
| 93 |
+
value=1.0,
|
| 94 |
+
min=0.5,
|
| 95 |
+
max=1.5,
|
| 96 |
+
step=0.1,
|
| 97 |
+
description='رف��ار:',
|
| 98 |
+
continuous_update=False
|
| 99 |
+
)
|
| 100 |
+
speed_slider.observe(on_speed_change, names='value')
|
| 101 |
+
display(speed_slider)
|
| 102 |
|
| 103 |
+
# Read button
|
| 104 |
+
read_button = widgets.Button(description="▶️ اگلی لائن پڑھیں")
|
| 105 |
+
read_button.on_click(read_next_line)
|
| 106 |
+
display(read_button)
|