├── .gitignore ├── README.md ├── main.py ├── requirements.txt └── templates └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 105 | __pypackages__/ 106 | 107 | # Celery stuff 108 | celerybeat-schedule 109 | celerybeat.pid 110 | 111 | # SageMath parsed files 112 | *.sage.py 113 | 114 | # Environments 115 | .env 116 | .venv 117 | env/ 118 | venv/ 119 | ENV/ 120 | env.bak/ 121 | venv.bak/ 122 | 123 | # Spyder project settings 124 | .spyderproject 125 | .spyproject 126 | 127 | # Rope project settings 128 | .ropeproject 129 | 130 | # mkdocs documentation 131 | /site 132 | 133 | # mypy 134 | .mypy_cache/ 135 | .dmypy.json 136 | dmypy.json 137 | 138 | # Pyre type checker 139 | .pyre/ 140 | 141 | # pytype static type analyzer 142 | .pytype/ 143 | 144 | # Cython debug symbols 145 | cython_debug/ 146 | 147 | # PyCharm 148 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 149 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 150 | # and can be added to the global gitignore or merged into this file. For a more nuclear 151 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 152 | #.idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Live Transcription With Python and FastAPI 2 | 3 | To run this project create a virtual environment by running the below commands. You can learn more about setting up a virtual environment in this [article](https://developers.deepgram.com/blog/2022/02/python-virtual-environments/). 4 | 5 | ``` 6 | mkdir [% NAME_OF_YOUR_DIRECTORY %] 7 | cd [% NAME_OF_YOUR_DIRECTORY %] 8 | python3 -m venv venv 9 | source venv/bin/activate 10 | ``` 11 | 12 | Make sure your virtual environment is activated and install the dependencies in the requirements.txt file inside. 13 | 14 | ``` 15 | pip install -r requirements.txt 16 | ``` 17 | 18 | Make sure you're in the directory with the **main.py** file and run the project in the development server. 19 | 20 | ``` 21 | uvicorn main:app --reload 22 | ``` 23 | 24 | Pull up a browser and go to your localhost, http://127.0.0.1:8000/. 25 | 26 | Allow access to your microphone and start speaking. A transcript of your audio will appear in the browser. 27 | 28 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI, Request, WebSocket 2 | from fastapi.responses import HTMLResponse 3 | from fastapi.templating import Jinja2Templates 4 | from typing import Dict, Callable 5 | from deepgram import Deepgram 6 | from dotenv import load_dotenv 7 | import os 8 | 9 | load_dotenv() 10 | 11 | app = FastAPI() 12 | 13 | dg_client = Deepgram(os.getenv('DEEPGRAM_API_KEY')) 14 | 15 | templates = Jinja2Templates(directory="templates") 16 | 17 | async def process_audio(fast_socket: WebSocket): 18 | async def get_transcript(data: Dict) -> None: 19 | if 'channel' in data: 20 | transcript = data['channel']['alternatives'][0]['transcript'] 21 | 22 | if transcript: 23 | await fast_socket.send_text(transcript) 24 | 25 | deepgram_socket = await connect_to_deepgram(get_transcript) 26 | 27 | return deepgram_socket 28 | 29 | async def connect_to_deepgram(transcript_received_handler: Callable[[Dict], None]): 30 | try: 31 | socket = await dg_client.transcription.live({'punctuate': True, 'interim_results': False}) 32 | socket.registerHandler(socket.event.CLOSE, lambda c: print(f'Connection closed with code {c}.')) 33 | socket.registerHandler(socket.event.TRANSCRIPT_RECEIVED, transcript_received_handler) 34 | 35 | return socket 36 | except Exception as e: 37 | raise Exception(f'Could not open socket: {e}') 38 | 39 | @app.get("/", response_class=HTMLResponse) 40 | def get(request: Request): 41 | return templates.TemplateResponse("index.html", {"request": request}) 42 | 43 | @app.websocket("/listen") 44 | async def websocket_endpoint(websocket: WebSocket): 45 | await websocket.accept() 46 | 47 | try: 48 | deepgram_socket = await process_audio(websocket) 49 | 50 | while True: 51 | data = await websocket.receive_bytes() 52 | deepgram_socket.send(data) 53 | except Exception as e: 54 | raise Exception(f'Could not process audio: {e}') 55 | finally: 56 | await websocket.close() -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aiohttp==3.8.1 2 | aiosignal==1.2.0 3 | anyio==3.5.0 4 | asgiref==3.5.0 5 | async-timeout==4.0.2 6 | attrs==21.4.0 7 | certifi==2021.10.8 8 | charset-normalizer==2.0.12 9 | click==8.0.4 10 | deepgram-sdk==0.2.4 11 | dnspython==2.2.0 12 | email-validator==1.1.3 13 | fastapi==0.74.1 14 | frozenlist==1.3.0 15 | h11==0.13.0 16 | httptools==0.2.0 17 | idna==3.3 18 | itsdangerous==2.1.0 19 | Jinja2==3.0.3 20 | MarkupSafe==2.1.0 21 | multidict==6.0.2 22 | orjson==3.6.7 23 | pydantic==1.9.0 24 | python-dotenv==0.19.2 25 | python-multipart==0.0.5 26 | PyYAML==5.4.1 27 | requests==2.27.1 28 | six==1.16.0 29 | sniffio==1.2.0 30 | starlette==0.17.1 31 | typing_extensions==4.1.1 32 | ujson==4.3.0 33 | urllib3==1.26.8 34 | uvicorn==0.15.0 35 | uvloop==0.16.0 36 | watchgod==0.7 37 | websockets==10.2 38 | yarl==1.7.2 39 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |Connection status will go here
9 | 10 | 11 | 51 | 52 | --------------------------------------------------------------------------------