Spaces:
Sleeping
Sleeping
Upload 18 files
Browse files- app/__init__.py +0 -0
- app/__pycache__/__init__.cpython-312.pyc +0 -0
- app/__pycache__/auth.cpython-312.pyc +0 -0
- app/__pycache__/crud.cpython-312.pyc +0 -0
- app/__pycache__/database.cpython-312.pyc +0 -0
- app/__pycache__/main.cpython-312.pyc +0 -0
- app/__pycache__/models.cpython-312.pyc +0 -0
- app/__pycache__/schemas.cpython-312.pyc +0 -0
- app/auth.py +53 -0
- app/crud.py +62 -0
- app/database.py +18 -0
- app/main.py +114 -0
- app/models.py +21 -0
- app/routers/__pycache__/device.cpython-312.pyc +0 -0
- app/routers/__pycache__/user.cpython-312.pyc +0 -0
- app/routers/device.py +51 -0
- app/routers/user.py +30 -0
- app/schemas.py +34 -0
app/__init__.py
ADDED
File without changes
|
app/__pycache__/__init__.cpython-312.pyc
ADDED
Binary file (140 Bytes). View file
|
|
app/__pycache__/auth.cpython-312.pyc
ADDED
Binary file (2.94 kB). View file
|
|
app/__pycache__/crud.cpython-312.pyc
ADDED
Binary file (4.94 kB). View file
|
|
app/__pycache__/database.cpython-312.pyc
ADDED
Binary file (830 Bytes). View file
|
|
app/__pycache__/main.cpython-312.pyc
ADDED
Binary file (8.21 kB). View file
|
|
app/__pycache__/models.cpython-312.pyc
ADDED
Binary file (1.17 kB). View file
|
|
app/__pycache__/schemas.cpython-312.pyc
ADDED
Binary file (1.9 kB). View file
|
|
app/auth.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_crud/app/auth.py
|
2 |
+
|
3 |
+
from fastapi import Depends, HTTPException, status
|
4 |
+
from jose import JWTError, jwt
|
5 |
+
from sqlalchemy.orm import Session
|
6 |
+
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
7 |
+
from passlib.context import CryptContext
|
8 |
+
from datetime import timedelta, datetime
|
9 |
+
from . import crud, models, schemas, database
|
10 |
+
|
11 |
+
# Generate your secret key using the script above and replace it here
|
12 |
+
SECRET_KEY = "a12f6578e42b1e1e4f48d4d4c8482e92f00a1de8eb5290a4f7a6b42a51220b29"
|
13 |
+
ALGORITHM = "HS256"
|
14 |
+
# Set a very long expiration time, e.g., 365 days
|
15 |
+
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 365
|
16 |
+
|
17 |
+
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
18 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
19 |
+
|
20 |
+
def authenticate_user(db: Session, username: str, password: str):
|
21 |
+
user = crud.get_user_by_username(db, username)
|
22 |
+
if not user:
|
23 |
+
return False
|
24 |
+
if not pwd_context.verify(password, user.hashed_password):
|
25 |
+
return False
|
26 |
+
return user
|
27 |
+
|
28 |
+
def create_access_token(data: dict, expires_delta: timedelta = None):
|
29 |
+
to_encode = data.copy()
|
30 |
+
if expires_delta:
|
31 |
+
to_encode.update({"exp": datetime.utcnow() + expires_delta})
|
32 |
+
else:
|
33 |
+
to_encode.update({"exp": datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)})
|
34 |
+
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
35 |
+
return encoded_jwt
|
36 |
+
|
37 |
+
async def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(database.get_db)):
|
38 |
+
credentials_exception = HTTPException(
|
39 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
40 |
+
detail="Could not validate credentials",
|
41 |
+
headers={"WWW-Authenticate": "Bearer"},
|
42 |
+
)
|
43 |
+
try:
|
44 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
45 |
+
username: str = payload.get("sub")
|
46 |
+
if username is None:
|
47 |
+
raise credentials_exception
|
48 |
+
except JWTError:
|
49 |
+
raise credentials_exception
|
50 |
+
user = crud.get_user_by_username(db, username=username)
|
51 |
+
if user is None:
|
52 |
+
raise credentials_exception
|
53 |
+
return user
|
app/crud.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_crud/app/crud.py
|
2 |
+
from sqlalchemy.orm import Session
|
3 |
+
from . import models, schemas
|
4 |
+
from passlib.context import CryptContext
|
5 |
+
|
6 |
+
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
7 |
+
|
8 |
+
# User CRUD operations
|
9 |
+
def get_user(db: Session, user_id: int):
|
10 |
+
return db.query(models.User).filter(models.User.id == user_id).first()
|
11 |
+
|
12 |
+
def get_user_by_username(db: Session, username: str):
|
13 |
+
return db.query(models.User).filter(models.User.username == username).first()
|
14 |
+
|
15 |
+
def create_user(db: Session, user: schemas.UserCreate):
|
16 |
+
hashed_password = pwd_context.hash(user.password)
|
17 |
+
db_user = models.User(username=user.username, hashed_password=hashed_password)
|
18 |
+
db.add(db_user)
|
19 |
+
db.commit()
|
20 |
+
db.refresh(db_user)
|
21 |
+
return db_user
|
22 |
+
|
23 |
+
# Device CRUD operations
|
24 |
+
def get_device(db: Session, device_id: int):
|
25 |
+
return db.query(models.Device).filter(models.Device.id == device_id).first()
|
26 |
+
|
27 |
+
def get_devices(db: Session, skip: int = 0, limit: int = 10):
|
28 |
+
return db.query(models.Device).offset(skip).limit(limit).all()
|
29 |
+
|
30 |
+
def create_device(db: Session, device: schemas.DeviceCreate):
|
31 |
+
db_device = models.Device(**device.dict())
|
32 |
+
db.add(db_device)
|
33 |
+
db.commit()
|
34 |
+
db.refresh(db_device)
|
35 |
+
return db_device
|
36 |
+
|
37 |
+
def update_device(db: Session, device_id: int, device: schemas.DeviceUpdate):
|
38 |
+
db_device = db.query(models.Device).filter(models.Device.id == device_id).first()
|
39 |
+
if db_device is None:
|
40 |
+
return None
|
41 |
+
for key, value in device.dict().items():
|
42 |
+
setattr(db_device, key, value)
|
43 |
+
db.commit()
|
44 |
+
db.refresh(db_device)
|
45 |
+
return db_device
|
46 |
+
|
47 |
+
def delete_device(db: Session, device_id: int):
|
48 |
+
db_device = db.query(models.Device).filter(models.Device.id == device_id).first()
|
49 |
+
if db_device is None:
|
50 |
+
return None
|
51 |
+
db.delete(db_device)
|
52 |
+
db.commit()
|
53 |
+
return db_device
|
54 |
+
|
55 |
+
def set_device_active(db: Session, device_id: int, active: bool):
|
56 |
+
db_device = db.query(models.Device).filter(models.Device.id == device_id).first()
|
57 |
+
if db_device is None:
|
58 |
+
return None
|
59 |
+
db_device.active = active
|
60 |
+
db.commit()
|
61 |
+
db.refresh(db_device)
|
62 |
+
return db_device
|
app/database.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_crud/app/database.py
|
2 |
+
from sqlalchemy import create_engine
|
3 |
+
from sqlalchemy.ext.declarative import declarative_base
|
4 |
+
from sqlalchemy.orm import sessionmaker
|
5 |
+
|
6 |
+
DATABASE_URL = "sqlite:///./test.db"
|
7 |
+
|
8 |
+
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
9 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
10 |
+
Base = declarative_base()
|
11 |
+
|
12 |
+
# Dependency
|
13 |
+
def get_db():
|
14 |
+
db = SessionLocal()
|
15 |
+
try:
|
16 |
+
yield db
|
17 |
+
finally:
|
18 |
+
db.close()
|
app/main.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_crud/app/main.py
|
2 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Form, Depends
|
3 |
+
from fastapi.responses import JSONResponse
|
4 |
+
from sqlalchemy.orm import Session
|
5 |
+
from pydub import AudioSegment
|
6 |
+
import io
|
7 |
+
import spacy
|
8 |
+
import speech_recognition as sr
|
9 |
+
|
10 |
+
from app.database import engine, Base, get_db
|
11 |
+
from app.routers import user, device
|
12 |
+
from app import crud, schemas, auth, models
|
13 |
+
|
14 |
+
# Create the database tables
|
15 |
+
Base.metadata.create_all(bind=engine)
|
16 |
+
|
17 |
+
app = FastAPI()
|
18 |
+
|
19 |
+
app.include_router(user.router)
|
20 |
+
app.include_router(device.router)
|
21 |
+
|
22 |
+
# Load spaCy models
|
23 |
+
nlp = spacy.load("custom_nlp_model")
|
24 |
+
nlp2 = spacy.load("text_categorizer_model")
|
25 |
+
|
26 |
+
def convert_audio_to_text(audio_file: UploadFile):
|
27 |
+
try:
|
28 |
+
audio_format = audio_file.filename.split(".")[-1]
|
29 |
+
if audio_format not in ["wav", "mp3", "ogg", "flac"]:
|
30 |
+
raise HTTPException(status_code=400, detail="Unsupported audio format. Please upload a wav, mp3, ogg, or flac file.")
|
31 |
+
|
32 |
+
audio = AudioSegment.from_file(io.BytesIO(audio_file.file.read()), format=audio_format)
|
33 |
+
audio = audio.set_channels(1).set_frame_rate(16000)
|
34 |
+
wav_io = io.BytesIO()
|
35 |
+
audio.export(wav_io, format="wav")
|
36 |
+
wav_io.seek(0)
|
37 |
+
|
38 |
+
recognizer = sr.Recognizer()
|
39 |
+
with sr.AudioFile(wav_io) as source:
|
40 |
+
audio_data = recognizer.record(source)
|
41 |
+
|
42 |
+
text = recognizer.recognize_google(audio_data)
|
43 |
+
return text
|
44 |
+
except sr.UnknownValueError:
|
45 |
+
raise HTTPException(status_code=400, detail="Speech recognition could not understand the audio.")
|
46 |
+
except sr.RequestError as e:
|
47 |
+
raise HTTPException(status_code=500, detail=f"Speech recognition service error: {e}")
|
48 |
+
except Exception as e:
|
49 |
+
raise HTTPException(status_code=500, detail=f"Audio processing error: {e}")
|
50 |
+
|
51 |
+
def update_device_status(db: Session, device_name: str, location: str, status: str):
|
52 |
+
db_device = db.query(models.Device).filter(models.Device.name == device_name, models.Device.location == location).first()
|
53 |
+
active_status = True if status.lower() == "on" else False
|
54 |
+
if db_device:
|
55 |
+
crud.set_device_active(db, db_device.id, active_status)
|
56 |
+
return {"device": db_device.name, "location": db_device.location, "status": "turned " + status}
|
57 |
+
return {"device": device_name, "location": location, "status": "not found"}
|
58 |
+
|
59 |
+
def find_location(db: Session, text: str):
|
60 |
+
words = text.split()
|
61 |
+
for word in words:
|
62 |
+
db_location = db.query(models.Device).filter(models.Device.location == word).first()
|
63 |
+
if db_location:
|
64 |
+
return db_location.location
|
65 |
+
return ''
|
66 |
+
|
67 |
+
def process_entities_and_update(db: Session, doc, text, status):
|
68 |
+
updates = []
|
69 |
+
location_entity = next((ent for ent in doc.ents if ent.label_.lower() == 'location'), None)
|
70 |
+
location = location_entity.text if location_entity else find_location(db, text)
|
71 |
+
|
72 |
+
for ent in doc.ents:
|
73 |
+
if ent.label_ == 'device':
|
74 |
+
update = update_device_status(db, ent.text, location, status)
|
75 |
+
updates.append(update)
|
76 |
+
|
77 |
+
if not updates: # No device entities found, process all words as potential device names
|
78 |
+
words = text.split()
|
79 |
+
for word in words:
|
80 |
+
update = update_device_status(db, word, location, status)
|
81 |
+
updates.append(update)
|
82 |
+
|
83 |
+
return updates
|
84 |
+
|
85 |
+
@app.post("/predict/")
|
86 |
+
async def predict(audio_file: UploadFile = File(...), db: Session = Depends(get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
87 |
+
try:
|
88 |
+
text = convert_audio_to_text(audio_file)
|
89 |
+
doc = nlp(text)
|
90 |
+
doc2 = nlp2(text)
|
91 |
+
|
92 |
+
predictions = {"category": max(doc2.cats, key=doc2.cats.get)}
|
93 |
+
entities = [{"text": ent.text, "label": ent.label_} for ent in doc.ents]
|
94 |
+
|
95 |
+
updates = process_entities_and_update(db, doc, text, predictions['category'])
|
96 |
+
|
97 |
+
return JSONResponse(content={"text": text, "predictions": predictions, "entities": entities, "updates": updates})
|
98 |
+
except HTTPException as e:
|
99 |
+
return JSONResponse(status_code=e.status_code, content={"detail": e.detail})
|
100 |
+
|
101 |
+
@app.post("/predict_text/")
|
102 |
+
async def predict_text(text: str = Form(...), db: Session = Depends(get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
103 |
+
try:
|
104 |
+
doc = nlp(text)
|
105 |
+
doc2 = nlp2(text)
|
106 |
+
|
107 |
+
predictions = {"category": max(doc2.cats, key=doc2.cats.get)}
|
108 |
+
entities = [{"text": ent.text, "label": ent.label_} for ent in doc.ents]
|
109 |
+
|
110 |
+
updates = process_entities_and_update(db, doc, text, predictions['category'])
|
111 |
+
|
112 |
+
return JSONResponse(content={"text": text, "predictions": predictions, "entities": entities, "updates": updates})
|
113 |
+
except HTTPException as e:
|
114 |
+
return JSONResponse(status_code=e.status_code, content={"detail": e.detail})
|
app/models.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_crud/app/models.py
|
2 |
+
from sqlalchemy import Column, Integer, String, Boolean
|
3 |
+
from .database import Base
|
4 |
+
|
5 |
+
class User(Base):
|
6 |
+
__tablename__ = "users"
|
7 |
+
|
8 |
+
id = Column(Integer, primary_key=True, index=True)
|
9 |
+
username = Column(String, unique=True, index=True)
|
10 |
+
hashed_password = Column(String)
|
11 |
+
is_active = Column(Boolean, default=True)
|
12 |
+
|
13 |
+
class Device(Base):
|
14 |
+
__tablename__ = "devices"
|
15 |
+
|
16 |
+
id = Column(Integer, primary_key=True, index=True)
|
17 |
+
channel = Column(Integer, unique=True, index=True)
|
18 |
+
name = Column(String, index=True)
|
19 |
+
rating = Column(Integer)
|
20 |
+
location = Column(String)
|
21 |
+
active = Column(Boolean, default=False)
|
app/routers/__pycache__/device.cpython-312.pyc
ADDED
Binary file (4.56 kB). View file
|
|
app/routers/__pycache__/user.cpython-312.pyc
ADDED
Binary file (2.37 kB). View file
|
|
app/routers/device.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_crud/app/routers/device.py
|
2 |
+
from fastapi import APIRouter, Depends, HTTPException
|
3 |
+
from sqlalchemy.orm import Session
|
4 |
+
from typing import List
|
5 |
+
from app import crud, schemas, database, auth
|
6 |
+
|
7 |
+
router = APIRouter()
|
8 |
+
|
9 |
+
@router.post("/devices/", response_model=schemas.Device)
|
10 |
+
def create_device(device: schemas.DeviceCreate, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
11 |
+
return crud.create_device(db=db, device=device)
|
12 |
+
|
13 |
+
@router.get("/devices/", response_model=List[schemas.Device])
|
14 |
+
def read_devices(skip: int = 0, limit: int = 10, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
15 |
+
devices = crud.get_devices(db, skip=skip, limit=limit)
|
16 |
+
return devices
|
17 |
+
|
18 |
+
@router.get("/devices/{device_id}", response_model=schemas.Device)
|
19 |
+
def read_device(device_id: int, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
20 |
+
db_device = crud.get_device(db, device_id=device_id)
|
21 |
+
if db_device is None:
|
22 |
+
raise HTTPException(status_code=404, detail="Device not found")
|
23 |
+
return db_device
|
24 |
+
|
25 |
+
@router.post("/devices/{device_id}/update", response_model=schemas.Device)
|
26 |
+
def update_device(device_id: int, device: schemas.DeviceUpdate, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
27 |
+
db_device = crud.update_device(db, device_id=device_id, device=device)
|
28 |
+
if db_device is None:
|
29 |
+
raise HTTPException(status_code=404, detail="Device not found")
|
30 |
+
return db_device
|
31 |
+
|
32 |
+
@router.delete("/devices/{device_id}", response_model=schemas.Device)
|
33 |
+
def delete_device(device_id: int, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
34 |
+
db_device = crud.delete_device(db, device_id=device_id)
|
35 |
+
if db_device is None:
|
36 |
+
raise HTTPException(status_code=404, detail="Device not found")
|
37 |
+
return db_device
|
38 |
+
|
39 |
+
@router.post("/devices/{device_id}/on", response_model=schemas.Device)
|
40 |
+
def set_device_on(device_id: int, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
41 |
+
db_device = crud.set_device_active(db, device_id=device_id, active=True)
|
42 |
+
if db_device is None:
|
43 |
+
raise HTTPException(status_code=404, detail="Device not found")
|
44 |
+
return db_device
|
45 |
+
|
46 |
+
@router.post("/devices/{device_id}/off", response_model=schemas.Device)
|
47 |
+
def set_device_off(device_id: int, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)):
|
48 |
+
db_device = crud.set_device_active(db, device_id=device_id, active=False)
|
49 |
+
if db_device is None:
|
50 |
+
raise HTTPException(status_code=404, detail="Device not found")
|
51 |
+
return db_device
|
app/routers/user.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_crud/app/routers/user.py
|
2 |
+
from fastapi import APIRouter, Depends, HTTPException
|
3 |
+
from sqlalchemy.orm import Session
|
4 |
+
from fastapi.security import OAuth2PasswordRequestForm
|
5 |
+
from app import crud, schemas, database, auth
|
6 |
+
router = APIRouter()
|
7 |
+
|
8 |
+
@router.post("/users/", response_model=schemas.User)
|
9 |
+
def create_user(user: schemas.UserCreate, db: Session = Depends(database.get_db)):
|
10 |
+
db_user = crud.get_user_by_username(db, username=user.username)
|
11 |
+
if db_user:
|
12 |
+
raise HTTPException(status_code=400, detail="Username already registered")
|
13 |
+
return crud.create_user(db=db, user=user)
|
14 |
+
|
15 |
+
# status_code=status.HTTP_401_UNAUTHORIZED
|
16 |
+
@router.post("/token", response_model=dict)
|
17 |
+
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(database.get_db)):
|
18 |
+
user = auth.authenticate_user(db, form_data.username, form_data.password)
|
19 |
+
if not user:
|
20 |
+
raise HTTPException(
|
21 |
+
status_code=401,
|
22 |
+
detail="Incorrect username or password",
|
23 |
+
headers={"WWW-Authenticate": "Bearer"},
|
24 |
+
)
|
25 |
+
access_token = auth.create_access_token(data={"sub": user.username})
|
26 |
+
return {"access_token": access_token, "token_type": "bearer"}
|
27 |
+
|
28 |
+
@router.get("/users/me/", response_model=schemas.User)
|
29 |
+
async def read_users_me(current_user: schemas.User = Depends(auth.get_current_user)):
|
30 |
+
return current_user
|
app/schemas.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi_crud/app/schemas.py
|
2 |
+
from pydantic import BaseModel
|
3 |
+
|
4 |
+
class UserBase(BaseModel):
|
5 |
+
username: str
|
6 |
+
|
7 |
+
class UserCreate(UserBase):
|
8 |
+
password: str
|
9 |
+
|
10 |
+
class User(UserBase):
|
11 |
+
id: int
|
12 |
+
is_active: bool
|
13 |
+
|
14 |
+
class Config:
|
15 |
+
orm_mode = True
|
16 |
+
|
17 |
+
class DeviceBase(BaseModel):
|
18 |
+
channel: int
|
19 |
+
name: str
|
20 |
+
rating: int
|
21 |
+
location: str
|
22 |
+
active: bool = False
|
23 |
+
|
24 |
+
class DeviceCreate(DeviceBase):
|
25 |
+
pass
|
26 |
+
|
27 |
+
class DeviceUpdate(DeviceBase):
|
28 |
+
pass
|
29 |
+
|
30 |
+
class Device(DeviceBase):
|
31 |
+
id: int
|
32 |
+
|
33 |
+
class Config:
|
34 |
+
orm_mode = True
|