File size: 2,255 Bytes
dc65b5f
ccf06cc
 
 
 
 
dc65b5f
ccf06cc
 
 
 
dc65b5f
ccf06cc
 
 
 
 
 
dc65b5f
ccf06cc
 
 
 
 
dc65b5f
ccf06cc
dc65b5f
ccf06cc
 
 
 
dc65b5f
ccf06cc
 
 
 
 
 
dc65b5f
ccf06cc
 
 
dc65b5f
ccf06cc
 
 
dc65b5f
ccf06cc
 
 
 
 
 
 
dc65b5f
ccf06cc
dc65b5f
ccf06cc
 
 
 
 
dc65b5f
ccf06cc
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
import gradio as gr
from gtts import gTTS
import tempfile
import os
import docx
from pydub import AudioSegment

# Function to extract text from docx
def extract_text_from_docx(file):
    doc = docx.Document(file.name)
    return [para.text.strip() for para in doc.paragraphs if para.text.strip()]

# Function to convert Urdu text to speech
def urdu_tts(text, speed):
    tts = gTTS(text=text, lang='ur')
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
        tts.save(tmp.name)
        audio_path = tmp.name

    # Change speed using pydub
    sound = AudioSegment.from_file(audio_path)
    sound = sound.speedup(playback_speed=speed)
    final_path = audio_path.replace(".mp3", f"_x{speed}.mp3")
    sound.export(final_path, format="mp3")

    return final_path

# Gradio stateful interface
def start_reader(file, speed):
    lines = extract_text_from_docx(file)
    return lines, f"کل {len(lines)} لائنیں ملی ہیں۔", 0

def read_line(lines, line_idx, speed):
    if line_idx < len(lines):
        audio_file = urdu_tts(lines[line_idx], speed)
        return lines[line_idx], audio_file, line_idx + 1
    else:
        return "📘 تمام لائنیں پڑھ لی گئیں!", None, line_idx

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 📖 اردو لائن بائی لائن ریڈر")

    with gr.Row():
        file_input = gr.File(label="📁 .docx فائل اپ لوڈ کریں", file_types=['.docx'])
        speed_slider = gr.Slider(0.5, 2.0, 1.0, step=0.1, label="🔊 رفتار")

    start_button = gr.Button("🚀 فائل پڑھنا شروع کریں")
    status = gr.Textbox(label="🔹 اسٹیٹس")
    lines_state = gr.State()
    index_state = gr.State()
    
    current_line = gr.Textbox(label="📌 موجودہ لائن", lines=2)
    audio_output = gr.Audio(label="🎧 آڈیو")

    next_button = gr.Button("▶️ اگلی لائن پڑھیں")

    start_button.click(fn=start_reader, inputs=[file_input, speed_slider],
                       outputs=[lines_state, status, index_state])
    
    next_button.click(fn=read_line, inputs=[lines_state, index_state, speed_slider],
                      outputs=[current_line, audio_output, index_state])

demo.launch()