Spaces:
Sleeping
Sleeping
Yarik
commited on
Commit
·
a6f4e74
1
Parent(s):
15a7075
Add application file
Browse files
app.py
CHANGED
@@ -30,8 +30,8 @@ tts_kwargs = {
|
|
30 |
|
31 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
32 |
|
33 |
-
class
|
34 |
-
|
35 |
password: str
|
36 |
|
37 |
# Password hashing context
|
@@ -44,46 +44,46 @@ def verify_password(plain_password, hashed_password):
|
|
44 |
return pwd_context.verify(plain_password, hashed_password)
|
45 |
|
46 |
# Load username and password from environment variables
|
47 |
-
|
48 |
password = os.getenv("XCHE_PASSWORD")
|
49 |
|
50 |
# In-memory storage for simplicity; in production use a database
|
51 |
-
|
52 |
-
|
53 |
-
"
|
54 |
"password": get_password_hash(password) # Pre-hashed password
|
55 |
}
|
56 |
}
|
57 |
|
58 |
-
def
|
59 |
-
if
|
60 |
-
|
61 |
-
return
|
62 |
|
63 |
-
def
|
64 |
-
|
65 |
-
if not
|
66 |
return False
|
67 |
-
if not verify_password(password,
|
68 |
return False
|
69 |
-
return
|
70 |
|
71 |
@app.post("/token")
|
72 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
73 |
-
|
74 |
-
if not
|
75 |
raise HTTPException(
|
76 |
status_code=400,
|
77 |
-
detail="Incorrect
|
78 |
headers={"WWW-Authenticate": "Bearer"},
|
79 |
)
|
80 |
-
return {"access_token":
|
81 |
|
82 |
def check_api_token(token: str = Depends(oauth2_scheme)):
|
83 |
-
|
84 |
-
if not
|
85 |
raise HTTPException(status_code=403, detail="Invalid or missing API Key")
|
86 |
-
return
|
87 |
|
88 |
def trim_memory():
|
89 |
libc = ctypes.CDLL("libc.so.6")
|
@@ -98,7 +98,7 @@ def init_models():
|
|
98 |
return models
|
99 |
|
100 |
@app.post("/create_audio")
|
101 |
-
async def tts(request: str,
|
102 |
|
103 |
print(request)
|
104 |
|
|
|
30 |
|
31 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
32 |
|
33 |
+
class Auth(BaseModel):
|
34 |
+
api_key: str
|
35 |
password: str
|
36 |
|
37 |
# Password hashing context
|
|
|
44 |
return pwd_context.verify(plain_password, hashed_password)
|
45 |
|
46 |
# Load username and password from environment variables
|
47 |
+
api_key = os.getenv("XCHE_API_KEY")
|
48 |
password = os.getenv("XCHE_PASSWORD")
|
49 |
|
50 |
# In-memory storage for simplicity; in production use a database
|
51 |
+
fake_data_db = {
|
52 |
+
api_key: {
|
53 |
+
"api_key": api_key,
|
54 |
"password": get_password_hash(password) # Pre-hashed password
|
55 |
}
|
56 |
}
|
57 |
|
58 |
+
def get_api_key(db, api_key: str):
|
59 |
+
if api_key in db:
|
60 |
+
api_dict = db[api_key]
|
61 |
+
return Auth(**api_dict)
|
62 |
|
63 |
+
def authenticate(fake_db, api_key: str, password: str):
|
64 |
+
api_data = get_api_key(fake_db, api_key)
|
65 |
+
if not api_data:
|
66 |
return False
|
67 |
+
if not verify_password(password, api_data.password):
|
68 |
return False
|
69 |
+
return api_data
|
70 |
|
71 |
@app.post("/token")
|
72 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
73 |
+
api_data = authenticate(fake_data_db, form_data.api_key, form_data.password)
|
74 |
+
if not api_data:
|
75 |
raise HTTPException(
|
76 |
status_code=400,
|
77 |
+
detail="Incorrect API KEY or Password",
|
78 |
headers={"WWW-Authenticate": "Bearer"},
|
79 |
)
|
80 |
+
return {"access_token": api_data.api_key, "token_type": "bearer"}
|
81 |
|
82 |
def check_api_token(token: str = Depends(oauth2_scheme)):
|
83 |
+
api_data = get_api_key(fake_data_db, token)
|
84 |
+
if not api_data:
|
85 |
raise HTTPException(status_code=403, detail="Invalid or missing API Key")
|
86 |
+
return api_data
|
87 |
|
88 |
def trim_memory():
|
89 |
libc = ctypes.CDLL("libc.so.6")
|
|
|
98 |
return models
|
99 |
|
100 |
@app.post("/create_audio")
|
101 |
+
async def tts(request: str, api_data: Auth = Depends(check_api_token)):
|
102 |
|
103 |
print(request)
|
104 |
|