Models.py
from django.db import models
class Voiceapi(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=200)
voice_text = models.CharField(max_length=200,default="voice_data")
Views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from rest_framework import viewsets
import requests
import gdown
from pydub import AudioSegment
import speech_recognition as sr
from .serializers import *
from .models import *
import time
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from urllib.request import urlopen
# create a viewset
class VoiceViewSet(viewsets.ModelViewSet):
# define queryset
queryset = Voiceapi.objects.all()
# specify serializer to be used
serializer_class = VoiceSerializer
#
print(Voiceapi.objects.values())
datas = Voiceapi.objects.values()
print(datas)
for i in datas:
try:
print("Audio File-->",i['name'])
audio_url = i['name']
audio_id = i['id']
def VoiceRecognizer(audio,audio_id):
r = sr.Recognizer()
with sr.AudioFile(audio) as source:
audio_text = r.listen(source)
try:
text = r.recognize_google(audio_text)
print(text)
Voiceapi.objects.filter(pk=audio_id).update(voice_text=text)
except:
print('Audio Not Clear')
audio = "/home/venpep/voicetotext/messages/media/test.wav"
VoiceRecognizer(audio,audio_id)
except:
print("Not audio file")
I need to pass the variable "text" from my view.py to models.py to set the default in voice_text.Is there any solution to get the text from the view page. The text variable is a basic string that needs to pass to the models.py
CodePudding user response:
as I understood from your comment, you can override the save method in django model
and put your logic, in this way you can edit field value before save it and send it in response
class Voiceapi(models.Model):
...
def save(self, *args, **kwargs):
...