asagasad commited on
Commit
a2ef783
·
verified ·
1 Parent(s): 491fb26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -54
app.py CHANGED
@@ -1,77 +1,64 @@
1
  import gradio as gr
2
  from gtts import gTTS
3
- import os
4
  import tempfile
5
- from pydub import AudioSegment
6
- from pydub.playback import play
7
  import docx
 
8
 
9
- # Global state
10
- line_index = 0
11
- all_lines = []
12
-
13
- # Function to extract text from an uploaded .docx file
14
  def extract_text_from_docx(file):
15
- doc = docx.Document(file)
16
  return [para.text.strip() for para in doc.paragraphs if para.text.strip()]
17
 
18
- # Function to generate TTS and return audio path
19
- def generate_tts_audio(text, speed):
20
  tts = gTTS(text=text, lang='ur')
21
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
22
- tts.save(tmp_file.name)
23
- audio_path = tmp_file.name
24
 
25
- # Adjust speed using pydub
26
  sound = AudioSegment.from_file(audio_path)
27
- new_speed = max(0.5, min(speed, 2.0)) # clamp between 0.5x and 2.0x
28
- altered_sound = sound._spawn(sound.raw_data, overrides={
29
- "frame_rate": int(sound.frame_rate * new_speed)
30
- }).set_frame_rate(sound.frame_rate)
31
 
32
- adjusted_path = audio_path.replace(".mp3", f"_x{speed}.mp3")
33
- altered_sound.export(adjusted_path, format="mp3")
34
- return adjusted_path
35
 
36
- # Called when file is uploaded and "start" is clicked
37
  def start_reader(file, speed):
38
- global line_index, all_lines
39
- line_index = 0
40
- all_lines = extract_text_from_docx(file)
41
- if all_lines:
42
- text = all_lines[0]
43
- audio_path = generate_tts_audio(text, speed)
44
- return text, audio_path, f"کل {len(all_lines)} لائنیں ملی ہیں۔", gr.update(visible=True)
45
- else:
46
- return "", None, "فائل میں کوئی اردو مواد نہیں ملا۔", gr.update(visible=False)
47
 
48
- # Called when "next line" is clicked
49
- def next_line(speed):
50
- global line_index, all_lines
51
- if line_index + 1 < len(all_lines):
52
- line_index += 1
53
- text = all_lines[line_index]
54
- audio_path = generate_tts_audio(text, speed)
55
- return text, audio_path, f"لائن {line_index + 1} / {len(all_lines)}", gr.update(visible=True)
56
  else:
57
- return " تمام لائنیں مکمل ہو گئیں۔", None, f"کل {len(all_lines)} لائنیں مکمل ہو چکی ہیں۔", gr.update(visible=False)
58
 
59
- # UI setup
60
- with gr.Blocks(title="📖 Urdu Line Reader") as demo:
61
- gr.Markdown("## 📚 اردو فائل پڑھنے والا بوٹ")
62
- gr.Markdown("`ایک Urdu .docx فائل اپلوڈ کریں، رفتار منتخب کریں، اور باری باری ہر لائن کو سنیں`")
63
 
64
- file = gr.File(label="📎 اردو فائل اپلوڈ کریں (.docx)", file_types=[".docx"])
65
- speed = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="🔊 پڑھنے کی رفتار")
 
66
 
67
- start_btn = gr.Button("فائل پڑھنا شروع کریں")
68
- next_btn = gr.Button("اگلی لائن پڑھیں", visible=False)
 
 
 
 
 
69
 
70
- text_display = gr.Textbox(label="📖 موجودہ لائن", interactive=False)
71
- audio_output = gr.Audio(label="🔈 آڈیو", type="filepath", autoplay=True)
72
- status = gr.Textbox(label="ℹ️ معلومات", interactive=False)
73
 
74
- start_btn.click(fn=start_reader, inputs=[file, speed], outputs=[text_display, audio_output, status, next_btn])
75
- next_btn.click(fn=next_line, inputs=speed, outputs=[text_display, audio_output, status, next_btn])
 
 
 
76
 
77
  demo.launch()
 
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()