Spaces:
Configuration error
Configuration error
| # 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) | |