我正在尝试做一个将 .wav 音频翻译成 django 中的文本的网站。我这里有问题。我正在使用 websocket 立即发送翻译后的文本。但是当我尝试发送文本时,会发生此错误(“TranscriptionConsumer”对象没有属性“base_send”)
views.py
# this function recognites text and send it to Consumers's function
def get_large_audio_transcription(path,language, consumer ,minutes=2):
sound = AudioSegment.from_file(path)
chunk_length_ms = int(1000*60*minutes)
chunks = [sound[i:i + chunk_length_ms] for i in range(0, len(sound), chunk_length_ms)]
folder_name=str(settings.MEDIA_ROOT) + "/audio-chunks/"
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
whole_text = ""
for i, audio_chunk in enumerate(chunks,start=1):
chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
audio_chunk.export(chunk_filename, format="wav")
try:
text = transcribe_small_audio(chunk_filename, language=language)
except sr.UnknownValueError as e:
print("Error:", str(e))
else:
text = f"{text.capitalize()}."
print(text)
whole_text += text
json_transcribe = json.dumps({"message": text})
# Sending json text through WebSocket
consumer.send_json(json_transcribe)
# deleting chunk
try:
os.remove(chunk_filename)
except FileNotFoundError:
print("Error: file not found")
return whole_text
@login_required(login_url='login_user')
def index(request):
context = None
audio_form = UploadFileForm()
if request.method == "POST":
audio_form = UploadFileForm(request.POST, request.FILES)
if not audio_form.is_valid():
messages.success(request, ("Error!"))
return render(request, 'recognition/index.html', {"error": "Provide a valid file"})
try:
form = audio_form.save(commit=False)
form.name = request.user
form.save()
file = form.audio # get the audio
file_size = file.size
file_path = str(settings.MEDIA_ROOT) + '/' + str(file.name)
consumer = TranscriptionConsumer()
messages.success(request, ("File size is too big. We will give you a file with transcription..."))
text = get_large_audio_transcription(file_path,"en-US", consumer)
os.remove(file_path)
context = {
"text" :text,
"AudioForm":audio_form,
"size" : file_size
}
except Exception as ex:
context = {"error": str(ex)}
else:
context = {
"AudioForm" : audio_form
}
return render(request, "recognition/index.html", context )
我的消费者.py
import json
from channels.generic.websocket import WebsocketConsumer
class TranscriptionConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def send_json(self, text_data):
self.connect()
text_data_json = json.loads(text_data)
text_message = text_data_json["message"]
self.send(text_data=json.dumps({
'message' : text_message
}))
index.html 文件
{% extends 'app/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<h2>Upload file</h2>
<form method="post", enctype="multipart/form-data">
{% csrf_token %}
{{ AudioForm | crispy }}
<div class="d-grid gap-3 mt-3">
<button type="submit" class="btn btn-outline-success">Upload audio</button>
</div>
</form>
<p>Your file size :</p>
{{ size|filesizeformat }}
{{ size }}
<h2>Transcript</h2>
{% if error %}
<p id="error" style="color: red">{{ error }}</p>
{% endif %}
<p id="app">
{{ text }}
</p>
<script>
const socket = new WebSocket('ws://' + window.location.host + '/ws/somepath/');
socket.onmessage = function (event) {
const data = JSON.parse(event.data);
console.log(data.message);
document.querySelector('#app').innerText = data.message
};
</script>
{% endblock %}
我希望识别的文本出现在页面上,但我收到错误。
顺便说一句,我是 django 新手
我注意到您的代码中的一些内容可能会导致问题。例如,在
self.connect()
中调用 send_json
是不必要的——建立 WebSocket 连接时,Django Channels 会自动调用 connect()
,因此您无需在 send_json
中再次调用它。
此外,在
send_json
中,您可以直接使用self.send
将JSON数据发送到WebSocket客户端。
这是您的
TranscriptionConsumer
类的更新版本以及应该可以使用的 get_large_audio_transcription
函数:
# consumers.py
import json
from channels.generic.websocket import WebsocketConsumer
class TranscriptionConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def send_json(self, text_data):
# Send text data as JSON to the WebSocket
self.send(text_data)
对于
get_large_audio_transcription
功能:
# This function recognizes text and sends it to the Consumer's function
def get_large_audio_transcription(path, language, consumer, minutes=2):
sound = AudioSegment.from_file(path)
chunk_length_ms = int(1000 * 60 * minutes)
chunks = [sound[i:i + chunk_length_ms] for i in range(0, len(sound), chunk_length_ms)]
folder_name = str(settings.MEDIA_ROOT) + "/audio-chunks/"
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
whole_text = ""
for i, audio_chunk in enumerate(chunks, start=1):
chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
audio_chunk.export(chunk_filename, format="wav")
try:
text = transcribe_small_audio(chunk_filename, language=language)
except sr.UnknownValueError as e:
print("Error:", str(e))
else:
text = f"{text.capitalize()}."
print(text)
whole_text += text
json_transcribe = json.dumps({"message": text})
# Sending JSON text through WebSocket
consumer.send_json(json_transcribe)
# Deleting chunk
try:
os.remove(chunk_filename)
except FileNotFoundError:
print("Error: file not found")
return whole_text