asagasad commited on
Commit
8f02cf0
·
verified ·
1 Parent(s): 3d37240

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -47
app.py CHANGED
@@ -1,64 +1,106 @@
1
- import gradio as gr
 
 
 
 
2
  from gtts import gTTS
3
- import tempfile
4
  import os
 
 
5
  import docx
6
- from pydub import AudioSegment
7
 
8
- # Function to extract text from docx
9
- def extract_text_from_docx(file):
10
- doc = docx.Document(file.name)
11
- return [para.text.strip() for para in doc.paragraphs if para.text.strip()]
12
 
13
- # Function to convert Urdu text to speech
14
- def urdu_tts(text, speed):
 
 
 
 
 
 
 
 
15
  tts = gTTS(text=text, lang='ur')
16
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
17
- tts.save(tmp.name)
18
- audio_path = tmp.name
19
 
20
- # Change speed using pydub
21
- sound = AudioSegment.from_file(audio_path)
22
- sound = sound.speedup(playback_speed=speed)
23
- final_path = audio_path.replace(".mp3", f"_x{speed}.mp3")
24
- sound.export(final_path, format="mp3")
25
 
26
- return final_path
27
 
28
- # Gradio stateful interface
29
- def start_reader(file, speed):
30
- lines = extract_text_from_docx(file)
31
- return lines, f"کل {len(lines)} لائنیں ملی ہیں۔", 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- def read_line(lines, line_idx, speed):
34
- if line_idx < len(lines):
35
- audio_file = urdu_tts(lines[line_idx], speed)
36
- return lines[line_idx], audio_file, line_idx + 1
 
 
 
 
 
 
37
  else:
38
- return "📘 تمام لائنیں پڑھ لی گئیں!", None, line_idx
 
 
 
 
 
39
 
40
- # Gradio UI
41
- with gr.Blocks() as demo:
42
- gr.Markdown("## 📖 اردو لائن بائی لائن ریڈر")
43
 
44
- with gr.Row():
45
- file_input = gr.File(label="📁 .docx فائل اپ لوڈ کریں", file_types=['.docx'])
46
- speed_slider = gr.Slider(0.5, 2.0, 1.0, step=0.1, label="🔊 رفتار")
 
47
 
48
- start_button = gr.Button("🚀 فائل پڑھنا شروع کریں")
49
- status = gr.Textbox(label="🔹 اسٹیٹس")
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
- next_button = gr.Button("▶️ اگلی لائن پڑھیں")
57
 
58
- start_button.click(fn=start_reader, inputs=[file_input, speed_slider],
59
- outputs=[lines_state, status, index_state])
60
-
61
- next_button.click(fn=read_line, inputs=[lines_state, index_state, speed_slider],
62
- outputs=[current_line, audio_output, index_state])
 
 
 
 
 
 
63
 
64
- demo.launch()
 
 
 
 
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)