Mediocre-Judge's picture
Update main.py
0e72f5d verified
Raw
History Blame Contribute Delete
1.45 kB
from fastapi import FastAPI, UploadFile, Form, File
from rag import PlatinumPipeline
import hashlib
import json
import os
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
pipeline = PlatinumPipeline()
CREDENTIALS_FILE = 'credentials.json'
user_credentials = {}
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def load_credentials():
global user_credentials
with open(CREDENTIALS_FILE, 'r') as f:
user_credentials = json.load(f)
load_credentials()
def verify_password(stored_hash, stored_salt_hex, entered_password):
stored_salt = bytes.fromhex(stored_salt_hex)
salted_entered_password = stored_salt + entered_password.encode('utf-8')
hashed_entered_password = hashlib.sha256(salted_entered_password).hexdigest()
return stored_hash == hashed_entered_password
@app.get("/")
async def root():
return {"message": "WELCOME TO THE QA RAG FastAPI!!!"}
@app.post("/query")
async def query_rag(cred: str = Form(...), file: UploadFile = File(...)):
salt = os.environ.get("SALT")
if verify_password(user_credentials["PlatinumPluto"], salt, cred):
image_bytes = await file.read()
response = pipeline.query(image_bytes=image_bytes)
print(f"Response: {response}")
return {"response": response}
else:
return {"response": "Wrong Credential..."}