Spaces:
Sleeping
Sleeping
Commit
·
15adf17
1
Parent(s):
72b52fb
initial commit
Browse files- Dockerfile +20 -0
- Makefile +16 -0
- README.md +2 -10
- app.py +29 -0
- apps/__init__.py +0 -0
- apps/__pycache__/__init__.cpython-38.pyc +0 -0
- apps/demand_assessment/__init__.py +0 -0
- apps/demand_assessment/__pycache__/__init__.cpython-38.pyc +0 -0
- apps/demand_assessment/__pycache__/main.cpython-38.pyc +0 -0
- apps/demand_assessment/main.py +12 -0
- authentication.py +87 -0
- main_backup.py +8 -0
- requirements.txt +8 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.8
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
RUN useradd -m -u 1000 user
|
10 |
+
|
11 |
+
USER user
|
12 |
+
|
13 |
+
ENV HOME=/home/user \
|
14 |
+
PATH=/home/user/.local/bin:$PATH
|
15 |
+
|
16 |
+
WORKDIR $HOME/app
|
17 |
+
|
18 |
+
COPY --chown=user . $HOME/app
|
19 |
+
|
20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
Makefile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Makefile
|
2 |
+
|
3 |
+
create_environment:
|
4 |
+
conda create --name searchapi_env python=3.8
|
5 |
+
|
6 |
+
activate_environment:
|
7 |
+
conda activate searchapi_env
|
8 |
+
|
9 |
+
install_packages:
|
10 |
+
pip install -r requirements.txt
|
11 |
+
|
12 |
+
run_app:
|
13 |
+
uvicorn app:app --reload
|
14 |
+
|
15 |
+
setup_and_run:
|
16 |
+
create_environment activate_environment install_packages run_app
|
README.md
CHANGED
@@ -1,10 +1,2 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
emoji: 📉
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: green
|
6 |
-
sdk: docker
|
7 |
-
pinned: false
|
8 |
-
---
|
9 |
-
|
10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
## Search API
|
2 |
+
- Demand Assessment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import timedelta
|
2 |
+
from fastapi import Depends, FastAPI, HTTPException, status
|
3 |
+
from fastapi.security import OAuth2PasswordRequestForm
|
4 |
+
from authentication import authenticate_user, Token, fake_users_db, ACCESS_TOKEN_EXPIRE_MINUTES, create_access_token, get_current_user
|
5 |
+
|
6 |
+
from apps.demand_assessment.main import router as DemandAssessmentRouter
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
app.include_router(DemandAssessmentRouter, prefix="/demand_assessment", tags=["demand_assessment"])
|
11 |
+
|
12 |
+
@app.post("/token", response_model=Token)
|
13 |
+
def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
14 |
+
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
15 |
+
if not user:
|
16 |
+
raise HTTPException(
|
17 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
18 |
+
detail="Incorrect username or password",
|
19 |
+
headers={"WWW-Authenticate": "Bearer"},
|
20 |
+
)
|
21 |
+
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
22 |
+
access_token = create_access_token(
|
23 |
+
data={"sub": user["username"]}, expires_delta=access_token_expires
|
24 |
+
)
|
25 |
+
return {"access_token": access_token, "token_type": "bearer"}
|
26 |
+
|
27 |
+
@app.get("/users/me")
|
28 |
+
async def read_users_me(current_user: str = Depends(get_current_user)):
|
29 |
+
return current_user
|
apps/__init__.py
ADDED
File without changes
|
apps/__pycache__/__init__.cpython-38.pyc
ADDED
Binary file (155 Bytes). View file
|
|
apps/demand_assessment/__init__.py
ADDED
File without changes
|
apps/demand_assessment/__pycache__/__init__.cpython-38.pyc
ADDED
Binary file (173 Bytes). View file
|
|
apps/demand_assessment/__pycache__/main.cpython-38.pyc
ADDED
Binary file (575 Bytes). View file
|
|
apps/demand_assessment/main.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import Depends, HTTPException, status
|
2 |
+
from fastapi import APIRouter
|
3 |
+
from authentication import get_current_user
|
4 |
+
|
5 |
+
router = APIRouter()
|
6 |
+
|
7 |
+
|
8 |
+
@router.get("/")
|
9 |
+
def read_demand_assessment(current_user: str = Depends(get_current_user)):
|
10 |
+
return {"Status": "Alive!!! Hello from Demand Assessment"}
|
11 |
+
|
12 |
+
|
authentication.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime, timedelta
|
2 |
+
from jose import JWTError, jwt
|
3 |
+
from passlib.context import CryptContext
|
4 |
+
from fastapi import Depends, FastAPI, HTTPException, status
|
5 |
+
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
6 |
+
from typing import Optional
|
7 |
+
from pydantic import BaseModel
|
8 |
+
|
9 |
+
|
10 |
+
# to get a string like this run:
|
11 |
+
# openssl rand -hex 32
|
12 |
+
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
|
13 |
+
ALGORITHM = "HS256"
|
14 |
+
ACCESS_TOKEN_EXPIRE_MINUTES = 60
|
15 |
+
|
16 |
+
fake_users_db = {
|
17 |
+
"abhisheky127": {
|
18 |
+
"username": "abhisheky127",
|
19 |
+
"full_name": "Abhishek Yadav",
|
20 |
+
"email": "[email protected]",
|
21 |
+
"hashed_password": "$2b$12$MaEi1YGqfFyp.lf0IR6mJulnX.HaT/ZhyT4vvy8LAzRPo.tHYmOoS",
|
22 |
+
"disabled": False,
|
23 |
+
}
|
24 |
+
}
|
25 |
+
|
26 |
+
class TokenData(BaseModel):
|
27 |
+
username: Optional[str] = None
|
28 |
+
|
29 |
+
class Token(BaseModel):
|
30 |
+
access_token: str
|
31 |
+
token_type: str
|
32 |
+
|
33 |
+
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
34 |
+
|
35 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
36 |
+
|
37 |
+
|
38 |
+
def verify_password(plain_password, hashed_password):
|
39 |
+
return pwd_context.verify(plain_password, hashed_password)
|
40 |
+
|
41 |
+
def get_password_hash(password):
|
42 |
+
return pwd_context.hash(password)
|
43 |
+
|
44 |
+
def get_user(db, username: str):
|
45 |
+
if username in db:
|
46 |
+
user_dict = db[username]
|
47 |
+
return user_dict
|
48 |
+
|
49 |
+
def authenticate_user(fake_db, username: str, password: str):
|
50 |
+
user = get_user(fake_db, username)
|
51 |
+
if not user:
|
52 |
+
return False
|
53 |
+
if not verify_password(password, user["hashed_password"]):
|
54 |
+
return False
|
55 |
+
return user
|
56 |
+
|
57 |
+
|
58 |
+
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
59 |
+
to_encode = data.copy()
|
60 |
+
if expires_delta:
|
61 |
+
expire = datetime.utcnow() + expires_delta
|
62 |
+
else:
|
63 |
+
expire = datetime.utcnow() + timedelta(minutes=15)
|
64 |
+
to_encode.update({"exp": expire})
|
65 |
+
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
66 |
+
return encoded_jwt
|
67 |
+
|
68 |
+
|
69 |
+
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
70 |
+
credentials_exception = HTTPException(
|
71 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
72 |
+
detail="Could not validate credentials",
|
73 |
+
headers={"WWW-Authenticate": "Bearer"},
|
74 |
+
)
|
75 |
+
try:
|
76 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
77 |
+
username: str = payload.get("sub")
|
78 |
+
if username is None:
|
79 |
+
raise credentials_exception
|
80 |
+
token_data = TokenData(username=username)
|
81 |
+
except JWTError:
|
82 |
+
raise credentials_exception
|
83 |
+
user = get_user(fake_users_db, username=token_data.username)
|
84 |
+
if user is None:
|
85 |
+
raise credentials_exception
|
86 |
+
return user
|
87 |
+
|
main_backup.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from apps.demand_assessment.main import router as DemandAssessmentRouter
|
3 |
+
|
4 |
+
app = FastAPI()
|
5 |
+
|
6 |
+
app.include_router(DemandAssessmentRouter, prefix="/api/demand_assessment", tags=["demand_assessment"])
|
7 |
+
|
8 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
python-jose[cryptography]
|
4 |
+
passlib[bcrypt]
|
5 |
+
python-multipart
|
6 |
+
pandas
|
7 |
+
numpy
|
8 |
+
|