Pujan-Dev commited on
Commit
753c2d1
·
1 Parent(s): 90debd3

refact: added the handshake part

Browse files
.env-example ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ MY_SECRET_TOKEN="SECRET_CODE_TOKEN"
2
+
features/text_classifier/controller.py CHANGED
@@ -1,13 +1,27 @@
1
  from .inferencer import classify_text
2
  import asyncio
3
- from fastapi import HTTPException, UploadFile
 
4
  from .preprocess import parse_docx, parse_pdf, parse_txt
5
  from nltk.tokenize import sent_tokenize
6
-
7
  from io import BytesIO
8
  import logging
9
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  async def handle_text_analysis(text: str):
13
  text = text.strip()
 
1
  from .inferencer import classify_text
2
  import asyncio
3
+ from fastapi import HTTPException, UploadFile,status,Depends
4
+ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
5
  from .preprocess import parse_docx, parse_pdf, parse_txt
6
  from nltk.tokenize import sent_tokenize
7
+ import os
8
  from io import BytesIO
9
  import logging
10
 
11
 
12
+ security = HTTPBearer()
13
+
14
+ async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
15
+ token = credentials.credentials
16
+ if token != os.getenv("MY_SECRET_TOKEN"): # Replace with your actual secret
17
+ raise HTTPException(
18
+ status_code=status.HTTP_403_FORBIDDEN,
19
+ detail="Invalid or expired token"
20
+ )
21
+ return token
22
+
23
+
24
+
25
 
26
  async def handle_text_analysis(text: str):
27
  text = text.strip()
features/text_classifier/routes.py CHANGED
@@ -1,24 +1,14 @@
1
- from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, status
2
- from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
3
  from pydantic import BaseModel
4
  from .controller import handle_text_analysis, handle_file_upload, handle_sentence_level_analysis, handle_file_sentance
5
- import os
6
-
7
  router = APIRouter()
8
  security = HTTPBearer()
9
 
10
  class TextInput(BaseModel):
11
  text: str
12
 
13
- async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
14
- token = credentials.credentials
15
- if token != os.getenv("MY_SECRET_TOKEN"): # Replace with your actual secret
16
- raise HTTPException(
17
- status_code=status.HTTP_403_FORBIDDEN,
18
- detail="Invalid or expired token"
19
- )
20
- return token
21
-
22
  @router.post("/analyse")
23
  async def analyze(data: TextInput, token: str = Depends(verify_token)):
24
  return await handle_text_analysis(data.text)
 
1
+ from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
2
+ from fastapi.security import HTTPBearer
3
  from pydantic import BaseModel
4
  from .controller import handle_text_analysis, handle_file_upload, handle_sentence_level_analysis, handle_file_sentance
5
+ from .controller import verify_token
 
6
  router = APIRouter()
7
  security = HTTPBearer()
8
 
9
  class TextInput(BaseModel):
10
  text: str
11
 
 
 
 
 
 
 
 
 
 
12
  @router.post("/analyse")
13
  async def analyze(data: TextInput, token: str = Depends(verify_token)):
14
  return await handle_text_analysis(data.text)