Skip to content

Commit

Permalink
feat(speech): enable local inference instead of calling api
Browse files Browse the repository at this point in the history
  • Loading branch information
qmi03 committed May 10, 2024
1 parent d9c730e commit df1c257
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ MQTT_PORT=1883
ADAFRUIT_USER=tuankiet0303
ADAFRUIT_KEY=
MONGO_USER=root
MONGO_PASSWORD=example
MONGO_PASSWORD=example
HUGGINGFACE_KEY=
2 changes: 1 addition & 1 deletion app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from flask import Flask, Response, render_template, request
from flask_cors import CORS
from flask_socketio import SocketIO, emit
from inference import query
from local_inference import query
from myMqtt import *
from pymongo import MongoClient
from werkzeug.utils import secure_filename
Expand Down
4 changes: 2 additions & 2 deletions app/inference.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio

import os
from aiohttp import ClientSession

headers = {"Authorization": f"Bearer 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"}
headers = {"Authorization": f"Bearer {os.environ.get("HUGGINGFACE_KEY")}"}
API_URL = "https://api-inference.huggingface.co/models/vinai/PhoWhisper-base"


Expand Down
18 changes: 18 additions & 0 deletions app/local_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import torch
from transformers import pipeline
import asyncio

device = "cuda:0" if torch.cuda.is_available() else "cpu"

model_id = "vinai/PhoWhisper-base"

transcriber = pipeline("automatic-speech-recognition", model="vinai/PhoWhisper-base",device=device)
async def query(filename):
return transcriber(filename)

async def main():
output = await query("uploads/audio.flac")
print(output)

if __name__ == "__main__":
asyncio.run(main())

0 comments on commit df1c257

Please sign in to comment.