Spaces:
Running
Running
File size: 23,505 Bytes
b82f276 5ed4dc4 b82f276 5ed4dc4 ebce1fe b82f276 ebce1fe b82f276 d92933f b82f276 d92933f b82f276 d92933f b82f276 063acf6 b82f276 063acf6 b82f276 bccefa7 063acf6 bccefa7 b82f276 d92933f b82f276 3edee92 d92933f 3edee92 d92933f b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 934c6ee b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 d5508ff b82f276 d5508ff b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 934c6ee b82f276 934c6ee b82f276 5ed4dc4 b82f276 5ed4dc4 b82f276 5ed4dc4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | import bcrypt
# Robust monkeypatch for bcrypt-passlib compatibility
if not hasattr(bcrypt, "__about__"):
bcrypt.__about__ = type('About', (object,), {'__version__': bcrypt.__version__})
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request, Response, Depends, HTTPException, status
from typing import Optional
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import uvicorn
import asyncio
import os
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from pydantic import BaseModel, EmailStr
from utils.main import RAG
from database import engine, Base, get_db
from models import User
from auth import get_password_hash, verify_password, create_access_token, ACCESS_TOKEN_EXPIRE_MINUTES, ALGORITHM, SECRET_KEY
from email_utils import send_reset_email
from datetime import timedelta
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
import uuid
from database import engine, Base, get_db, AsyncSessionLocal
# Initialize Database and Create Default Admin
async def init_db():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
# Ensure Admin exists
async with AsyncSessionLocal() as db:
result = await db.execute(select(User).where(User.username == "admin"))
admin_user = result.scalars().first()
if not admin_user:
print("Creating default admin user...")
new_admin = User(
username="admin",
email="admin@lawbot.ai",
hashed_password=get_password_hash("ADMin"),
role="Admin"
)
db.add(new_admin)
await db.commit()
print("Admin user created successfully.")
app = FastAPI(on_startup=[init_db])
chat_history = []
# Get the base directory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_DIR = os.path.join(BASE_DIR, "static")
TEMPLATES_DIR = os.path.join(BASE_DIR, "templates")
# Mount static directories
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
app.mount("/images", StaticFiles(directory=os.path.join(STATIC_DIR, "images")), name="images")
# Initialize Jinja2 templates
templates = Jinja2Templates(directory=TEMPLATES_DIR)
# Pydantic Models for Auth
class UserCreate(BaseModel):
username: str
email: EmailStr
password: str
role: str
mobile_number: str # Exactly 10 digits required
class UserLogin(BaseModel):
username: str
password: str
role: Optional[str] = None
class ForgotPassword(BaseModel):
email: EmailStr
class ResetPassword(BaseModel):
token: str
new_password: str
class Interaction(BaseModel):
caseId: str
query: str
response: str
role: str # Role context for this interaction
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/login")
async def get_current_user(request: Request, db: AsyncSession = Depends(get_db)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
# Try to get token from Header first (for API calls), then Cookie (for Page navigation)
token = None
auth_header = request.headers.get("Authorization")
if auth_header and auth_header.startswith("Bearer "):
token = auth_header.split(" ")[1]
else:
token = request.cookies.get("access_token")
if not token:
raise credentials_exception
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
if username is None:
raise credentials_exception
except JWTError:
raise credentials_exception
result = await db.execute(select(User).where(User.username == username))
user = result.scalars().first()
if user is None:
raise credentials_exception
return user
def role_required(required_role: str):
async def role_checker(user: User = Depends(get_current_user)):
if user.role != required_role and user.role != "Admin":
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=f"Access denied: Requires {required_role} role (or Admin)"
)
return user
return role_checker
# Auth Routes
@app.post("/api/register")
async def register(user: UserCreate, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(User).where((User.username == user.username) | (User.email == user.email)))
if result.scalars().first():
raise HTTPException(status_code=400, detail="Username or Email already registered")
# Validation: Exactly 10 digits
if not user.mobile_number.isdigit() or len(user.mobile_number) != 10:
raise HTTPException(status_code=400, detail="Mobile number must be exactly 10 digits.")
hashed_password = get_password_hash(user.password)
new_user = User(
username=user.username,
email=user.email,
hashed_password=hashed_password,
role=user.role,
mobile_number=user.mobile_number
)
db.add(new_user)
await db.commit()
return {"message": "User created successfully"}
@app.post("/api/login")
async def login(response: Response, user: UserLogin, db: AsyncSession = Depends(get_db)):
print(f"Login attempt for username: {user.username}")
result = await db.execute(select(User).where(User.username == user.username))
db_user = result.scalars().first()
if not db_user:
print(f"User not found: {user.username}")
raise HTTPException(status_code=400, detail="Incorrect username or password")
print(f"User found: {db_user.username}, checking password...")
password_valid = verify_password(user.password, db_user.hashed_password)
print(f"Password valid: {password_valid}")
if not password_valid:
raise HTTPException(status_code=400, detail="Incorrect username or password")
# Verify role if provided
if user.role and db_user.role != user.role and db_user.role != "Admin":
print(f"Role mismatch: user has {db_user.role}, but tried to login as {user.role}")
raise HTTPException(status_code=403, detail=f"Access denied: This account is registered as {db_user.role}, not {user.role}")
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(data={"sub": db_user.username, "role": db_user.role}, expires_delta=access_token_expires)
# Set secure cookie - use samesite="none" for Hugging Face iframes
response.set_cookie(
key="access_token",
value=access_token,
httponly=True,
max_age=ACCESS_TOKEN_EXPIRE_MINUTES * 60,
samesite="none",
secure=True # Required for Hugging Face HTTPS and samesite="none"
)
print(f"Login successful for {db_user.username}, role: {db_user.role}")
return {
"access_token": access_token,
"token_type": "bearer",
"role": db_user.role,
"question_count": db_user.question_count,
"is_admin": db_user.role == "Admin"
}
@app.post("/api/logout")
async def logout(response: Response):
response.delete_cookie(
key="access_token",
samesite="none",
secure=True
)
return {"message": "Logged out successfully"}
@app.post("/api/forgot-password")
async def forgot_password(request: ForgotPassword, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(User).where(User.email == request.email))
user = result.scalars().first()
if user:
token = str(uuid.uuid4())
user.reset_token = token
await db.commit()
try:
await send_reset_email(user.email, token)
except Exception as e:
print(f"Error sending email: {e}")
raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}")
return {"message": "If an account exists, a reset email has been sent"}
@app.post("/api/reset-password")
async def reset_password(request: ResetPassword, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(User).where(User.reset_token == request.token))
user = result.scalars().first()
if not user:
raise HTTPException(status_code=400, detail="Invalid or expired reset token")
user.hashed_password = get_password_hash(request.new_password)
user.reset_token = None
await db.commit()
return {"message": "Password reset successful"}
@app.post("/api/save-interaction")
async def save_interaction(interaction: Interaction, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user)):
from models import ChatInteraction
# Create new chat interaction record linked to user with role
new_interaction = ChatInteraction(
case_id=interaction.caseId,
query=interaction.query,
response=interaction.response,
role=interaction.role,
user_id=current_user.id
)
db.add(new_interaction)
await db.commit()
await db.refresh(new_interaction)
print(f"Saved interaction for user {current_user.username}: ID={new_interaction.id}, CaseID={interaction.caseId}")
return {"status": "success", "message": "Interaction saved", "id": new_interaction.id}
@app.get("/api/interactions")
async def get_interactions(role: Optional[str] = None, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user)):
from models import ChatInteraction
from sqlalchemy import func
# Build base where clause
where_clause = ChatInteraction.user_id == current_user.id
if role:
where_clause = (ChatInteraction.user_id == current_user.id) & (ChatInteraction.role == role)
# Subquery to find the latest interaction per case_id (filtered by role if specified)
subquery = (
select(
ChatInteraction.case_id,
func.max(ChatInteraction.created_at).label("max_created")
)
.where(where_clause)
.group_by(ChatInteraction.case_id)
.subquery()
)
# Join to get query detail for the latest message in each case
result = await db.execute(
select(ChatInteraction)
.join(subquery, (ChatInteraction.case_id == subquery.c.case_id) & (ChatInteraction.created_at == subquery.c.max_created))
.order_by(ChatInteraction.created_at.desc())
.limit(20)
)
interactions = result.scalars().all()
return [{"id": i.id, "case_id": i.case_id, "query": i.query, "created_at": i.created_at} for i in interactions]
@app.get("/api/interactions/{case_id}")
async def get_conversation_thread(case_id: str, role: Optional[str] = None, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user)):
from models import ChatInteraction
# Build where clause with optional role filtering
where_clause = (ChatInteraction.user_id == current_user.id) & (ChatInteraction.case_id == case_id)
if role:
where_clause = where_clause & (ChatInteraction.role == role)
result = await db.execute(
select(ChatInteraction)
.where(where_clause)
.order_by(ChatInteraction.created_at.asc())
)
interactions = result.scalars().all()
if not interactions:
raise HTTPException(status_code=404, detail="Conversation not found")
return [{"query": i.query, "response": i.response, "created_at": i.created_at} for i in interactions]
@app.delete("/api/interactions/{case_id}")
async def delete_conversation(case_id: str, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user)):
from models import ChatInteraction
from sqlalchemy import delete as sqlalchemy_delete
await db.execute(
sqlalchemy_delete(ChatInteraction)
.where((ChatInteraction.user_id == current_user.id) & (ChatInteraction.case_id == case_id))
)
await db.commit()
return {"status": "success", "message": "Conversation deleted"}
@app.get("/api/user-status")
async def get_user_status(current_user: User = Depends(get_current_user)):
return {
"username": current_user.username,
"role": current_user.role,
"question_count": current_user.question_count,
"limit": 2 if current_user.role != "Admin" else None,
"is_admin": current_user.role == "Admin"
}
@app.get("/api/admin/users")
async def get_all_users(db: AsyncSession = Depends(get_db), current_user: User = Depends(role_required("Admin"))):
result = await db.execute(select(User))
users = result.scalars().all()
return [
{
"id": u.id,
"username": u.username,
"email": u.email,
"role": u.role,
"mobile_number": u.mobile_number,
"question_count": u.question_count,
"is_blocked": False # Placeholder if needed
} for u in users
]
# Frontend Routes for Auth
@app.get("/login", response_class=HTMLResponse)
async def login_page(request: Request):
return templates.TemplateResponse("login.html", {"request": request})
@app.get("/register", response_class=HTMLResponse)
async def register_page(request: Request):
return templates.TemplateResponse("register.html", {"request": request})
@app.get("/forgot-password", response_class=HTMLResponse)
async def forgot_password_page(request: Request):
return templates.TemplateResponse("forgot_password.html", {"request": request})
@app.get("/reset-password", response_class=HTMLResponse)
async def reset_password_page(request: Request):
return templates.TemplateResponse("reset_password.html", {"request": request})
@app.get("/users", response_class=HTMLResponse)
@app.get("/admin/users", response_class=HTMLResponse)
async def admin_users_page(request: Request):
# Removing API-level Depends() here so the HTML page loads and handles auth via JS fetch()
return templates.TemplateResponse("admin_users.html", {"request": request})
# Home and Role Selection
@app.get("/", response_class=HTMLResponse)
async def role_selection(request: Request):
return templates.TemplateResponse("roleselection.html", {"request": request, "show_roles": False})
@app.get("/role", response_class=HTMLResponse)
async def roleselection_page(request: Request):
return templates.TemplateResponse("roleselection.html", {"request": request, "show_roles": True})
# Chatbot Pages
@app.get("/judgechatbot.html", response_class=HTMLResponse)
async def judge_chatbot(request: Request, user: User = Depends(role_required("Judge"))):
return templates.TemplateResponse("Judgechatbot.html", {"request": request})
@app.get("/judgedashboard.html", response_class=HTMLResponse)
async def judge_dashboard(request: Request, user: User = Depends(role_required("Judge"))):
return templates.TemplateResponse("judgedashboard.html", {"request": request})
@app.get("/viewall.html", response_class=HTMLResponse)
async def view_all(request: Request, user: User = Depends(role_required("Judge"))):
return templates.TemplateResponse("viewall.html", {"request": request})
@app.get("/judgecalender.html", response_class=HTMLResponse)
async def judge_calender(request: Request, user: User = Depends(role_required("Judge"))):
return templates.TemplateResponse("judgecalender.html", {"request": request})
@app.get("/advocatedashboard.html", response_class=HTMLResponse)
async def advocate_dashboard(request: Request, user: User = Depends(role_required("Advocate/Lawyer"))):
return templates.TemplateResponse("advocatedashboard.html", {"request": request})
@app.get("/advocateresources.html", response_class=HTMLResponse)
async def advocate_resources(request: Request, user: User = Depends(role_required("Advocate/Lawyer"))):
return templates.TemplateResponse("advocateresources.html", {"request": request})
# ========== Other Role Pages ==========
@app.get("/woman.html", response_class=HTMLResponse)
async def woman_page(request: Request, user: User = Depends(role_required("Woman"))):
return templates.TemplateResponse("woman.html", {"request": request})
@app.get("/citizen.html", response_class=HTMLResponse)
async def citizen_page(request: Request, user: User = Depends(role_required("Citizen"))):
return templates.TemplateResponse("citizen.html", {"request": request})
@app.get("/minor.html", response_class=HTMLResponse)
async def minor_page(request: Request, user: User = Depends(role_required("Minor"))):
return templates.TemplateResponse("minor.html", {"request": request})
# ========== Chatbot Pages ==========
@app.get("/studentchatbot.html", response_class=HTMLResponse)
async def student_page(request: Request, user: User = Depends(role_required("Student"))):
return templates.TemplateResponse("studentchatbot.html", {"request": request})
@app.get("/advocatechatbot.html", response_class=HTMLResponse)
async def advocatechatbot_page(request: Request):
return templates.TemplateResponse("advocatechatbot.html", {"request": request})
@app.get("/womanchatbot.html", response_class=HTMLResponse)
async def womanchatbot_page(request: Request):
return templates.TemplateResponse("womanchatbot.html", {"request": request})
@app.get("/safetytips.html", response_class=HTMLResponse)
async def safetytips_page(request: Request):
return templates.TemplateResponse("safetytips.html", {"request": request})
@app.get("/resources.html", response_class=HTMLResponse)
async def resources_page(request: Request):
return templates.TemplateResponse("resources.html", {"request": request})
@app.get("/legalrights.html", response_class=HTMLResponse)
async def legalrights_page(request: Request):
return templates.TemplateResponse("legalrights.html", {"request": request})
@app.get("/FIR.html", response_class=HTMLResponse)
async def fir_page(request: Request):
return templates.TemplateResponse("FIR.html", {"request": request})
@app.get("/studentdashboard.html", response_class=HTMLResponse)
async def student_page(request: Request):
return templates.TemplateResponse("studentdashboard.html", {"request": request})
# WebSocket for Chatbot
async def stream_text_conversational(websocket: WebSocket, query: str, role: str = "General"):
chat_limit = 10
temp_chat = {"user": "" ,"system":""}
temp_chat["user"] = query
# print(f"DEBUG: stream_text_conversational executing with role={role}")
model_response = ""
try:
if role == "Citizen":
completion = RAG(query, chat_history, role=role)
else:
completion = RAG(query, chat_history, role=role)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
await websocket.send_text(chunk.choices[0].delta.content)
await asyncio.sleep(0.01)
model_response += chunk.choices[0].delta.content
# Signal completion to frontend
await websocket.send_text("[DONE]")
# print(model_response)
temp_chat['system']=model_response
chat_history.append(temp_chat)
if len(chat_history)>chat_limit:
chat_history.pop(0)
except Exception as e:
error_str = str(e)
print(f"Chatbot error: {error_str}")
# Show a professional message — never dump raw API errors to users
if "404" in error_str or "No endpoints" in error_str:
user_msg = "\n\n⚠️ **Service Temporarily Unavailable**\nThe AI provider is currently unavailable. Please send your message again — the system will automatically switch to an alternative model.\n\nIf the issue persists, please try again in a moment."
else:
user_msg = f"\n\n⚠️ **An error occurred.** Please try sending your message again.\n\n_If the issue persists, please contact support._"
await websocket.send_text(user_msg)
await websocket.send_text("[DONE]")
@app.websocket("/conversational_chat")
async def conversational_chat(websocket: WebSocket, role: Optional[str] = None):
await websocket.accept()
# --- STRICT ROLE ENFORCEMENT ---
# 1. Get role from Token/Cookie (Source of Truth)
token = websocket.cookies.get("access_token")
token_role = "General"
user_id = None
question_count = 0
if token:
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username = payload.get("sub")
if username:
async for db in get_db():
result = await db.execute(select(User).where(User.username == username))
user_obj = result.scalars().first()
if user_obj:
token_role = user_obj.role
user_id = user_obj.id
question_count = user_obj.question_count
break
except JWTError:
pass
if user_id is None:
await websocket.send_text("\n\n⚠️ Authentication Required. Please sign in to use the chatbot.")
await websocket.close()
return
# 2. Apply Logic based on Source of Truth
if token_role == "Admin":
if not role:
role = "Admin"
else:
role = token_role
while True:
try:
query = await websocket.receive_text()
# --- USAGE LIMIT CHECK ---
if token_role != "Admin" and question_count >= 2:
limit_message = (
"### ✨ Free usage limit reached\n\n"
"You've reached the free usage limit (2 questions).\n"
"Further access is restricted.\n\n"
"Please contact the administrator for extended access:\n"
"LinkedIn: https://www.linkedin.com/in/vishwanath77"
)
await websocket.send_text(limit_message)
continue
print(f"Query ({role}): {query}")
await stream_text_conversational(websocket, query, role=role)
# --- INCREMENT USAGE ---
if token_role != "Admin":
async for db in get_db():
result = await db.execute(select(User).where(User.id == user_id))
user_to_update = result.scalars().first()
if user_to_update:
user_to_update.question_count += 1
question_count = user_to_update.question_count # Keep local sync
await db.commit()
break
except WebSocketDisconnect:
chat_history.clear()
break
except Exception as e:
print(f"WebSocket error: {e}")
try:
await websocket.send_text(
"\n\n⚠️ **Connection interrupted.** Please try sending your message again."
)
await websocket.send_text("[DONE]")
except:
break
# Run the application
if __name__ == "__main__":
print("Starting Law Bot Server...")
port = int(os.getenv("PORT", 8000))
uvicorn.run(app, host="0.0.0.0", port=port)
|