File size: 1,868 Bytes
f61aa39
98e8f5d
d0e9cf7
ce4aa6e
 
d0e9cf7
 
ce4aa6e
d0e9cf7
e900cc0
 
d0e9cf7
 
ce4aa6e
d0e9cf7
e900cc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f61aa39
 
ce4aa6e
f61aa39
d0e9cf7
ce4aa6e
 
 
 
 
f61aa39
 
 
d0e9cf7
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
import gradio as gr
import subprocess
import tempfile
import whisper
import os

# -----------------------------
# بارگذاری مدل Whisper
# -----------------------------
print("🎧 Loading Whisper model (large-v3)...")
model = whisper.load_model("large-v3")  # می‌توانید small, medium, large هم انتخاب کنید

# -----------------------------
# تابع پردازش ویدیو و تبدیل به متن
# -----------------------------
def transcribe_video(video_path):  # video_path یک رشته مسیر فایل است
    try:
        # ایجاد فایل صوتی موقت
        tmp_audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name

        # استخراج صدا با ffmpeg
        subprocess.run(
            ["ffmpeg", "-y", "-i", video_path, "-ar", "16000", "-ac", "1", tmp_audio_path],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL
        )

        # تبدیل صدا به متن
        result = model.transcribe(tmp_audio_path, fp16=False)
        text = result["text"].strip()

        # پاک کردن فایل صوتی موقت
        os.remove(tmp_audio_path)

        return text or "متنی شناسایی نشد."
    
    except Exception as e:
        return f"⚠️ خطا در پردازش ویدیو: {e}"

# -----------------------------
# رابط کاربری Gradio
# -----------------------------
iface = gr.Interface(
    fn=transcribe_video,
    inputs=gr.Video(label="🎥 ویدیو را آپلود کنید"),
    outputs=gr.Textbox(label="📝 متن استخراج‌شده"),
    title="🎧 ویدیو به متن با Whisper",
    description="ویدیوی خود را آپلود کنید تا گفتار آن به متن تبدیل شود."
)

if __name__ == "__main__":
    iface.launch(server_name="0.0.0.0", server_port=7860)