Код: Выделить всё
class PostForm(StylishForm):
class Meta:
model = Post
fields = ["title", "body", "upload"]
widgets = {
"owner": forms.HiddenInput(),
}
Код: Выделить всё
# add new
def add(request):
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.owner = request.user
post.save()
return HttpResponseRedirect(reverse("view_posts"))
else:
form = PostForm()
return render(
request,
"posts/index.html",
{
"form": form,
},
)
Код: Выделить всё
def record_audio(request):
RATE = 16000
CHUNK = 1024
recordings = os.path.join(settings.MEDIA_ROOT, 'recordings')
# Ensure the folder exists
if not os.path.exists(recordings):
os.makedirs('recordings')
# Define the path to save the audio file
file_path = os.path.join(recordings, 'output.wav')
# def callback(data_input, frame_count, time_info, status):
# wav_file.writeframes(data_input)
# return None, pyaudio.paContinue
with wave.open(file_path, "wb") as wav_file:
wav_file.setnchannels(1) # Mono channel
wav_file.setsampwidth(2) # 16-bit samples
wav_file.setframerate(16000) # 16kHz sample rate
audio = pyaudio.PyAudio()
stream = audio.open(
format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
)
for _ in range(0, RATE // CHUNK):
wav_file.writeframes(stream.read(CHUNK))
stream.stop_stream()
stream.close()
audio.terminate()
audio_file= open(file_path, "rb")
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
print(transcription.text)
return HttpResponseRedirect(reverse("view_posts"))
Мой html-шаблон выглядит так:
Код: Выделить всё
tabindex="0">
{% csrf_token %}
{% for field in post_form %}
{{ field.label }}
{{ form.media }}
{{ field }}
{% endfor %}
Save
Подробнее здесь: https://stackoverflow.com/questions/792 ... jango-form
Мобильная версия