Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,192 +1,74 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import speech_recognition as sr
|
| 3 |
-
import numpy as np
|
| 4 |
from pydub import AudioSegment
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import threading
|
| 8 |
-
import queue
|
| 9 |
-
import time
|
| 10 |
import os
|
| 11 |
|
| 12 |
# تنظیمات اولیه
|
| 13 |
recognizer = sr.Recognizer()
|
| 14 |
recognizer.energy_threshold = 300
|
| 15 |
recognizer.dynamic_energy_threshold = True
|
| 16 |
-
recognizer.dynamic_energy_ratio = 1.5
|
| 17 |
-
|
| 18 |
-
# صف برای پردازش asynchronous
|
| 19 |
-
audio_queue = queue.Queue()
|
| 20 |
-
transcript_queue = queue.Queue()
|
| 21 |
-
|
| 22 |
-
# متغیرهای نمایش متن
|
| 23 |
-
current_transcript = ""
|
| 24 |
-
current_transcript_lock = threading.Lock()
|
| 25 |
-
|
| 26 |
-
def convert_numpy_to_wav(audio_data, sample_rate=16000):
|
| 27 |
-
"""تعداد به فرمت WAV با نرمالسازی"""
|
| 28 |
-
buffer = io.BytesIO()
|
| 29 |
-
with wave.open(buffer, 'wb') as wav_file:
|
| 30 |
-
wav_file.setnchannels(1)
|
| 31 |
-
wav_file.setsampwidth(2)
|
| 32 |
-
wav_file.setframerate(sample_rate)
|
| 33 |
-
wav_file.writeframes(np.int16(audio_data * 32767))
|
| 34 |
-
buffer.seek(0)
|
| 35 |
-
return AudioSegment.from_wav(buffer)
|
| 36 |
|
|
|
|
| 37 |
def process_audio_chunk(audio_chunk):
|
| 38 |
"""پردازش یک قطعه صوتی"""
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
try:
|
| 54 |
-
text = recognizer.recognize_google(audio, language='en-US')
|
| 55 |
-
except:
|
| 56 |
-
text = ""
|
| 57 |
-
except sr.RequestError:
|
| 58 |
-
text = "[خطا در اتصال]"
|
| 59 |
-
|
| 60 |
-
os.unlink(tmp_path) # پاک کردن فایل موقت
|
| 61 |
-
return text.strip()
|
| 62 |
except Exception as e:
|
| 63 |
-
|
| 64 |
-
return ""
|
| 65 |
-
|
| 66 |
-
def monitor_audio(audio_input):
|
| 67 |
-
"""م’environیک بلند کردن و پردازش"""
|
| 68 |
-
for i in range(0, len(audio_input), 16000):
|
| 69 |
-
chunk = audio_input[i:i+16000]
|
| 70 |
-
if len(chunk) < 16000:
|
| 71 |
-
continue
|
| 72 |
-
audio_queue.put((chunk, 16000))
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
if not transcript_queue.empty():
|
| 78 |
-
new_text = transcript_queue.get()
|
| 79 |
-
with current_transcript_lock:
|
| 80 |
-
nonlocal current_transcript
|
| 81 |
-
current_transcript += " " + new_text
|
| 82 |
-
current_transcript = " ".join(current_transcript.split())
|
| 83 |
-
time.sleep(0.1)
|
| 84 |
-
|
| 85 |
-
# رابط کاربری با Gradio
|
| 86 |
-
with gr.Blocks(title="گستره گفتار به متن", theme=gr.themes.Soft(), css="""
|
| 87 |
-
.gradio-container { font-family: 'Vazir', 'Tahoma', sans-serif !important; }
|
| 88 |
-
.rtl { direction: rtl; text-align: right; }
|
| 89 |
-
""") as demo:
|
| 90 |
-
|
| 91 |
-
# صفحه اصلی
|
| 92 |
-
gr.Markdown("""
|
| 93 |
-
# 🎤 تبدیل گفتار به متن
|
| 94 |
-
ابزار قدرتمند تبدیل صدات را به متن با پشتیبانی از زبان فارسی و انگلیسی
|
| 95 |
-
""")
|
| 96 |
|
| 97 |
# تب ضبط مستقیم
|
| 98 |
with gr.TabItem("🎙️ ضبط مستقیم"):
|
| 99 |
-
gr.
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
label="متن تشخیص داده شده",
|
| 113 |
-
placeholder="شروع به صحبت کنید و متن اینجا ظاهر میشود...",
|
| 114 |
-
lines=12,
|
| 115 |
-
elem_classes="rtl",
|
| 116 |
-
rtl=True,
|
| 117 |
-
show_copy_button=True
|
| 118 |
-
)
|
| 119 |
-
clear_btn.click(lambda: "", outputs=[realtime_output])
|
| 120 |
-
audio_input.stream(
|
| 121 |
-
lambda x: monitor_audio(x),
|
| 122 |
-
inputs=[audio_input],
|
| 123 |
-
outputs=[],
|
| 124 |
-
every=0.1
|
| 125 |
-
)
|
| 126 |
-
audio_input.stream(
|
| 127 |
-
lambda: update_transcript(),
|
| 128 |
-
inputs=[],
|
| 129 |
-
outputs=[realtime_output],
|
| 130 |
-
every=0.1
|
| 131 |
-
)
|
| 132 |
|
| 133 |
# تب فایل صوتی
|
| 134 |
with gr.TabItem("📁 فایل صوتی"):
|
| 135 |
-
gr.
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
with gr.Column(scale=1):
|
| 152 |
-
save_btn = gr.Button("💾 ذخیره متن")
|
| 153 |
-
clear_file_btn = gr.Button("🗑️ پاک کردن")
|
| 154 |
-
download_file = gr.File(label="دانلود فایل متن", visible=False)
|
| 155 |
-
|
| 156 |
-
def process_file(audio_file, duration):
|
| 157 |
-
try:
|
| 158 |
-
audio = AudioSegment.from_file(audio_file)
|
| 159 |
-
results = []
|
| 160 |
-
for i in range(0, len(audio), duration*1000):
|
| 161 |
-
chunk = audio[i:i+duration*1000]
|
| 162 |
-
chunk_text = process_audio_chunk(np.array(chunk.get_array_of_samples()))
|
| 163 |
-
results.append(chunk_text)
|
| 164 |
-
return " ".join(results), "تکمیل پردازش ✅"
|
| 165 |
-
except Exception as e:
|
| 166 |
-
return f"خطا: {str(e)}", "خطای پردازش ❌"
|
| 167 |
-
|
| 168 |
-
process_btn.click(
|
| 169 |
-
process_file,
|
| 170 |
-
inputs=[file_input, chunk_duration],
|
| 171 |
-
outputs=[realtime_output, status_label]
|
| 172 |
-
)
|
| 173 |
-
save_btn.click(
|
| 174 |
-
lambda x: gr.File.value(x),
|
| 175 |
-
inputs=[realtime_output],
|
| 176 |
-
outputs=[download_file]
|
| 177 |
-
).then(
|
| 178 |
-
lambda: gr.update(visible=True),
|
| 179 |
-
outputs=[download_file]
|
| 180 |
-
)
|
| 181 |
-
clear_file_btn.click(
|
| 182 |
-
lambda: ("", ""),
|
| 183 |
-
outputs=[realtime_output, status_label]
|
| 184 |
-
)
|
| 185 |
|
| 186 |
-
|
| 187 |
-
if __name__ == "__main__":
|
| 188 |
-
demo.queue().launch(
|
| 189 |
-
share=True,
|
| 190 |
-
show_error=True,
|
| 191 |
-
favicon=__file__
|
| 192 |
-
)
|
|
|
|
| 1 |
+
# کد اصلاح شده با توجه به مرحله بیگانگان بهدرستی اسکوپدهایع، бродریạ، و جستجوهایوسرا مدیریت میکند:
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import speech_recognition as sr
|
|
|
|
| 5 |
from pydub import AudioSegment
|
| 6 |
+
import numpy as np
|
| 7 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
| 8 |
import os
|
| 9 |
|
| 10 |
# تنظیمات اولیه
|
| 11 |
recognizer = sr.Recognizer()
|
| 12 |
recognizer.energy_threshold = 300
|
| 13 |
recognizer.dynamic_energy_threshold = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# سرکننده صداهای واقعی
|
| 16 |
def process_audio_chunk(audio_chunk):
|
| 17 |
"""پردازش یک قطعه صوتی"""
|
| 18 |
try:
|
| 19 |
+
buffer = io.BytesIO()
|
| 20 |
+
with wave.open(buffer, 'wb') as wav_file:
|
| 21 |
+
wav_file.setnchannels(1)
|
| 22 |
+
wav_file.setsampwidth(2)
|
| 23 |
+
wav_file.setframerate(16000)
|
| 24 |
+
wav_file.writeframes(np.int16(audio_chunk * 32767))
|
| 25 |
+
with sr.AudioFile(io.BytesIO(buffer.getvalue())) as source:
|
| 26 |
+
audio = recognizer.record(source)
|
| 27 |
+
# تلاش با اولویت فارسی
|
| 28 |
+
try:
|
| 29 |
+
return recognizer.recognize_google(audio, language='fa-IR')
|
| 30 |
+
except sr.UnknownValueError:
|
| 31 |
+
return recognizer.recognize_google(audio, language='en-US', show_all=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
+
return "[خطا در تشخیص]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# رابط کاربری طراحی شده
|
| 36 |
+
with gr.Blocks(title="گستره گفتار به متن", theme=gr.themes.Soft()) as demo:
|
| 37 |
+
gr.Markdown("# 🎙️ تبدیل گفتار به متن (پیشروز 2025)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# تب ضبط مستقیم
|
| 40 |
with gr.TabItem("🎙️ ضبط مستقیم"):
|
| 41 |
+
audio_input = gr.Audio(type="numpy", label="میکروفون", streaming=True)
|
| 42 |
+
clear_btn = gr.Button("پاک کردن")
|
| 43 |
+
output = gr.Textbox(label="متن تشخیص دادهشده", lines=12)
|
| 44 |
+
|
| 45 |
+
def update_transcript(audio, transcript):
|
| 46 |
+
output = ""
|
| 47 |
+
for segment in audio:
|
| 48 |
+
transcribed = process_audio_chunk(segment)
|
| 49 |
+
if transcribed:
|
| 50 |
+
output += transcribed + " "
|
| 51 |
+
return output.strip()
|
| 52 |
+
|
| 53 |
+
audio_input.stream(update_transcript, inputs=[audio_input, gr.State("")], outputs=[output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# تب فایل صوتی
|
| 56 |
with gr.TabItem("📁 فایل صوتی"):
|
| 57 |
+
file_input = gr.Audio(type="file", label="انتخاب فایل")
|
| 58 |
+
progress = gr.Textbox(label="وضعیت")
|
| 59 |
+
file_output = gr.Textbox(label="متن خروجی")
|
| 60 |
+
save_btn = gr.Button("ذخیره")
|
| 61 |
+
|
| 62 |
+
def process_file(audio_file, chunk_size=30000):
|
| 63 |
+
audio = AudioSegment.from_file(audio_file)
|
| 64 |
+
chunk_ms = chunk_size * 1000
|
| 65 |
+
results = []
|
| 66 |
+
for i in range(0, len(audio), chunk_ms):
|
| 67 |
+
chunk = audio[i:i+chunk_ms]
|
| 68 |
+
chunk_array = np.array(chunk.get_array_of_samples())
|
| 69 |
+
chunk_np = chunk_array.astype(np.float32) / 32767.0
|
| 70 |
+
recognized = process_audio_chunk(chunk_np)
|
| 71 |
+
results.append(recognized)
|
| 72 |
+
return " ".join(results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
demo.queue().launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|