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 # jarvis-models
## Dependency ## Dependency
- filetype - filetype
| System | URL | | System | package | web |
| --- | --- | | --- |--- | --- |
| python | https://pypi.org/project/filetype/ | | 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 speech_recognition as sr
import filetype import filetype
import io import io
@ -10,9 +12,13 @@ class AudioToText(Blackbox):
kind = filetype.guess(data) kind = filetype.guess(data)
if kind is None: if kind is None:
return False 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): if not self.valid(data):
raise ValueError("Invalid data") raise ValueError("Invalid data")
r = sr.Recognizer() r = sr.Recognizer()
@ -20,3 +26,14 @@ class AudioToText(Blackbox):
audio_data = r.record(source) audio_data = r.record(source)
text = r.recognize_google(audio_data) text = r.recognize_google(audio_data)
return text 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,18 +1,21 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from fastapi import Request, Response
class Blackbox(ABC): class Blackbox(ABC):
def __init__(self, config: any) -> None:
pass
@abstractmethod @abstractmethod
def processing(self, data: any): def processing(self, data: any):
pass pass
@abstractmethod @abstractmethod
def valid(self, data: any) -> bool: def valid(self, data: any) -> bool:
pass pass
class TextToAudio(Blackbox): @abstractmethod
def fast_api_handler(self, request: Request) -> Response:
def processing(self, data: any): pass
print("TextToAudio processing")

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 from fastapi import FastAPI, Request, UploadFile, status
import websockets from fastapi.responses import JSONResponse
from websockets.server import serve
from websockets.legacy.server import WebSocketServerProtocol
from connection.connection import ConnectionContext from blackbox.blockbox_factory import BlockboxFactory
import io
async def echo(websocket: WebSocketServerProtocol): app = FastAPI()
connection = ConnectionContext()
while True:
try:
message = await websocket.recv()
except websockets.ConnectionClosedOK:
break
connection.handle(message)
async def main(): blackbox_factory = BlockboxFactory()
async with serve(echo, "localhost", 8765):
await asyncio.Future()
if __name__ == "__main__": @app.post("/")
asyncio.run(main()) 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)