Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,32 @@
|
|
| 1 |
-
# Install
|
| 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 |
-
#
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -33,10 +39,52 @@ def speak_urdu(text, speed=1.0):
|
|
| 33 |
# Play the audio
|
| 34 |
display(Audio(adjusted_audio, autoplay=True))
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install dependencies
|
| 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 state
|
| 15 |
+
lines = []
|
| 16 |
+
current_speed = 1.0
|
| 17 |
|
| 18 |
+
# Function to extract text from docx
|
| 19 |
+
def get_text_from_docx(file_path):
|
| 20 |
+
doc = docx.Document(file_path)
|
| 21 |
+
text = []
|
| 22 |
+
for paragraph in doc.paragraphs:
|
| 23 |
+
text.append(paragraph.text)
|
| 24 |
+
return '\n'.join(text)
|
| 25 |
|
| 26 |
# Function to create and play TTS audio
|
| 27 |
def speak_urdu(text, speed=1.0):
|
| 28 |
tts = gTTS(text=text, lang='ur')
|
| 29 |
+
|
|
|
|
| 30 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 31 |
tts.save(tmp_file.name)
|
| 32 |
audio_path = tmp_file.name
|
|
|
|
| 39 |
# Play the audio
|
| 40 |
display(Audio(adjusted_audio, autoplay=True))
|
| 41 |
|
| 42 |
+
# Upload .docx file using Colab method
|
| 43 |
+
uploaded = files.upload()
|
| 44 |
+
filename = next(iter(uploaded))
|
| 45 |
+
|
| 46 |
+
# Process the uploaded .docx file
|
| 47 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp_file:
|
| 48 |
+
tmp_file.write(uploaded[filename])
|
| 49 |
+
tmp_file_path = tmp_file.name
|
| 50 |
+
|
| 51 |
+
text = get_text_from_docx(tmp_file_path)
|
| 52 |
+
lines = [line.strip() for line in text.split('\n') if line.strip()]
|
| 53 |
+
|
| 54 |
+
print("✅ File uploaded and processed successfully!")
|
| 55 |
+
|
| 56 |
+
# Speed slider
|
| 57 |
+
speed_slider = widgets.FloatSlider(
|
| 58 |
+
value=1.0,
|
| 59 |
+
min=0.5,
|
| 60 |
+
max=1.5,
|
| 61 |
+
step=0.1,
|
| 62 |
+
description='Speed:',
|
| 63 |
+
continuous_update=False
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
def on_speed_change(change):
|
| 67 |
+
global current_speed
|
| 68 |
+
current_speed = change['new']
|
| 69 |
+
|
| 70 |
+
speed_slider.observe(on_speed_change, names='value')
|
| 71 |
+
display(speed_slider)
|
| 72 |
+
|
| 73 |
+
# Function to read the text aloud
|
| 74 |
+
def display_line(line, idx):
|
| 75 |
+
styled_line = f"<h3 style='font-family: sans-serif; color: #333;'>📖 Line {idx + 1}: {line}</h3>"
|
| 76 |
+
display(HTML(styled_line))
|
| 77 |
+
|
| 78 |
+
def read_text(_=None):
|
| 79 |
+
clear_output(wait=True)
|
| 80 |
+
display(speed_slider)
|
| 81 |
+
display(read_button)
|
| 82 |
+
for idx, line in enumerate(lines):
|
| 83 |
+
display_line(line, idx)
|
| 84 |
+
speak_urdu(line, speed=current_speed)
|
| 85 |
+
input("🔸 Press Enter to read next line...")
|
| 86 |
|
| 87 |
+
# Read button
|
| 88 |
+
read_button = widgets.Button(description="🔊 Read Text")
|
| 89 |
+
read_button.on_click(read_text)
|
| 90 |
+
display(read_button)
|