Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,7 @@ import os
|
|
| 2 |
import speech_recognition as sr
|
| 3 |
import difflib
|
| 4 |
import gradio as gr
|
| 5 |
-
import
|
| 6 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
-
import soundfile as sf
|
| 8 |
|
| 9 |
# Tạo thư mục audio nếu chưa tồn tại
|
| 10 |
if not os.path.exists('audio'):
|
|
@@ -16,18 +14,6 @@ def transcribe_audio(audio):
|
|
| 16 |
return "No audio file provided." # Xử lý trường hợp không có tệp âm thanh
|
| 17 |
|
| 18 |
recognizer = sr.Recognizer()
|
| 19 |
-
audio_format = audio.split('.')[-1].lower()
|
| 20 |
-
|
| 21 |
-
# Chuyển đổi sang WAV nếu âm thanh không ở định dạng hỗ trợ
|
| 22 |
-
if audio_format != 'wav':
|
| 23 |
-
try:
|
| 24 |
-
audio_segment = AudioSegment.from_file(audio)
|
| 25 |
-
wav_path = audio.replace(audio_format, 'wav')
|
| 26 |
-
audio_segment.export(wav_path, format='wav') # Chuyển đổi sang WAV
|
| 27 |
-
audio = wav_path # Cập nhật đường dẫn âm thanh
|
| 28 |
-
except Exception as e:
|
| 29 |
-
return f"Error converting audio: {e}"
|
| 30 |
-
|
| 31 |
audio_file = sr.AudioFile(audio)
|
| 32 |
|
| 33 |
with audio_file as source:
|
|
@@ -43,20 +29,9 @@ def transcribe_audio(audio):
|
|
| 43 |
|
| 44 |
# Bước 2: Tạo âm thanh phát âm cho các từ sai
|
| 45 |
def create_pronunciation_audio(word):
|
| 46 |
-
|
| 47 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 48 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 49 |
-
|
| 50 |
-
inputs = tokenizer(word, return_tensors="pt")
|
| 51 |
-
|
| 52 |
-
# Tạo âm thanh từ văn bản
|
| 53 |
-
with torch.no_grad():
|
| 54 |
-
outputs = model.generate(**inputs)
|
| 55 |
-
|
| 56 |
-
# Lưu âm thanh vào tệp
|
| 57 |
audio_file_path = f"audio/{word}.wav"
|
| 58 |
-
|
| 59 |
-
|
| 60 |
return audio_file_path
|
| 61 |
|
| 62 |
# Bước 3: So sánh văn bản đã chuyển đổi với đoạn văn bản gốc
|
|
|
|
| 2 |
import speech_recognition as sr
|
| 3 |
import difflib
|
| 4 |
import gradio as gr
|
| 5 |
+
from transformers import pipeline
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Tạo thư mục audio nếu chưa tồn tại
|
| 8 |
if not os.path.exists('audio'):
|
|
|
|
| 14 |
return "No audio file provided." # Xử lý trường hợp không có tệp âm thanh
|
| 15 |
|
| 16 |
recognizer = sr.Recognizer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
audio_file = sr.AudioFile(audio)
|
| 18 |
|
| 19 |
with audio_file as source:
|
|
|
|
| 29 |
|
| 30 |
# Bước 2: Tạo âm thanh phát âm cho các từ sai
|
| 31 |
def create_pronunciation_audio(word):
|
| 32 |
+
tts = pipeline("text-to-speech", model="tts_models/en/ljspeech/tacotron2") # Sử dụng pipeline TTS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
audio_file_path = f"audio/{word}.wav"
|
| 34 |
+
tts(word, output_file=audio_file_path) # Tạo âm thanh từ văn bản
|
|
|
|
| 35 |
return audio_file_path
|
| 36 |
|
| 37 |
# Bước 3: So sánh văn bản đã chuyển đổi với đoạn văn bản gốc
|