diff --git a/README.md b/README.md index a33c164..94a5496 100644 --- a/README.md +++ b/README.md @@ -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/ | \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index d306e74..0000000 --- a/main.py +++ /dev/null @@ -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.") \ No newline at end of file diff --git a/src/blackbox/audio_to_text.py b/src/blackbox/audio_to_text.py index ffea58b..321de64 100644 --- a/src/blackbox/audio_to_text.py +++ b/src/blackbox/audio_to_text.py @@ -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) \ No newline at end of file diff --git a/src/blackbox/blackbox.py b/src/blackbox/blackbox.py index c072e64..bc573ee 100644 --- a/src/blackbox/blackbox.py +++ b/src/blackbox/blackbox.py @@ -1,18 +1,21 @@ 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 - + @abstractmethod 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 \ No newline at end of file diff --git a/src/blackbox/blockbox_factory.py b/src/blackbox/blockbox_factory.py new file mode 100644 index 0000000..18d9c31 --- /dev/null +++ b/src/blackbox/blockbox_factory.py @@ -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") \ No newline at end of file diff --git a/src/main.py b/src/main.py index 1db254c..29bfe8e 100644 --- a/src/main.py +++ b/src/main.py @@ -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() -async def main(): - async with serve(echo, "localhost", 8765): - await asyncio.Future() +blackbox_factory = BlockboxFactory() -if __name__ == "__main__": - asyncio.run(main()) \ No newline at end of file +@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)