jwt works the routes are secured!
Browse files- App/Analytics/Model.py +1 -1
- App/Transcription/Model.py +1 -1
- App/Transcription/TranscriptionRoutes.py +7 -10
- App/Users/Model.py +1 -1
- App/Users/Schemas.py +9 -0
- App/Users/UserRoutes.py +38 -8
- App/__main__.py +2 -22
- App/app.py +14 -3
- App/modelInit.py +1 -11
- Dockerfile +1 -1
- requirements.txt +2 -0
App/Analytics/Model.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import asyncio
|
2 |
-
|
3 |
import psycopg2
|
4 |
import datetime
|
5 |
import pydantic
|
|
|
1 |
import asyncio
|
2 |
+
import orm
|
3 |
import psycopg2
|
4 |
import datetime
|
5 |
import pydantic
|
App/Transcription/Model.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
import datetime
|
3 |
from App.modelInit import database, models
|
4 |
from App.Users.Model import User
|
|
|
1 |
+
import orm
|
2 |
import datetime
|
3 |
from App.modelInit import database, models
|
4 |
from App.Users.Model import User
|
App/Transcription/TranscriptionRoutes.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import APIRouter, status,
|
2 |
from typing_extensions import Annotated
|
3 |
from .Schemas import UserDetails
|
4 |
from App import bot
|
@@ -7,9 +7,12 @@ import tempfile
|
|
7 |
from celery.result import AsyncResult
|
8 |
from App.Worker import transcription_task, downloadfile
|
9 |
from App.Users.Model import User
|
|
|
|
|
10 |
from .Model import Transcriptions
|
11 |
from .Utils.fastapi_tasks import perform_background_task
|
12 |
import yt_dlp
|
|
|
13 |
|
14 |
# from .Model import User
|
15 |
# from sqlalchemy import and_
|
@@ -27,8 +30,8 @@ async def download_audio(
|
|
27 |
enum=["tiny", "small", "medium", "base", "large-v2"],
|
28 |
description="Whisper model Sizes",
|
29 |
),
|
|
|
30 |
):
|
31 |
-
user = await User.objects.filter(id=userId).first()
|
32 |
if user == None:
|
33 |
return {"code": 400, "message": "doesn't exist", "payload": None}
|
34 |
|
@@ -55,26 +58,20 @@ async def download_audio(
|
|
55 |
transcription_enrty = await Transcriptions.objects.create(
|
56 |
task_id=task.id, user=user
|
57 |
)
|
58 |
-
return {
|
59 |
-
"task_id": task.id,
|
60 |
-
}
|
61 |
|
62 |
|
63 |
@transcription_router.post("/uploadfile/")
|
64 |
async def create_file(
|
65 |
background_tasks: BackgroundTasks,
|
66 |
file: UploadFile,
|
67 |
-
userId: int = 1,
|
68 |
model: str = Query(
|
69 |
"tiny",
|
70 |
enum=["tiny", "small", "medium", "base", "large-v2"],
|
71 |
description="Whisper model Sizes",
|
72 |
),
|
|
|
73 |
):
|
74 |
-
user = await User.objects.filter(id=userId).first()
|
75 |
-
if user == None:
|
76 |
-
return {"code": 400, "message": "doesn't exist", "payload": None}
|
77 |
-
|
78 |
# Write the file to disk asynchronously
|
79 |
try:
|
80 |
async with aiofiles.open(file.filename, "wb") as f:
|
|
|
1 |
+
from fastapi import APIRouter, status, Depends, UploadFile, File, Query, BackgroundTasks
|
2 |
from typing_extensions import Annotated
|
3 |
from .Schemas import UserDetails
|
4 |
from App import bot
|
|
|
7 |
from celery.result import AsyncResult
|
8 |
from App.Worker import transcription_task, downloadfile
|
9 |
from App.Users.Model import User
|
10 |
+
from App.Users.UserRoutes import get_token_owner
|
11 |
+
from App.Users.Schemas import UserSchema
|
12 |
from .Model import Transcriptions
|
13 |
from .Utils.fastapi_tasks import perform_background_task
|
14 |
import yt_dlp
|
15 |
+
from fastapi_jwt_auth import AuthJWT
|
16 |
|
17 |
# from .Model import User
|
18 |
# from sqlalchemy import and_
|
|
|
30 |
enum=["tiny", "small", "medium", "base", "large-v2"],
|
31 |
description="Whisper model Sizes",
|
32 |
),
|
33 |
+
user: UserSchema = Depends(get_token_owner),
|
34 |
):
|
|
|
35 |
if user == None:
|
36 |
return {"code": 400, "message": "doesn't exist", "payload": None}
|
37 |
|
|
|
58 |
transcription_enrty = await Transcriptions.objects.create(
|
59 |
task_id=task.id, user=user
|
60 |
)
|
61 |
+
return {"task_id": task.id, "file_name": filename}
|
|
|
|
|
62 |
|
63 |
|
64 |
@transcription_router.post("/uploadfile/")
|
65 |
async def create_file(
|
66 |
background_tasks: BackgroundTasks,
|
67 |
file: UploadFile,
|
|
|
68 |
model: str = Query(
|
69 |
"tiny",
|
70 |
enum=["tiny", "small", "medium", "base", "large-v2"],
|
71 |
description="Whisper model Sizes",
|
72 |
),
|
73 |
+
user: UserSchema = Depends(get_token_owner),
|
74 |
):
|
|
|
|
|
|
|
|
|
75 |
# Write the file to disk asynchronously
|
76 |
try:
|
77 |
async with aiofiles.open(file.filename, "wb") as f:
|
App/Users/Model.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import asyncio
|
2 |
-
|
3 |
import psycopg2
|
4 |
import datetime
|
5 |
import pydantic
|
|
|
1 |
import asyncio
|
2 |
+
import orm
|
3 |
import psycopg2
|
4 |
import datetime
|
5 |
import pydantic
|
App/Users/Schemas.py
CHANGED
@@ -5,6 +5,15 @@ from passlib.context import CryptContext
|
|
5 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
6 |
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
class BaseRequest(BaseModel):
|
9 |
email: EmailStr
|
10 |
name: str
|
|
|
5 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
6 |
|
7 |
|
8 |
+
class UserSchema(BaseModel):
|
9 |
+
pk: int
|
10 |
+
id: int
|
11 |
+
name: str
|
12 |
+
|
13 |
+
class Config:
|
14 |
+
orm_mode = True
|
15 |
+
|
16 |
+
|
17 |
class BaseRequest(BaseModel):
|
18 |
email: EmailStr
|
19 |
name: str
|
App/Users/UserRoutes.py
CHANGED
@@ -1,13 +1,23 @@
|
|
1 |
-
from fastapi import APIRouter,
|
2 |
-
from .Schemas import BaseRequest
|
|
|
3 |
from .Model import User
|
4 |
from sqlalchemy import and_
|
5 |
-
from
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
user_router = APIRouter(tags=["User"])
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
@user_router.post("/user/register")
|
12 |
async def register_user(user: BaseRequest):
|
13 |
data = await User.objects.filter(email=user.email).first()
|
@@ -19,10 +29,30 @@ async def register_user(user: BaseRequest):
|
|
19 |
return {"code": 200, "message": "success", "payload": None}
|
20 |
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
@user_router.post("/user/login")
|
23 |
-
async def register_user(user: BaseRequest):
|
24 |
db_user = await User.objects.filter(email=user.email).first()
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Request, Depends, HTTPException
|
2 |
+
from .Schemas import BaseRequest, UserSchema
|
3 |
+
from pydantic import BaseModel
|
4 |
from .Model import User
|
5 |
from sqlalchemy import and_
|
6 |
+
from fastapi_jwt_auth import AuthJWT
|
7 |
+
|
8 |
+
|
9 |
+
class Settings(BaseModel):
|
10 |
+
authjwt_secret_key: str = "secret"
|
11 |
|
12 |
|
13 |
user_router = APIRouter(tags=["User"])
|
14 |
|
15 |
|
16 |
+
@AuthJWT.load_config
|
17 |
+
def get_config():
|
18 |
+
return Settings()
|
19 |
+
|
20 |
+
|
21 |
@user_router.post("/user/register")
|
22 |
async def register_user(user: BaseRequest):
|
23 |
data = await User.objects.filter(email=user.email).first()
|
|
|
29 |
return {"code": 200, "message": "success", "payload": None}
|
30 |
|
31 |
|
32 |
+
async def get_token_owner(Authorize: AuthJWT = Depends()):
|
33 |
+
Authorize.jwt_required()
|
34 |
+
current_user = Authorize.get_jwt_subject()
|
35 |
+
user = await User.objects.filter(id=int(current_user)).first()
|
36 |
+
if not user:
|
37 |
+
raise HTTPException(status_code=401, detail="Invalid Credentials")
|
38 |
+
|
39 |
+
return UserSchema.from_orm(user)
|
40 |
+
|
41 |
+
|
42 |
@user_router.post("/user/login")
|
43 |
+
async def register_user(user: BaseRequest, Authorize: AuthJWT = Depends()):
|
44 |
db_user = await User.objects.filter(email=user.email).first()
|
45 |
+
|
46 |
+
if not db_user:
|
47 |
+
raise HTTPException(status_code=401, detail="Invalid Credentials")
|
48 |
+
if not db_user.verify_password(user.password):
|
49 |
+
raise HTTPException(status_code=401, detail="Invalid Credentials")
|
50 |
+
user = UserSchema.from_orm(db_user)
|
51 |
+
access_token = Authorize.create_access_token(subject=user.id)
|
52 |
+
|
53 |
+
return {
|
54 |
+
"code": 200,
|
55 |
+
"message": "success",
|
56 |
+
"payload": db_user.__dict__,
|
57 |
+
"access_token": access_token,
|
58 |
+
}
|
App/__main__.py
CHANGED
@@ -1,28 +1,8 @@
|
|
1 |
-
|
2 |
-
from App.modelInit import models, dbLink
|
3 |
-
import asyncio
|
4 |
-
from sqlalchemy.ext.asyncio import create_async_engine
|
5 |
-
|
6 |
-
|
7 |
-
async def async_main() -> None:
|
8 |
-
engine = create_async_engine(
|
9 |
-
dbLink,
|
10 |
-
echo=True,
|
11 |
-
)
|
12 |
-
|
13 |
-
async with engine.begin() as conn:
|
14 |
-
await conn.run_sync(models.metadata.create_all)
|
15 |
-
|
16 |
|
17 |
import uvicorn
|
18 |
-
|
19 |
-
|
20 |
-
async def create_tables():
|
21 |
-
await models.create_all()
|
22 |
|
23 |
|
24 |
if __name__ == "__main__":
|
25 |
-
loop = asyncio.get_event_loop()
|
26 |
-
loop.run_until_complete(create_tables())
|
27 |
-
loop.close()
|
28 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from App.app import app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
import uvicorn
|
4 |
+
import asyncio
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
if __name__ == "__main__":
|
|
|
|
|
|
|
8 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
App/app.py
CHANGED
@@ -1,13 +1,19 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
2 |
from App import bot
|
3 |
|
|
|
4 |
from .Users.UserRoutes import user_router
|
5 |
from .modelInit import models, database
|
6 |
from .Transcription.TranscriptionRoutes import transcription_router
|
7 |
from .Streaming.StreamingRoutes import streaming_router
|
8 |
from .UserTranscriptions.UserTranscriptionsRoutes import user_transcriptions_router
|
9 |
from .Monitor.monitorRoutes import monitor_router
|
|
|
|
|
10 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
11 |
import logging, orm
|
12 |
|
13 |
|
@@ -28,7 +34,6 @@ async def create_async_model(model):
|
|
28 |
|
29 |
|
30 |
app = FastAPI()
|
31 |
-
models = orm.ModelRegistry(database=database)
|
32 |
origins = ["*"]
|
33 |
|
34 |
app.add_middleware(
|
@@ -40,11 +45,17 @@ app.add_middleware(
|
|
40 |
)
|
41 |
|
42 |
|
|
|
|
|
|
|
|
|
|
|
43 |
@app.on_event("startup")
|
44 |
async def startup_event():
|
45 |
# await bot.start(bot_token="6183919505:AAEhHFt4mI18bQeAf2Lj7AePXFRPVLrOFM8")
|
46 |
# await upload_bot.start()
|
47 |
-
|
|
|
48 |
# await create_async_model(models)
|
49 |
if not database.is_connected:
|
50 |
await database.connect()
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
from App import bot
|
4 |
|
5 |
+
|
6 |
from .Users.UserRoutes import user_router
|
7 |
from .modelInit import models, database
|
8 |
from .Transcription.TranscriptionRoutes import transcription_router
|
9 |
from .Streaming.StreamingRoutes import streaming_router
|
10 |
from .UserTranscriptions.UserTranscriptionsRoutes import user_transcriptions_router
|
11 |
from .Monitor.monitorRoutes import monitor_router
|
12 |
+
|
13 |
+
|
14 |
from fastapi.middleware.cors import CORSMiddleware
|
15 |
+
from fastapi_jwt_auth.exceptions import AuthJWTException
|
16 |
+
|
17 |
import logging, orm
|
18 |
|
19 |
|
|
|
34 |
|
35 |
|
36 |
app = FastAPI()
|
|
|
37 |
origins = ["*"]
|
38 |
|
39 |
app.add_middleware(
|
|
|
45 |
)
|
46 |
|
47 |
|
48 |
+
@app.exception_handler(AuthJWTException)
|
49 |
+
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
|
50 |
+
return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})
|
51 |
+
|
52 |
+
|
53 |
@app.on_event("startup")
|
54 |
async def startup_event():
|
55 |
# await bot.start(bot_token="6183919505:AAEhHFt4mI18bQeAf2Lj7AePXFRPVLrOFM8")
|
56 |
# await upload_bot.start()
|
57 |
+
await models.create_all()
|
58 |
+
|
59 |
# await create_async_model(models)
|
60 |
if not database.is_connected:
|
61 |
await database.connect()
|
App/modelInit.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import databases
|
2 |
import orm, asyncio
|
3 |
-
|
4 |
|
5 |
# deployment
|
6 |
# database = databases.Database(
|
@@ -15,13 +15,3 @@ database = databases.Database(dbLink)
|
|
15 |
|
16 |
|
17 |
models = orm.ModelRegistry(database=database)
|
18 |
-
|
19 |
-
|
20 |
-
class Note(orm.Model):
|
21 |
-
tablename = "notes"
|
22 |
-
registry = models
|
23 |
-
fields = {
|
24 |
-
"id": orm.Integer(primary_key=True),
|
25 |
-
"text": orm.String(max_length=100),
|
26 |
-
"completed": orm.Boolean(default=False),
|
27 |
-
}
|
|
|
1 |
import databases
|
2 |
import orm, asyncio
|
3 |
+
|
4 |
|
5 |
# deployment
|
6 |
# database = databases.Database(
|
|
|
15 |
|
16 |
|
17 |
models = orm.ModelRegistry(database=database)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dockerfile
CHANGED
@@ -17,7 +17,7 @@ USER user
|
|
17 |
COPY --chown=user . /srv
|
18 |
|
19 |
# Command to run the application
|
20 |
-
CMD
|
21 |
|
22 |
# Expose the server port
|
23 |
EXPOSE 7860
|
|
|
17 |
COPY --chown=user . /srv
|
18 |
|
19 |
# Command to run the application
|
20 |
+
CMD uvicorn App.app:app --host 0.0.0.0 --port 7860 & celery -A App.Worker.celery worker -c 8 --loglevel=info
|
21 |
|
22 |
# Expose the server port
|
23 |
EXPOSE 7860
|
requirements.txt
CHANGED
@@ -27,4 +27,6 @@ python-multipart
|
|
27 |
redis==4.5.1
|
28 |
python-multipart
|
29 |
telethon
|
|
|
|
|
30 |
asyncmy
|
|
|
27 |
redis==4.5.1
|
28 |
python-multipart
|
29 |
telethon
|
30 |
+
fastapi-jwt-auth
|
31 |
+
bcrypt
|
32 |
asyncmy
|