diff --git a/backend/areas/views.py b/backend/areas/views.py index 6671510..b2008be 100644 --- a/backend/areas/views.py +++ b/backend/areas/views.py @@ -2,6 +2,9 @@ from rest_framework.response import Response from rest_framework.exceptions import NotFound import requests +import base64 +import ffmpeg +import subprocess # Create your views here. from .models import Dataset, Tool, Model,News @@ -15,6 +18,7 @@ DHRUVA_MODEL_VIEW_URL = "https://api.dhruva.ekstep.ai/services/details/view_service" DHRUVA_API_KEY = "0aaef7ff-86f3-4bb0-a30b-9f50f3de1a52" + @permission_classes((permissions.AllowAny,)) class InferenceView(APIView): def post(self, request, format=None): @@ -44,8 +48,60 @@ def post(self, request, format=None): } ] }) + return Response(inferenceResult.json(),status=status.HTTP_200_OK) + + elif task == "asr": + + INFERENCE_API = "https://api.dhruva.ekstep.ai/services/inference/asr" + + webm_base64 = body["audioContent"] + webm_data = base64.b64decode(webm_base64) + + with open("/tmp/temp.webm", "wb") as webm_file: + webm_file.write(webm_data) + + subprocess.run(["ffmpeg","-y", "-i", "/tmp/temp.webm", "/tmp/temp.wav"], check=True) + + with open("/tmp/temp.wav", "rb") as wav_file: + wav_data = wav_file.read() + wav_base64 = base64.b64encode(wav_data).decode('utf-8') + + + inferenceResult = requests.post(INFERENCE_API,headers= + {'x-auth-source': 'API_KEY', + 'Authorization': DHRUVA_API_KEY}, + json={ + "controlConfig": { + "dataTracking": True + }, + "config": { + "audioFormat": "wav", + "language": { + "sourceLanguage": body["sourceLanguage"], + "sourceScriptCode": "" + }, + "encoding": "wav", + "samplingRate": body["samplingRate"], + "serviceId": body["serviceId"], + "preProcessors": body["preProcessors"], + "postProcessors": body["postProcessors"], + "transcriptionFormat": { + "value": "transcript" + }, + "bestTokenCount": 0 + }, + "audio": [ + { + "audioContent": wav_base64, + } + ] + } + ) + return Response(inferenceResult.json(),status=status.HTTP_200_OK) + + + - return Response(inferenceResult.json(),status=status.HTTP_200_OK) class NewsViewSet(viewsets.ModelViewSet): diff --git a/frontend/components/Models.tsx b/frontend/components/Models.tsx index 41bf4e2..36568a1 100644 --- a/frontend/components/Models.tsx +++ b/frontend/components/Models.tsx @@ -15,6 +15,7 @@ import { API_URL } from "@/app/config"; import { useEffect, useState } from "react"; import axios from "axios"; import NMT from "./TryOut/NMT"; +import ASR from "./TryOut/ASR"; import { FaPaperclip, FaGithub } from "react-icons/fa"; const fetchModel = async ({ title }: { title: string }) => { @@ -49,6 +50,13 @@ const renderTryOut = ({ area, model }: { area: string; model: Model }) => { serviceId={model.service_id} /> ); + case "ASR": + return ( + + ); } }; diff --git a/frontend/components/TryOut/ASR.tsx b/frontend/components/TryOut/ASR.tsx new file mode 100644 index 0000000..a5fc997 --- /dev/null +++ b/frontend/components/TryOut/ASR.tsx @@ -0,0 +1,68 @@ +"use client"; +import React from "react"; +import { useState } from "react"; +import { + FormControl, + FormLabel, + FormErrorMessage, + FormHelperText, + Select, + Textarea, + Button, + Card, + HStack, + VStack, + Switch, +} from "@chakra-ui/react"; +import { LANGUAGE_CODE_NAMES } from "@/app/config"; +import axios from "axios"; +import { API_URL } from "@/app/config"; +import { useToast } from "@chakra-ui/react"; +import { FaMicrophone } from "react-icons/fa"; +import { AudioRecorder, useAudioRecorder } from "react-audio-voice-recorder"; + +const fetchAudio = ({ blob }: { blob: Blob }) => { + const reader = new FileReader(); + let base64data: string | ArrayBuffer | null; + reader.readAsDataURL(blob); + reader.onloadend = function () { + base64data = reader.result; + const audioString = (base64data as string).split(",")[1]; + console.log(audioString); + }; +}; + +interface LanguageCodeNames { + [key: string]: string; +} + +export default function ASR({ + sourceLanguages = [], + serviceId, +}: { + sourceLanguages: Array; + serviceId: string; +}) { + const recorderControls = useAudioRecorder(); + return ( + + + + + + Select Source Language: + + + + + + fetchAudio({ blob: blob })} + recorderControls={recorderControls} + /> + + + + + ); +} diff --git a/frontend/components/TryOut/NMT.tsx b/frontend/components/TryOut/NMT.tsx index 27a5f8b..81635ad 100644 --- a/frontend/components/TryOut/NMT.tsx +++ b/frontend/components/TryOut/NMT.tsx @@ -18,6 +18,7 @@ import { LANGUAGE_CODE_NAMES } from "@/app/config"; import axios from "axios"; import { API_URL } from "@/app/config"; import { IndicTransliterate } from "@ai4bharat/indic-transliterate"; +import { useToast } from "@chakra-ui/react"; const fetchTranslation = async ({ sourceLanguage, @@ -66,9 +67,11 @@ export default function NMT({ const [inputText, setInputText] = useState(""); const [outputText, setOutputText] = useState(""); + const toast = useToast(); + return ( - + @@ -130,26 +133,36 @@ export default function NMT({ }} lang={sourceLanguage} /> - {/*