Skip to content

Commit 92c55f5

Browse files
committed
initial public
0 parents  commit 92c55f5

12 files changed

+551
-0
lines changed

.gitignore

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
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+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
113+
# Spyder project settings
114+
.spyderproject
115+
.spyproject
116+
117+
# Rope project settings
118+
.ropeproject
119+
120+
# mkdocs documentation
121+
/site
122+
123+
# mypy
124+
.mypy_cache/
125+
.dmypy.json
126+
dmypy.json
127+
128+
# Pyre type checker
129+
.pyre/
130+
131+
.idea
132+
133+
.DS_Store

Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.10-slim
2+
3+
WORKDIR /
4+
5+
ENV MAX_WORKERS=5
6+
7+
COPY ./requirements.txt /app/requirements.txt
8+
9+
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
10+
11+
RUN apt-get update && apt-get install -y ffmpeg
12+
13+
COPY ./app /app

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# The Ultimate AI Assistant
2+
3+
Now you can use ChatGPT from anywhere! Talk to ChatGPT when driving in the car. Bounce ideas off the ultimate
4+
personal assistant.
5+
6+
To get it running:
7+
```
8+
(while inside ultimate-ai-assistant project root directory)
9+
docker build -t ultimate_ai_assistant .
10+
docker run -d -e OPENAI_API_KEY='<YOUR_API_KEY>' -p 8000:80 ultimate_ai_assistant
11+
```
12+
13+
and then navigate to `localhost:8000`
14+
15+
16+
Check out the demo at https://ultimate-ai-assistant.up.railway.app/
17+
18+
Star the repo if you like it!

app/main.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import json
2+
import time
3+
from fastapi import FastAPI, UploadFile, BackgroundTasks, Header
4+
from fastapi.responses import FileResponse
5+
import os
6+
import openai
7+
import shutil
8+
import uuid
9+
from gtts import gTTS
10+
import ffmpeg
11+
import base64
12+
from fastapi.staticfiles import StaticFiles
13+
14+
AI_COMPLETION_MODEL = "gpt-3.5-turbo"
15+
app = FastAPI()
16+
17+
18+
@app.post("/inference")
19+
async def infer(audio: UploadFile, background_tasks: BackgroundTasks,
20+
conversation: str = Header(default=None)) -> FileResponse:
21+
print("received request")
22+
start_time = time.time()
23+
24+
user_prompt = await transcribe(audio)
25+
ai_response = await get_completion(user_prompt, conversation)
26+
27+
output_audio_filepath = to_audio(ai_response)
28+
background_tasks.add_task(delete_file, output_audio_filepath)
29+
30+
print('total processing time:', time.time() - start_time, 'seconds')
31+
32+
return FileResponse(path=output_audio_filepath, media_type="audio/mpeg",
33+
headers={"text": construct_response_header(user_prompt, ai_response)})
34+
35+
36+
app.mount("/", StaticFiles(directory="app/static", html=True), name="static")
37+
38+
39+
async def transcribe(audio):
40+
start_time = time.time()
41+
initial_filepath = f"/tmp/{uuid.uuid4()}{audio.filename}"
42+
43+
with open(initial_filepath, "wb+") as file_object:
44+
shutil.copyfileobj(audio.file, file_object)
45+
46+
converted_filepath = f"/tmp/ffmpeg-{uuid.uuid4()}{audio.filename}"
47+
48+
print("running through ffmpeg")
49+
(
50+
ffmpeg
51+
.input(initial_filepath)
52+
.output(converted_filepath, loglevel="error")
53+
.run()
54+
)
55+
print("ffmpeg done")
56+
57+
delete_file(initial_filepath)
58+
59+
read_file = open(converted_filepath, "rb")
60+
61+
print("calling whisper")
62+
transcription = (await openai.Audio.atranscribe("whisper-1", read_file))["text"]
63+
print("STT response received from whisper in", time.time() - start_time, 'seconds')
64+
print('user prompt:', transcription)
65+
66+
delete_file(converted_filepath)
67+
68+
return transcription
69+
70+
71+
async def get_completion(user_prompt, conversation_thus_far):
72+
start_time = time.time()
73+
messages = [
74+
{"role": "system", "content": "You are a helpful assistant."},
75+
{"role": "user",
76+
"content": "You are a helpful assistant with a voice interface. Keep your responses limited to a single sentence of reasonable length. Make sure your response is in English, regardless of the language that the user's input is in."},
77+
]
78+
79+
messages.extend(json.loads(base64.b64decode(conversation_thus_far)))
80+
81+
messages.append({"role": "user", "content": user_prompt})
82+
83+
print("calling", AI_COMPLETION_MODEL)
84+
res = await openai.ChatCompletion.acreate(model=AI_COMPLETION_MODEL, messages=messages, timeout=15)
85+
print("response received from", AI_COMPLETION_MODEL, "in", time.time() - start_time, "seconds")
86+
87+
completion = res['choices'][0]['message']['content']
88+
print(AI_COMPLETION_MODEL, "response:", completion)
89+
90+
return completion
91+
92+
93+
def to_audio(text):
94+
start_time = time.time()
95+
96+
tts = gTTS(text)
97+
filepath = f"/tmp/{uuid.uuid4()}.mp3"
98+
tts.save(filepath)
99+
100+
print('TTS time:', time.time() - start_time, 'seconds')
101+
return filepath
102+
103+
104+
def delete_file(filepath: str):
105+
os.remove(filepath)
106+
107+
108+
def construct_response_header(user_prompt, ai_response):
109+
return base64.b64encode(
110+
json.dumps(
111+
[{"role": "user", "content": user_prompt}, {"role": "assistant", "content": ai_response}]).encode(
112+
'utf-8')).decode("utf-8")

app/static/favicon-16.png

347 Bytes
Loading

app/static/favicon-32.png

631 Bytes
Loading

app/static/favicon-96.png

1.61 KB
Loading

app/static/index.html

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<link href="style.css" rel="stylesheet" type="text/css">
5+
<script src="https://kit.fontawesome.com/80276f8bce.js" crossorigin="anonymous"></script>
6+
<script type="text/javascript" src="script.js"></script>
7+
<!-- Cloudflare Web Analytics -->
8+
<script defer src='https://static.cloudflareinsights.com/beacon.min.js'
9+
data-cf-beacon='{"token": "2929d361bab44c8392e78bf5421d742f"}'></script>
10+
<!-- End Cloudflare Web Analytics -->
11+
<link rel="icon" type="image/png" sizes="16x16" href="./favicon-16.png">
12+
<link rel="icon" type="image/png" sizes="32x32" href="./favicon-32.png">
13+
<link rel="icon" type="image/png" sizes="96x96" href="./favicon-96.png">
14+
</head>
15+
<meta charset="UTF-8">
16+
<title>Record audio</title>
17+
</head>
18+
<body>
19+
<div id="container">
20+
<button id="record-button"><i class="fas fa-microphone"></i></button>
21+
<div id="error-message">Error occurred</div>
22+
<div id="spinner"></div>
23+
</div>
24+
</body>
25+
</html>

0 commit comments

Comments
 (0)