Spaces:
Sleeping
Sleeping
Yarik
commited on
Commit
·
dca572a
1
Parent(s):
7da5a9a
Add application file
Browse files
app.py
CHANGED
|
@@ -20,6 +20,7 @@ from passlib.context import CryptContext
|
|
| 20 |
|
| 21 |
app = FastAPI()
|
| 22 |
|
|
|
|
| 23 |
# Set environment variable for Hugging Face cache directory
|
| 24 |
os.environ["HF_HOME"] = "/app/.cache"
|
| 25 |
|
|
@@ -30,8 +31,8 @@ tts_kwargs = {
|
|
| 30 |
|
| 31 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
| 32 |
|
| 33 |
-
class
|
| 34 |
-
|
| 35 |
password: str
|
| 36 |
|
| 37 |
# Password hashing context
|
|
@@ -43,52 +44,45 @@ def get_password_hash(password):
|
|
| 43 |
def verify_password(plain_password, hashed_password):
|
| 44 |
return pwd_context.verify(plain_password, hashed_password)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
password = os.getenv("XCHE_SECRET_PASSWORD")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
print(username)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
fake_users_db = {
|
| 57 |
-
username: {
|
| 58 |
-
"username": username,
|
| 59 |
"password": get_password_hash(password) # Pre-hashed password
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
-
def
|
| 64 |
-
if
|
| 65 |
-
|
| 66 |
-
return
|
| 67 |
|
| 68 |
-
def
|
| 69 |
-
|
| 70 |
-
if not
|
| 71 |
return False
|
| 72 |
-
if not verify_password(password,
|
| 73 |
return False
|
| 74 |
-
return
|
| 75 |
|
| 76 |
@app.post("/token")
|
| 77 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
| 78 |
-
|
| 79 |
-
if not
|
| 80 |
raise HTTPException(
|
| 81 |
status_code=400,
|
| 82 |
-
detail="Incorrect
|
| 83 |
headers={"WWW-Authenticate": "Bearer"},
|
| 84 |
)
|
| 85 |
-
return {"access_token":
|
| 86 |
|
| 87 |
def check_api_token(token: str = Depends(oauth2_scheme)):
|
| 88 |
-
|
| 89 |
-
if not
|
| 90 |
-
raise HTTPException(status_code=403, detail="Invalid or missing API
|
| 91 |
-
return
|
| 92 |
|
| 93 |
def trim_memory():
|
| 94 |
libc = ctypes.CDLL("libc.so.6")
|
|
@@ -103,9 +97,7 @@ def init_models():
|
|
| 103 |
return models
|
| 104 |
|
| 105 |
@app.post("/create_audio")
|
| 106 |
-
async def tts(request: str,
|
| 107 |
-
|
| 108 |
-
print(request)
|
| 109 |
|
| 110 |
accented_text = accentification(request)
|
| 111 |
plussed_text = stress_replace_and_shift(accented_text)
|
|
|
|
| 20 |
|
| 21 |
app = FastAPI()
|
| 22 |
|
| 23 |
+
|
| 24 |
# Set environment variable for Hugging Face cache directory
|
| 25 |
os.environ["HF_HOME"] = "/app/.cache"
|
| 26 |
|
|
|
|
| 31 |
|
| 32 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
| 33 |
|
| 34 |
+
class Auth(BaseModel):
|
| 35 |
+
api_key: str
|
| 36 |
password: str
|
| 37 |
|
| 38 |
# Password hashing context
|
|
|
|
| 44 |
def verify_password(plain_password, hashed_password):
|
| 45 |
return pwd_context.verify(plain_password, hashed_password)
|
| 46 |
|
| 47 |
+
api_key = os.getenv("XCHE_API_KEY")
|
| 48 |
+
password = os.getenv("XCHE_PASSWORD")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
fake_data_db = {
|
| 51 |
+
api_key: {
|
| 52 |
+
"api_key": api_key,
|
|
|
|
|
|
|
|
|
|
| 53 |
"password": get_password_hash(password) # Pre-hashed password
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
+
def get_api_key(db, api_key: str):
|
| 58 |
+
if api_key in db:
|
| 59 |
+
api_dict = db[api_key]
|
| 60 |
+
return Auth(**api_dict)
|
| 61 |
|
| 62 |
+
def authenticate(fake_db, api_key: str, password: str):
|
| 63 |
+
api_key = get_api_key(fake_db, api_key)
|
| 64 |
+
if not api_key:
|
| 65 |
return False
|
| 66 |
+
if not verify_password(password, api_key.password):
|
| 67 |
return False
|
| 68 |
+
return api_key
|
| 69 |
|
| 70 |
@app.post("/token")
|
| 71 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
| 72 |
+
api_key = authenticate(fake_data_db, form_data.api_key, form_data.password)
|
| 73 |
+
if not api_key:
|
| 74 |
raise HTTPException(
|
| 75 |
status_code=400,
|
| 76 |
+
detail="Incorrect API KEY or Password",
|
| 77 |
headers={"WWW-Authenticate": "Bearer"},
|
| 78 |
)
|
| 79 |
+
return {"access_token": api_key.api_key, "token_type": "bearer"}
|
| 80 |
|
| 81 |
def check_api_token(token: str = Depends(oauth2_scheme)):
|
| 82 |
+
api_key = get_api_key(fake_data_db, token)
|
| 83 |
+
if not api_key:
|
| 84 |
+
raise HTTPException(status_code=403, detail="Invalid or missing API KEY")
|
| 85 |
+
return api_key
|
| 86 |
|
| 87 |
def trim_memory():
|
| 88 |
libc = ctypes.CDLL("libc.so.6")
|
|
|
|
| 97 |
return models
|
| 98 |
|
| 99 |
@app.post("/create_audio")
|
| 100 |
+
async def tts(request: str, api_key: Auth = Depends(check_api_token)):
|
|
|
|
|
|
|
| 101 |
|
| 102 |
accented_text = accentification(request)
|
| 103 |
plussed_text = stress_replace_and_shift(accented_text)
|