File size: 5,082 Bytes
7781557 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
import os
import base64
import io
import csv
import json
import datetime
import pandas as pd
from datetime import timedelta
from fastapi import FastAPI,WebSocket, Depends, HTTPException, status, UploadFile, File
from fastapi.responses import FileResponse, JSONResponse
from fastapi.requests import Request
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import OAuth2PasswordRequestForm
from .auth import (
get_current_user,
create_access_token,
verify_password,
get_password_hash,
)
from .db.models import User, Token, FileUpload
from .db.database import get_user_by_username, create_user, save_file, get_user_files
from .websocket import handle_websocket
from .llm_models import invoke_general_model, invoke_customer_search
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Get the directory of the current file
current_dir = os.path.dirname(os.path.realpath(__file__))
# Construct the path to the frontend dist directory
frontend_dir = os.path.join(current_dir, "..", "..", "frontend", "dist")
@app.get("/")
async def serve_react_root() -> FileResponse:
return FileResponse(os.path.join(frontend_dir, "index.html"))
@app.post("/register", response_model=Token)
async def register(user: User) -> Token:
if await get_user_by_username(user.username):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Username already registered"
)
hashed_password = get_password_hash(user.password)
user.password = hashed_password
if not await create_user(user):
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Could not create user"
)
access_token = create_access_token(
data={"sub": user.username},
expires_delta=timedelta(minutes=30)
)
return Token(access_token=access_token, token_type="bearer")
@app.post("/token", response_model=Token)
async def login(form_data: OAuth2PasswordRequestForm = Depends()) -> Token:
user = await get_user_by_username(form_data.username)
if not user or not verify_password(form_data.password, user.password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token = create_access_token(
data={"sub": user.username},
expires_delta=timedelta(minutes=30)
)
return Token(access_token=access_token, token_type="bearer")
@app.post("/upload")
async def upload_file(
file: UploadFile = File(...),
current_user: User = Depends(get_current_user)
) -> dict:
contents = await file.read()
# Convert to pandas DataFrame
df = pd.read_csv(io.StringIO(contents.decode('utf-8')))
# Convert DataFrame to list of dictionaries
records = json.loads(df.to_json(orient='records'))
# Insert into MongoDB
if not await save_file(current_user.username, records, file.filename):
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Could not save file"
)
return {"message": "File uploaded successfully"}
@app.get("/api/opportunities")
async def get_opportunities(request: Request, current_user: User = Depends(get_current_user)) -> dict:
records = await get_user_files(current_user.username)
print("records", records)
all_records = []
for record in records:
all_records.extend(record.content)
return {"records": all_records , "success": len(all_records) > 0}
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket) -> None:
await handle_websocket(websocket)
@app.post("/api/message")
async def message(obj: dict, current_user: User = Depends(get_current_user)) -> JSONResponse:
"""Endpoint to handle general incoming messages from the frontend."""
answer = invoke_general_model(obj["message"])
return JSONResponse(content={"message": answer.model_dump_json()})
@app.post("/api/customer_insights")
async def customer_insights(obj: dict) -> JSONResponse:
"""Endpoint to launch a customer insight search."""
answer = invoke_customer_search(obj["message"])
return JSONResponse(content={"AIMessage": answer.model_dump_json()})
app.mount("/assets", StaticFiles(directory=os.path.join(frontend_dir, "assets")), name="static")
if __name__ == "__main__":
from fastapi.testclient import TestClient
client = TestClient(app)
def test_message_endpoint():
# Test that the message endpoint returns answers to questions.
response = client.post("/api/message", json={"message": "What is MEDDPICC?"})
print(response.json())
assert response.status_code == 200
assert "AIMessage" in response.json()
test_message_endpoint() |