Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
| 35 |
Base = declarative_base()
|
| 36 |
|
| 37 |
# --- Database Models ---
|
| 38 |
-
class
|
| 39 |
__tablename__ = "users"
|
| 40 |
id = Column(Integer, primary_key=True, index=True)
|
| 41 |
username = Column(String, unique=True, index=True)
|
|
@@ -44,7 +44,7 @@ class User(Base):
|
|
| 44 |
is_active = Column(Boolean, default=True)
|
| 45 |
is_admin = Column(Boolean, default=False)
|
| 46 |
|
| 47 |
-
class
|
| 48 |
__tablename__ = "feedback"
|
| 49 |
id = Column(Integer, primary_key=True, index=True)
|
| 50 |
username = Column(String)
|
|
@@ -62,7 +62,7 @@ class UserBase(BaseModel):
|
|
| 62 |
class UserCreate(UserBase):
|
| 63 |
pass
|
| 64 |
|
| 65 |
-
class
|
| 66 |
id: int
|
| 67 |
is_active: bool
|
| 68 |
is_admin: bool
|
|
@@ -82,7 +82,7 @@ class FeedbackBase(BaseModel):
|
|
| 82 |
class FeedbackCreate(FeedbackBase):
|
| 83 |
pass
|
| 84 |
|
| 85 |
-
class
|
| 86 |
id: int
|
| 87 |
created_at: datetime.datetime
|
| 88 |
class Config:
|
|
@@ -124,12 +124,12 @@ async def get_current_user(db: Session = Depends(get_db), token: str = Depends(o
|
|
| 124 |
)
|
| 125 |
return user
|
| 126 |
|
| 127 |
-
async def get_current_active_user(current_user:
|
| 128 |
if not current_user.is_active:
|
| 129 |
raise HTTPException(status_code=400, detail="Inactive user")
|
| 130 |
return current_user
|
| 131 |
|
| 132 |
-
async def get_current_admin_user(current_user:
|
| 133 |
if not current_user.is_admin:
|
| 134 |
raise HTTPException(status_code=403, detail="Not an administrator")
|
| 135 |
return current_user
|
|
@@ -137,20 +137,20 @@ async def get_current_admin_user(current_user: User = Depends(get_current_active
|
|
| 137 |
|
| 138 |
# --- CRUD Operations ---
|
| 139 |
def get_user(db: Session, user_id: int):
|
| 140 |
-
return db.query(
|
| 141 |
|
| 142 |
def get_user_by_username(db: Session, username: str):
|
| 143 |
-
return db.query(
|
| 144 |
|
| 145 |
def get_user_by_email(db: Session, email: str):
|
| 146 |
-
return db.query(
|
| 147 |
|
| 148 |
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
| 149 |
-
return db.query(
|
| 150 |
|
| 151 |
def create_user(db: Session, user: UserCreate):
|
| 152 |
hashed_password = pwd_context.hash(user.password)
|
| 153 |
-
db_user =
|
| 154 |
db.add(db_user)
|
| 155 |
db.commit()
|
| 156 |
db.refresh(db_user)
|
|
@@ -177,14 +177,14 @@ def verify_password(plain_password, hashed_password):
|
|
| 177 |
return pwd_context.verify(plain_password, hashed_password)
|
| 178 |
|
| 179 |
def create_feedback(db: Session, feedback: FeedbackCreate):
|
| 180 |
-
db_feedback =
|
| 181 |
db.add(db_feedback)
|
| 182 |
db.commit()
|
| 183 |
db.refresh(db_feedback)
|
| 184 |
return db_feedback
|
| 185 |
|
| 186 |
def get_feedback(db: Session, skip: int = 0, limit: int = 100):
|
| 187 |
-
return db.query(
|
| 188 |
|
| 189 |
|
| 190 |
|
|
@@ -340,7 +340,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
|
|
| 340 |
access_token = user.username
|
| 341 |
return {"access_token": access_token, "token_type": "bearer"}
|
| 342 |
|
| 343 |
-
@app.post("/signup", response_model=
|
| 344 |
async def signup(user: UserCreate = Depends(), db: Session = Depends(get_db)):
|
| 345 |
db_user = get_user_by_username(db, username=user.username)
|
| 346 |
if db_user:
|
|
@@ -352,7 +352,7 @@ async def signup(user: UserCreate = Depends(), db: Session = Depends(get_db)):
|
|
| 352 |
|
| 353 |
# OCR Endpoint
|
| 354 |
@app.post("/process/", response_model=OCRResponse)
|
| 355 |
-
async def process(file: UploadFile = File(...), current_user:
|
| 356 |
if not file.content_type.startswith("image/"):
|
| 357 |
raise HTTPException(status_code=400, detail="File must be an image")
|
| 358 |
|
|
@@ -382,52 +382,52 @@ async def process(file: UploadFile = File(...), current_user: User = Depends(ge
|
|
| 382 |
os.unlink(temp_file.name)
|
| 383 |
|
| 384 |
@app.get("/word-detection/")
|
| 385 |
-
async def get_word_detection(current_user:
|
| 386 |
if 'word_detection' not in session_files or not os.path.exists(session_files['word_detection']):
|
| 387 |
raise HTTPException(status_code=404, detail="Word detection image not found")
|
| 388 |
return FileResponse(session_files['word_detection'])
|
| 389 |
|
| 390 |
@app.get("/prediction/")
|
| 391 |
-
async def get_prediction(current_user:
|
| 392 |
if 'prediction' not in session_files or not os.path.exists(session_files['prediction']):
|
| 393 |
raise HTTPException(status_code=404, detail="Prediction image not found")
|
| 394 |
return FileResponse(session_files['prediction'])
|
| 395 |
|
| 396 |
# Feedback Endpoint
|
| 397 |
-
@app.post("/feedback/", response_model=
|
| 398 |
-
async def create_feedback_route(feedback: FeedbackCreate, current_user:
|
| 399 |
return create_feedback(db=db, feedback=feedback)
|
| 400 |
|
| 401 |
# Admin Endpoints
|
| 402 |
-
@app.get("/admin/users/", response_model=List[
|
| 403 |
-
async def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), current_user:
|
| 404 |
users = get_users(db, skip=skip, limit=limit)
|
| 405 |
return users
|
| 406 |
|
| 407 |
-
@app.get("/admin/users/{user_id}", response_model=
|
| 408 |
-
async def read_user(user_id: int, db: Session = Depends(get_db), current_user:
|
| 409 |
db_user = get_user(db, user_id=user_id)
|
| 410 |
if db_user is None:
|
| 411 |
raise HTTPException(status_code=404, detail="User not found")
|
| 412 |
return db_user
|
| 413 |
|
| 414 |
-
@app.put("/admin/users/{user_id}", response_model=
|
| 415 |
-
async def update_user_route(user_id: int, user: UserUpdate, db: Session = Depends(get_db), current_user:
|
| 416 |
updated_user = update_user(db=db, user_id=user_id, user=user)
|
| 417 |
if updated_user is None:
|
| 418 |
raise HTTPException(status_code=404, detail="User not found")
|
| 419 |
return updated_user
|
| 420 |
|
| 421 |
@app.delete("/admin/users/{user_id}", response_model=dict)
|
| 422 |
-
async def delete_user_route(user_id: int, db: Session = Depends(get_db), current_user:
|
| 423 |
if delete_user(db=db, user_id=user_id):
|
| 424 |
return {"message": "User deleted successfully"}
|
| 425 |
else:
|
| 426 |
raise HTTPException(status_code=404, detail="User not found")
|
| 427 |
|
| 428 |
|
| 429 |
-
@app.get("/admin/feedback/", response_model=List[
|
| 430 |
-
async def read_feedback(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), current_user:
|
| 431 |
feedback = get_feedback(db, skip=skip, limit=limit)
|
| 432 |
return feedback
|
| 433 |
|
|
|
|
| 35 |
Base = declarative_base()
|
| 36 |
|
| 37 |
# --- Database Models ---
|
| 38 |
+
class UserModel(Base):
|
| 39 |
__tablename__ = "users"
|
| 40 |
id = Column(Integer, primary_key=True, index=True)
|
| 41 |
username = Column(String, unique=True, index=True)
|
|
|
|
| 44 |
is_active = Column(Boolean, default=True)
|
| 45 |
is_admin = Column(Boolean, default=False)
|
| 46 |
|
| 47 |
+
class FeedbackModel(Base):
|
| 48 |
__tablename__ = "feedback"
|
| 49 |
id = Column(Integer, primary_key=True, index=True)
|
| 50 |
username = Column(String)
|
|
|
|
| 62 |
class UserCreate(UserBase):
|
| 63 |
pass
|
| 64 |
|
| 65 |
+
class UserResponse(UserBase):
|
| 66 |
id: int
|
| 67 |
is_active: bool
|
| 68 |
is_admin: bool
|
|
|
|
| 82 |
class FeedbackCreate(FeedbackBase):
|
| 83 |
pass
|
| 84 |
|
| 85 |
+
class FeedbackResponse(FeedbackBase):
|
| 86 |
id: int
|
| 87 |
created_at: datetime.datetime
|
| 88 |
class Config:
|
|
|
|
| 124 |
)
|
| 125 |
return user
|
| 126 |
|
| 127 |
+
async def get_current_active_user(current_user: UserResponse = Depends(get_current_user)):
|
| 128 |
if not current_user.is_active:
|
| 129 |
raise HTTPException(status_code=400, detail="Inactive user")
|
| 130 |
return current_user
|
| 131 |
|
| 132 |
+
async def get_current_admin_user(current_user: UserResponse = Depends(get_current_active_user)):
|
| 133 |
if not current_user.is_admin:
|
| 134 |
raise HTTPException(status_code=403, detail="Not an administrator")
|
| 135 |
return current_user
|
|
|
|
| 137 |
|
| 138 |
# --- CRUD Operations ---
|
| 139 |
def get_user(db: Session, user_id: int):
|
| 140 |
+
return db.query(UserModel).filter(UserModel.id == user_id).first()
|
| 141 |
|
| 142 |
def get_user_by_username(db: Session, username: str):
|
| 143 |
+
return db.query(UserModel).filter(UserModel.username == username).first()
|
| 144 |
|
| 145 |
def get_user_by_email(db: Session, email: str):
|
| 146 |
+
return db.query(UserModel).filter(UserModel.email == email).first()
|
| 147 |
|
| 148 |
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
| 149 |
+
return db.query(UserModel).offset(skip).limit(limit).all()
|
| 150 |
|
| 151 |
def create_user(db: Session, user: UserCreate):
|
| 152 |
hashed_password = pwd_context.hash(user.password)
|
| 153 |
+
db_user = UserModel(username=user.username, email=user.email, hashed_password=hashed_password)
|
| 154 |
db.add(db_user)
|
| 155 |
db.commit()
|
| 156 |
db.refresh(db_user)
|
|
|
|
| 177 |
return pwd_context.verify(plain_password, hashed_password)
|
| 178 |
|
| 179 |
def create_feedback(db: Session, feedback: FeedbackCreate):
|
| 180 |
+
db_feedback = FeedbackModel(**feedback.dict())
|
| 181 |
db.add(db_feedback)
|
| 182 |
db.commit()
|
| 183 |
db.refresh(db_feedback)
|
| 184 |
return db_feedback
|
| 185 |
|
| 186 |
def get_feedback(db: Session, skip: int = 0, limit: int = 100):
|
| 187 |
+
return db.query(FeedbackModel).order_by(FeedbackModel.created_at.desc()).offset(skip).limit(limit).all()
|
| 188 |
|
| 189 |
|
| 190 |
|
|
|
|
| 340 |
access_token = user.username
|
| 341 |
return {"access_token": access_token, "token_type": "bearer"}
|
| 342 |
|
| 343 |
+
@app.post("/signup", response_model=UserResponse)
|
| 344 |
async def signup(user: UserCreate = Depends(), db: Session = Depends(get_db)):
|
| 345 |
db_user = get_user_by_username(db, username=user.username)
|
| 346 |
if db_user:
|
|
|
|
| 352 |
|
| 353 |
# OCR Endpoint
|
| 354 |
@app.post("/process/", response_model=OCRResponse)
|
| 355 |
+
async def process(file: UploadFile = File(...), current_user: UserResponse = Depends(get_current_active_user)):
|
| 356 |
if not file.content_type.startswith("image/"):
|
| 357 |
raise HTTPException(status_code=400, detail="File must be an image")
|
| 358 |
|
|
|
|
| 382 |
os.unlink(temp_file.name)
|
| 383 |
|
| 384 |
@app.get("/word-detection/")
|
| 385 |
+
async def get_word_detection(current_user: UserResponse = Depends(get_current_active_user)):
|
| 386 |
if 'word_detection' not in session_files or not os.path.exists(session_files['word_detection']):
|
| 387 |
raise HTTPException(status_code=404, detail="Word detection image not found")
|
| 388 |
return FileResponse(session_files['word_detection'])
|
| 389 |
|
| 390 |
@app.get("/prediction/")
|
| 391 |
+
async def get_prediction(current_user: UserResponse = Depends(get_current_active_user)):
|
| 392 |
if 'prediction' not in session_files or not os.path.exists(session_files['prediction']):
|
| 393 |
raise HTTPException(status_code=404, detail="Prediction image not found")
|
| 394 |
return FileResponse(session_files['prediction'])
|
| 395 |
|
| 396 |
# Feedback Endpoint
|
| 397 |
+
@app.post("/feedback/", response_model=FeedbackResponse)
|
| 398 |
+
async def create_feedback_route(feedback: FeedbackCreate, current_user: UserResponse = Depends(get_current_active_user),db: Session = Depends(get_db)):
|
| 399 |
return create_feedback(db=db, feedback=feedback)
|
| 400 |
|
| 401 |
# Admin Endpoints
|
| 402 |
+
@app.get("/admin/users/", response_model=List[UserResponse])
|
| 403 |
+
async def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), current_user: UserResponse = Depends(get_current_admin_user)):
|
| 404 |
users = get_users(db, skip=skip, limit=limit)
|
| 405 |
return users
|
| 406 |
|
| 407 |
+
@app.get("/admin/users/{user_id}", response_model=UserResponse)
|
| 408 |
+
async def read_user(user_id: int, db: Session = Depends(get_db), current_user: UserResponse = Depends(get_current_admin_user)):
|
| 409 |
db_user = get_user(db, user_id=user_id)
|
| 410 |
if db_user is None:
|
| 411 |
raise HTTPException(status_code=404, detail="User not found")
|
| 412 |
return db_user
|
| 413 |
|
| 414 |
+
@app.put("/admin/users/{user_id}", response_model=UserResponse)
|
| 415 |
+
async def update_user_route(user_id: int, user: UserUpdate, db: Session = Depends(get_db), current_user: UserResponse = Depends(get_current_admin_user)):
|
| 416 |
updated_user = update_user(db=db, user_id=user_id, user=user)
|
| 417 |
if updated_user is None:
|
| 418 |
raise HTTPException(status_code=404, detail="User not found")
|
| 419 |
return updated_user
|
| 420 |
|
| 421 |
@app.delete("/admin/users/{user_id}", response_model=dict)
|
| 422 |
+
async def delete_user_route(user_id: int, db: Session = Depends(get_db), current_user: UserResponse = Depends(get_current_admin_user)):
|
| 423 |
if delete_user(db=db, user_id=user_id):
|
| 424 |
return {"message": "User deleted successfully"}
|
| 425 |
else:
|
| 426 |
raise HTTPException(status_code=404, detail="User not found")
|
| 427 |
|
| 428 |
|
| 429 |
+
@app.get("/admin/feedback/", response_model=List[FeedbackResponse])
|
| 430 |
+
async def read_feedback(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), current_user: UserResponse = Depends(get_current_admin_user)):
|
| 431 |
feedback = get_feedback(db, skip=skip, limit=limit)
|
| 432 |
return feedback
|
| 433 |
|