add a basic backend skeleton
Browse files- .github/workflows/lint.yml +21 -0
- backend/backend.py +90 -0
- backend/crappy_test.py +33 -0
- backend/requirements.txt +10 -0
.github/workflows/lint.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Lint with black
|
| 2 |
+
on:
|
| 3 |
+
push:
|
| 4 |
+
pull_request:
|
| 5 |
+
workflow_dispatch:
|
| 6 |
+
|
| 7 |
+
jobs:
|
| 8 |
+
build:
|
| 9 |
+
runs-on: ubuntu-latest
|
| 10 |
+
defaults:
|
| 11 |
+
run:
|
| 12 |
+
working-directory: backend
|
| 13 |
+
steps:
|
| 14 |
+
- name: setup
|
| 15 |
+
uses: actions/checkout@v3
|
| 16 |
+
- name: setup-python
|
| 17 |
+
uses: actions/setup-python@v3
|
| 18 |
+
- name: install black
|
| 19 |
+
run: pip install black
|
| 20 |
+
- name: lint
|
| 21 |
+
run: black --check .
|
backend/backend.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from firebase_admin import credentials, messaging, initialize_app, auth
|
| 4 |
+
import openai
|
| 5 |
+
import uuid
|
| 6 |
+
import asyncio
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# To store user information in memory.
|
| 11 |
+
# WARNING: This information will be lost when the server stops/restarts
|
| 12 |
+
user_data = {}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class RegisterItem(BaseModel):
|
| 16 |
+
apiKey: str
|
| 17 |
+
authDomain: str
|
| 18 |
+
databaseURL: str
|
| 19 |
+
storageBucket: str
|
| 20 |
+
openai_key: str
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@app.post("/register")
|
| 24 |
+
async def register(item: RegisterItem):
|
| 25 |
+
# Generate a unique user id
|
| 26 |
+
uid = str(uuid.uuid4())
|
| 27 |
+
|
| 28 |
+
# Initialize the Firebase app
|
| 29 |
+
user_data[uid] = {
|
| 30 |
+
"firebase": credentials.Certificate(
|
| 31 |
+
{
|
| 32 |
+
"apiKey": item.apiKey,
|
| 33 |
+
"authDomain": item.authDomain,
|
| 34 |
+
"databaseURL": item.databaseURL,
|
| 35 |
+
"storageBucket": item.storageBucket,
|
| 36 |
+
}
|
| 37 |
+
),
|
| 38 |
+
"openai_key": item.openai_key,
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
initialize_app(user_data[uid]["firebase"], name=uid)
|
| 42 |
+
|
| 43 |
+
return {"uid": uid}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class ProcessItem(BaseModel):
|
| 47 |
+
uid: str
|
| 48 |
+
prompt: str
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@app.post("/process_request")
|
| 52 |
+
async def process_request(item: ProcessItem):
|
| 53 |
+
# Get the user data using the provided uid
|
| 54 |
+
user = user_data.get(item.uid)
|
| 55 |
+
|
| 56 |
+
if not user:
|
| 57 |
+
raise HTTPException(status_code=400, detail="Invalid uid")
|
| 58 |
+
|
| 59 |
+
# Set the OpenAI key for this user
|
| 60 |
+
openai.api_key = user["openai_key"]
|
| 61 |
+
|
| 62 |
+
# Call OpenAI
|
| 63 |
+
response = openai.Completion.create(
|
| 64 |
+
engine="text-davinci-002", prompt=item.prompt, max_tokens=150
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# The message data that will be sent to the client
|
| 68 |
+
message = messaging.Message(
|
| 69 |
+
data={
|
| 70 |
+
"message": response.choices[0].text.strip(),
|
| 71 |
+
},
|
| 72 |
+
topic="updates",
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Send the message asynchronously
|
| 76 |
+
asyncio.run(send_notification(message, item.uid))
|
| 77 |
+
|
| 78 |
+
return {"message": "Notification sent"}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def send_notification(message, uid):
|
| 82 |
+
# Send a message to the devices subscribed to the provided topic.
|
| 83 |
+
response = messaging.send(message, app=user_data[uid]["firebase"])
|
| 84 |
+
print("Successfully sent message:", response)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
import uvicorn
|
| 89 |
+
|
| 90 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
backend/crappy_test.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def test_register():
|
| 6 |
+
data = {
|
| 7 |
+
"apiKey": "test-api-key",
|
| 8 |
+
"authDomain": "test-auth-domain",
|
| 9 |
+
"databaseURL": "test-database-url",
|
| 10 |
+
"storageBucket": "test-storage-bucket",
|
| 11 |
+
"openai_key": "test-openai-key",
|
| 12 |
+
}
|
| 13 |
+
response = requests.post(
|
| 14 |
+
"http://localhost:8000/register",
|
| 15 |
+
data=json.dumps(data),
|
| 16 |
+
headers={"Content-Type": "application/json"},
|
| 17 |
+
)
|
| 18 |
+
assert response.status_code == 200
|
| 19 |
+
uid = response.json().get("uid")
|
| 20 |
+
assert uid is not None
|
| 21 |
+
|
| 22 |
+
# Let's use this uid to send a process request
|
| 23 |
+
data = {
|
| 24 |
+
"uid": uid,
|
| 25 |
+
"prompt": "Hello, OpenAI!",
|
| 26 |
+
}
|
| 27 |
+
response = requests.post(
|
| 28 |
+
"http://localhost:8000/process_request",
|
| 29 |
+
data=json.dumps(data),
|
| 30 |
+
headers={"Content-Type": "application/json"},
|
| 31 |
+
)
|
| 32 |
+
assert response.status_code == 200
|
| 33 |
+
assert response.json().get("message") == "Notification sent"
|
backend/requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pytest
|
| 2 |
+
requests
|
| 3 |
+
fastapi
|
| 4 |
+
firebase-admin
|
| 5 |
+
gradio
|
| 6 |
+
openai
|
| 7 |
+
pinecone-io
|
| 8 |
+
pydantic
|
| 9 |
+
python-dotenv
|
| 10 |
+
swagger-ui-bundle
|