This commit is contained in:
Dan Chen
2024-03-18 16:19:26 +08:00
parent 253577da04
commit afb78295c8
6 changed files with 62 additions and 55 deletions

View File

@ -1,6 +1,8 @@
# jarvis-models
## Dependency
- filetype
| System | URL |
| --- | --- |
| python | https://pypi.org/project/filetype/ |
| System | package | web |
| --- |--- | --- |
| python | filetype |https://pypi.org/project/filetype/ |
| python | fastAPI | https://fastapi.tiangolo.com/ |
| python | python-multipart | https://pypi.org/project/python-multipart/ |

23
main.py
View File

@ -1,23 +0,0 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
hostName = "localhost"
serverPort = 8080
class MyServer(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Jarvis-models Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")

View File

@ -1,3 +1,5 @@
from fastapi import Response,status
from fastapi.responses import JSONResponse
import speech_recognition as sr
import filetype
import io
@ -10,9 +12,13 @@ class AudioToText(Blackbox):
kind = filetype.guess(data)
if kind is None:
return False
return kind.mime == "audio/wav"
return kind.extension == "wav"
def processing(self, data: io.BytesIO):
def processing(self, data: bytes | io.BytesIO):
if data is None:
raise ValueError("Data is required")
if isinstance(data, bytes):
data = io.BytesIO(data)
if not self.valid(data):
raise ValueError("Invalid data")
r = sr.Recognizer()
@ -20,3 +26,14 @@ class AudioToText(Blackbox):
audio_data = r.record(source)
text = r.recognize_google(audio_data)
return text
async def fast_api_handler(self, request) -> Response:
data = (await request.form()).get("data")
if data is None:
return JSONResponse(content={"error": "data is required"}, status_code=status.HTTP_400_BAD_REQUEST)
d = await data.read()
try:
txt = self.processing(d)
except ValueError as e:
return JSONResponse(content={"error": str(e)}, status_code=status.HTTP_400_BAD_REQUEST)
return JSONResponse(content={"txt": txt}, status_code=status.HTTP_200_OK)

View File

@ -1,8 +1,13 @@
from abc import ABC, abstractmethod
from fastapi import Request, Response
class Blackbox(ABC):
def __init__(self, config: any) -> None:
pass
@abstractmethod
def processing(self, data: any):
pass
@ -11,8 +16,6 @@ class Blackbox(ABC):
def valid(self, data: any) -> bool:
pass
class TextToAudio(Blackbox):
def processing(self, data: any):
print("TextToAudio processing")
@abstractmethod
def fast_api_handler(self, request: Request) -> Response:
pass

View File

@ -0,0 +1,12 @@
from blackbox.audio_to_text import AudioToText
from blackbox.blackbox import Blackbox
class BlockboxFactory:
def create_blockbox(self, blockbox_type: str, blockbox_config: dict) -> Blackbox:
if blockbox_type == "audio_to_text":
return AudioToText(blockbox_config)
raise ValueError("Invalid blockbox type")

View File

@ -1,25 +1,21 @@
from typing import Union
import asyncio
import websockets
from websockets.server import serve
from websockets.legacy.server import WebSocketServerProtocol
from fastapi import FastAPI, Request, UploadFile, status
from fastapi.responses import JSONResponse
from connection.connection import ConnectionContext
async def echo(websocket: WebSocketServerProtocol):
connection = ConnectionContext()
while True:
try:
message = await websocket.recv()
except websockets.ConnectionClosedOK:
break
connection.handle(message)
from blackbox.blockbox_factory import BlockboxFactory
import io
app = FastAPI()
blackbox_factory = BlockboxFactory()
async def main():
async with serve(echo, "localhost", 8765):
await asyncio.Future()
if __name__ == "__main__":
asyncio.run(main())
@app.post("/")
async def blackbox(blackbox_name: Union[str, None] = None, request: Request = None):
if not blackbox_name:
return await JSONResponse(content={"error": "blackbox_name is required"}, status_code=status.HTTP_400_BAD_REQUEST)
try:
box = blackbox_factory.create_blockbox(blackbox_name, {})
except ValueError:
return await JSONResponse(content={"error": "value error"}, status_code=status.HTTP_400_BAD_REQUEST)
return await box.fast_api_handler(request)