Skip to content

Commit

Permalink
self hosted model added (#327)
Browse files Browse the repository at this point in the history
* self hosted model added

* url changed

* self hosted endpoint added
  • Loading branch information
ishanExtreme authored Sep 19, 2023
1 parent 72c50d3 commit f87d4e3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ayushma/migrations/0044_alter_project_stt_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2023-09-17 12:20

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ayushma', '0043_merge_20230905_1530'),
]

operations = [
migrations.AlterField(
model_name='project',
name='stt_engine',
field=models.IntegerField(choices=[(1, 'Whisper'), (2, 'Google'), (3, 'Self Hosted')], default=1),
),
]
1 change: 1 addition & 0 deletions ayushma/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DocumentType(IntegerChoices):
class STTEngine(IntegerChoices):
WHISPER = 1
GOOGLE = 2
SELF_HOSTED = 3


class FeedBackRating(IntegerChoices):
Expand Down
26 changes: 26 additions & 0 deletions ayushma/utils/speech_to_text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
import os

import openai
import requests
from django.conf import settings
from google.cloud import speech

from ayushma.models.enums import STTEngine
Expand Down Expand Up @@ -48,11 +51,34 @@ def recognize(self, audio):
if not response.results:
return ""
return response.results[0].alternatives[0].transcript

class SelfHostedEngine:
def __init__(self, api_key, language_code):
self.language_code = language_code

def recognize(self, audio):

response = requests.post(
settings.SELF_HOSTED_ENDPOINT,
files={"audio": audio},
data={
# change this model to get faster results see: https://github.com/coronasafe/care-whisper
"model": "small",
"language": self.language_code.replace("-IN", ""),
},
)

if not response.ok:
print("Failed to recognize speech with self hosted engine")
return ""
response = response.json()
return response["data"]["transcription"].strip()


engines = {
"whisper": WhisperEngine,
"google": GoogleEngine,
"self_hosted": SelfHostedEngine,
# Add new engines here
}

Expand Down
3 changes: 3 additions & 0 deletions core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,6 @@

AI_NAME = env("AI_NAME", default="Ayushma")
GOOGLE_RECAPTCHA_SECRET_KEY = env("GOOGLE_RECAPTCHA_SECRET_KEY", default=None)

# url for self hosted speech to text
SELF_HOSTED_ENDPOINT = env("SELF_HOSTED_ENDPOINT", default=None)

0 comments on commit f87d4e3

Please sign in to comment.