logeswari commited on
Commit
94287ed
·
1 Parent(s): d620167
Files changed (2) hide show
  1. main.py +10 -3
  2. requirements.txt +1 -4
main.py CHANGED
@@ -3,6 +3,7 @@ from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
3
  from jose import JWTError, jwt
4
  from pinecone import Pinecone
5
  import os
 
6
  from PIL import Image
7
  import io
8
  from transformers import AutoProcessor, CLIPModel
@@ -10,6 +11,7 @@ import numpy as np
10
  from datetime import datetime, timedelta
11
 
12
  # Load environment variables
 
13
 
14
  # Secret key for JWT
15
  ALGORITHM = "HS256"
@@ -26,8 +28,13 @@ fake_users_db = {
26
  # Initialize FastAPI
27
  app = FastAPI()
28
 
 
 
 
 
 
29
  # Initialize Pinecone
30
- pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
31
  index_name = "images-index"
32
  unsplash_index = pc.Index(index_name)
33
 
@@ -42,7 +49,7 @@ def create_access_token(data: dict, expires_delta: timedelta = None):
42
  to_encode = data.copy()
43
  expire = datetime.utcnow() + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
44
  to_encode.update({"exp": expire})
45
- return jwt.encode(to_encode, algorithm=ALGORITHM)
46
 
47
  def authenticate_user(username: str, password: str):
48
  user = fake_users_db.get(username)
@@ -52,7 +59,7 @@ def authenticate_user(username: str, password: str):
52
 
53
  def get_current_user(token: str = Depends(oauth2_scheme)):
54
  try:
55
- payload = jwt.decode(token, algorithms=[ALGORITHM])
56
  username: str = payload.get("sub")
57
  if username is None or username not in fake_users_db:
58
  raise HTTPException(status_code=401, detail="Invalid authentication")
 
3
  from jose import JWTError, jwt
4
  from pinecone import Pinecone
5
  import os
6
+ from dotenv import load_dotenv
7
  from PIL import Image
8
  import io
9
  from transformers import AutoProcessor, CLIPModel
 
11
  from datetime import datetime, timedelta
12
 
13
  # Load environment variables
14
+ load_dotenv()
15
 
16
  # Secret key for JWT
17
  ALGORITHM = "HS256"
 
28
  # Initialize FastAPI
29
  app = FastAPI()
30
 
31
+ # Load Pinecone API key
32
+ PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
33
+ if not PINECONE_API_KEY:
34
+ raise RuntimeError("PINECONE_API_KEY is not set. Please set it in the environment or .env file.")
35
+
36
  # Initialize Pinecone
37
+ pc = Pinecone(api_key=PINECONE_API_KEY)
38
  index_name = "images-index"
39
  unsplash_index = pc.Index(index_name)
40
 
 
49
  to_encode = data.copy()
50
  expire = datetime.utcnow() + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
51
  to_encode.update({"exp": expire})
52
+ return jwt.encode(to_encode, "secret", algorithm=ALGORITHM)
53
 
54
  def authenticate_user(username: str, password: str):
55
  user = fake_users_db.get(username)
 
59
 
60
  def get_current_user(token: str = Depends(oauth2_scheme)):
61
  try:
62
+ payload = jwt.decode(token, "secret", algorithms=[ALGORITHM])
63
  username: str = payload.get("sub")
64
  if username is None or username not in fake_users_db:
65
  raise HTTPException(status_code=401, detail="Invalid authentication")
requirements.txt CHANGED
@@ -6,8 +6,5 @@ transformers
6
  torch
7
  numpy
8
  pinecone
9
- pyjwt
10
- passlib[bcrypt]
11
- python-multipart
12
  python-jose[cryptography]
13
-
 
6
  torch
7
  numpy
8
  pinecone
 
 
 
9
  python-jose[cryptography]
10
+ python-multipart