Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,8 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
| 9 |
from langchain_chroma import Chroma
|
| 10 |
from langchain.schema import HumanMessage, SystemMessage
|
| 11 |
from tiktoken import encoding_for_model
|
| 12 |
-
from fastapi import FastAPI,UploadFile,File, HTTPException
|
|
|
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
from pydantic import BaseModel
|
| 15 |
import uvicorn
|
|
@@ -35,7 +36,9 @@ try:
|
|
| 35 |
EXCELIS_USER = os.getenv("EXCELIS_USER")
|
| 36 |
EXCELIS_MDP = os.getenv("EXCELIS_MDP")
|
| 37 |
VECTORSTORE_BASE_DIR = "vectorstores"
|
|
|
|
| 38 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
print("Missing value of parameters", e)
|
| 41 |
raise
|
|
@@ -302,6 +305,10 @@ def classify_complaint(text):
|
|
| 302 |
return response.content.strip()
|
| 303 |
|
| 304 |
# -- API SECTION
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
# -- section FAST API
|
| 306 |
app_api = FastAPI()
|
| 307 |
|
|
@@ -327,7 +334,7 @@ class ChatRequest(BaseModel):
|
|
| 327 |
|
| 328 |
# -- Chatbot
|
| 329 |
@app_api.post("/chat")
|
| 330 |
-
def chat(request: ChatRequest):
|
| 331 |
try:
|
| 332 |
response = respond(
|
| 333 |
message=request.message,
|
|
@@ -345,7 +352,7 @@ def chat(request: ChatRequest):
|
|
| 345 |
|
| 346 |
# -- Extract ID CARD
|
| 347 |
@app_api.post("/extract-id-card-info")
|
| 348 |
-
async def extract_id_card_info_api(file: UploadFile = File(...)):
|
| 349 |
try:
|
| 350 |
contents = await file.read()
|
| 351 |
image = Image.open(BytesIO(contents))
|
|
|
|
| 9 |
from langchain_chroma import Chroma
|
| 10 |
from langchain.schema import HumanMessage, SystemMessage
|
| 11 |
from tiktoken import encoding_for_model
|
| 12 |
+
from fastapi import FastAPI,UploadFile,File, HTTPException,Security
|
| 13 |
+
from fastapi.security.api_key import APIKeyHeader
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
from pydantic import BaseModel
|
| 16 |
import uvicorn
|
|
|
|
| 36 |
EXCELIS_USER = os.getenv("EXCELIS_USER")
|
| 37 |
EXCELIS_MDP = os.getenv("EXCELIS_MDP")
|
| 38 |
VECTORSTORE_BASE_DIR = "vectorstores"
|
| 39 |
+
API_KEY = os.getenv("APIKEY")
|
| 40 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 41 |
+
api_key_header = APIKeyHeader(name="X-API-Key",auto_error=False)
|
| 42 |
except Exception as e:
|
| 43 |
print("Missing value of parameters", e)
|
| 44 |
raise
|
|
|
|
| 305 |
return response.content.strip()
|
| 306 |
|
| 307 |
# -- API SECTION
|
| 308 |
+
def validate_api_key(api_key: str = Security(api_key_header)):
|
| 309 |
+
if api_key != API_KEY:
|
| 310 |
+
raise HTTPException(status_code=403, detail="Clé API invalide")
|
| 311 |
+
|
| 312 |
# -- section FAST API
|
| 313 |
app_api = FastAPI()
|
| 314 |
|
|
|
|
| 334 |
|
| 335 |
# -- Chatbot
|
| 336 |
@app_api.post("/chat")
|
| 337 |
+
async def chat(request: ChatRequest,api_key: str = Security(validate_api_key)):
|
| 338 |
try:
|
| 339 |
response = respond(
|
| 340 |
message=request.message,
|
|
|
|
| 352 |
|
| 353 |
# -- Extract ID CARD
|
| 354 |
@app_api.post("/extract-id-card-info")
|
| 355 |
+
async def extract_id_card_info_api(file: UploadFile = File(...),api_key: str = Security(validate_api_key)):
|
| 356 |
try:
|
| 357 |
contents = await file.read()
|
| 358 |
image = Image.open(BytesIO(contents))
|