Hosdroid commited on
Commit
7e8ab5e
·
verified ·
1 Parent(s): 6c1ee00

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import os
4
+ import tempfile
5
+ import subprocess
6
+ from transformers import pipeline
7
+
8
+ # بارگذاری مدل Whisper از HuggingFace
9
+ pipe = pipeline("automatic-speech-recognition", model="openai/whisper-medium")
10
+
11
+ def transcribe(audio_file):
12
+ # ذخیره موقت فایل صوتی
13
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
14
+ tmp_path = tmp.name
15
+ subprocess.run(["ffmpeg", "-i", audio_file, "-ar", "16000", "-ac", "1", tmp_path, "-y"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
16
+
17
+ # اجرای چندبار مدل برای دقت بالاتر
18
+ texts = []
19
+ for i in range(2):
20
+ result = pipe(tmp_path)
21
+ texts.append(result["text"].strip())
22
+
23
+ # انتخاب دقیق‌ترین خروجی (طولانی‌ترین متن به احتمال زیاد درست‌تره)
24
+ best_text = max(texts, key=len)
25
+
26
+ os.remove(tmp_path)
27
+ return best_text
28
+
29
+ # رابط کاربری Gradio
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown("# 🎙️ تبدیل صوت به متن با دقت بالا")
32
+ gr.Markdown("هر فایل صوتی رو آپلود کن، متن دقیق استخراج می‌شه 👇")
33
+
34
+ with gr.Row():
35
+ audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="فایل صوتی")
36
+
37
+ output_text = gr.Textbox(label="متن استخراج‌شده", lines=8)
38
+ btn_copy = gr.Button("📋 کپی متن")
39
+
40
+ audio_input.change(fn=transcribe, inputs=audio_input, outputs=output_text)
41
+ btn_copy.click(lambda x: x, inputs=output_text, outputs=output_text)
42
+
43
+ demo.launch()