Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,42 @@
|
|
| 1 |
-
# Install
|
| 2 |
-
!pip install gTTS
|
| 3 |
!apt-get install -y ffmpeg
|
| 4 |
|
| 5 |
-
# Imports
|
| 6 |
from gtts import gTTS
|
| 7 |
-
from IPython.display import Audio, display
|
| 8 |
import os
|
| 9 |
from google.colab import files
|
| 10 |
import tempfile
|
| 11 |
-
import docx
|
| 12 |
-
import ipywidgets as widgets
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
current_speed = 1.0
|
| 17 |
-
current_index = 0
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
for paragraph in doc.paragraphs:
|
| 24 |
-
text.append(paragraph.text)
|
| 25 |
-
return '\n'.join(text)
|
| 26 |
|
| 27 |
-
# Function to
|
| 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 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
#
|
| 104 |
-
|
| 105 |
-
read_button.on_click(read_next_line)
|
| 106 |
-
display(read_button)
|
|
|
|
| 1 |
+
# Install necessary libraries
|
| 2 |
+
!pip install gTTS
|
| 3 |
!apt-get install -y ffmpeg
|
| 4 |
|
|
|
|
| 5 |
from gtts import gTTS
|
| 6 |
+
from IPython.display import Audio, display
|
| 7 |
import os
|
| 8 |
from google.colab import files
|
| 9 |
import tempfile
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Upload a file
|
| 12 |
+
uploaded = files.upload()
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Load Urdu text file
|
| 15 |
+
file_name = list(uploaded.keys())[0]
|
| 16 |
+
with open(file_name, 'r', encoding='utf-8') as f:
|
| 17 |
+
lines = [line.strip() for line in f if line.strip()]
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Function to create and play TTS audio
|
| 20 |
def speak_urdu(text, speed=1.0):
|
| 21 |
tts = gTTS(text=text, lang='ur')
|
| 22 |
+
|
| 23 |
+
# Save to temporary file
|
| 24 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 25 |
tts.save(tmp_file.name)
|
| 26 |
audio_path = tmp_file.name
|
| 27 |
|
| 28 |
+
# Adjust speed using ffmpeg
|
| 29 |
speed_str = str(speed)
|
| 30 |
adjusted_audio = audio_path.replace(".mp3", "_fast.mp3")
|
| 31 |
os.system(f"ffmpeg -i {audio_path} -filter:a \"atempo={speed_str}\" -vn {adjusted_audio} -y -loglevel quiet")
|
| 32 |
|
| 33 |
+
# Play the audio
|
| 34 |
display(Audio(adjusted_audio, autoplay=True))
|
| 35 |
|
| 36 |
+
# Speak each line
|
| 37 |
+
for idx, line in enumerate(lines):
|
| 38 |
+
print(f"\n📖 Line {idx + 1}: {line}")
|
| 39 |
+
speak_urdu(line, speed=1.1) # Set speed here (1.0 is normal, 0.9 is slower, 1.2 is faster)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Ask user to proceed (interactive)
|
| 42 |
+
input("Press Enter to continue to the next line...")
|
|
|
|
|
|