Spaces:
Configuration error
Configuration error
File size: 2,485 Bytes
5dad3a7 8f02cf0 5dad3a7 03b21bd 5dad3a7 a2ef783 8f02cf0 5dad3a7 03b21bd 5dad3a7 03b21bd 5dad3a7 8f02cf0 b0a7c73 8f02cf0 03b21bd 5dad3a7 8f02cf0 03b21bd b0a7c73 8f02cf0 03b21bd b0a7c73 8f02cf0 03b21bd 5dad3a7 5d417aa 5dad3a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# Install dependencies
!pip install gTTS python-docx
!apt-get install -y ffmpeg
# Imports
from gtts import gTTS
from IPython.display import Audio, display, HTML, clear_output
import os
from google.colab import files
import tempfile
import docx
import ipywidgets as widgets
# Global state
lines = []
current_speed = 1.0
# Function to extract text from docx
def get_text_from_docx(file_path):
doc = docx.Document(file_path)
text = []
for paragraph in doc.paragraphs:
text.append(paragraph.text)
return '\n'.join(text)
# Function to create and play TTS audio
def speak_urdu(text, speed=1.0):
tts = gTTS(text=text, lang='ur')
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
tts.save(tmp_file.name)
audio_path = tmp_file.name
# Adjust speed using ffmpeg
speed_str = str(speed)
adjusted_audio = audio_path.replace(".mp3", "_fast.mp3")
os.system(f"ffmpeg -i {audio_path} -filter:a \"atempo={speed_str}\" -vn {adjusted_audio} -y -loglevel quiet")
# Play the audio
display(Audio(adjusted_audio, autoplay=True))
# Upload .docx file using Colab method
uploaded = files.upload()
filename = next(iter(uploaded))
# Process the uploaded .docx file
with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp_file:
tmp_file.write(uploaded[filename])
tmp_file_path = tmp_file.name
text = get_text_from_docx(tmp_file_path)
lines = [line.strip() for line in text.split('\n') if line.strip()]
print("โ
File uploaded and processed successfully!")
# Speed slider
speed_slider = widgets.FloatSlider(
value=1.0,
min=0.5,
max=1.5,
step=0.1,
description='Speed:',
continuous_update=False
)
def on_speed_change(change):
global current_speed
current_speed = change['new']
speed_slider.observe(on_speed_change, names='value')
display(speed_slider)
# Function to read the text aloud
def display_line(line, idx):
styled_line = f"<h3 style='font-family: sans-serif; color: #333;'>๐ Line {idx + 1}: {line}</h3>"
display(HTML(styled_line))
def read_text(_=None):
clear_output(wait=True)
display(speed_slider)
display(read_button)
for idx, line in enumerate(lines):
display_line(line, idx)
speak_urdu(line, speed=current_speed)
input("๐ธ Press Enter to read next line...")
# Read button
read_button = widgets.Button(description="๐ Read Text")
read_button.on_click(read_text)
display(read_button)
|