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