humres-api / main.py
vaishnav019's picture
Pin huggingface_hub<0.25.0 to resolve Gradio HfFolder ImportError
162c9bb
Raw History Blame Contribute Delete
35.7 kB
from dotenv import load_dotenv
load_dotenv()
from contextlib import asynccontextmanager
import random
from fastapi import FastAPI, Depends, HTTPException, BackgroundTasks, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from database import supabase
from cache import (
cache_get, cache_set, cache_delete, cache_delete_pattern,
invalidate_user_cache, invalidate_jobs_cache,
rate_limit_check, token_bucket_consume, token_bucket_remaining, hash_text,
)
from auth import create_token, get_current_user, verify_password, hash_password
from models import (
RegisterUser, SendOtpInput, VerifyOtpInput, ResendOtpInput,
LoginUser, ForgotPasswordSendOtpInput, ForgotPasswordResetInput,
JobsInput, SavedJob, AlertPreference, SearchJob, SourceInput, ResumeChatInput
)
from alerts import send_otp_email, send_password_reset_otp_email, test_smtp_diagnostic, get_email_status_diagnostic, get_last_email_error
import shutil
import json
import tempfile
import os
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
from scrapers.main import refresh_and_import_jobs
from RAG import (
extract_text, get_embedding, save_resume, get_resume,
match_jobs, generate_answer, analyze_resume_data,
extract_ai_profile, save_ai_profile, get_ai_profile,
extract_skills_local, chunk_resume_text, get_embeddings_batch,
save_resume_chunks,
)
import time
from collections import defaultdict
from datetime import date
# ─── Rate Limiting (Redis-backed with in-memory fallback) ─────────────
class SlidingWindowRateLimiter:
"""In-memory fallback rate limiter (used only when Redis is unavailable)."""
def __init__(self, requests_per_minute: int = 10):
self.rpm = requests_per_minute
self.requests = defaultdict(list)
def check_rate_limit(self, identifier: str):
now = time.time()
window_start = now - 60
timestamps = [t for t in self.requests[identifier] if t > window_start]
if len(timestamps) >= self.rpm:
raise HTTPException(
status_code=429,
detail=f"Rate limit exceeded (max {self.rpm} queries/min). Please wait a moment!"
)
timestamps.append(now)
self.requests[identifier] = timestamps
minute_limiter = SlidingWindowRateLimiter(requests_per_minute=10)
MAX_TOKENS_PER_WINDOW = 5000
WINDOW_MINUTES = 30
WINDOW_SECONDS = WINDOW_MINUTES * 60
class SlidingWindowTokenLimiter:
"""In-memory fallback token limiter (used only when Redis is unavailable)."""
def __init__(self, max_tokens: int = 5000, window_seconds: int = 1800):
self.max_tokens = max_tokens
self.window_seconds = window_seconds
self.history = defaultdict(list)
def _clean_old(self, user_id: int):
now = time.time()
cutoff = now - self.window_seconds
self.history[user_id] = [entry for entry in self.history[user_id] if entry[0] > cutoff]
def get_remaining_tokens(self, user_id: int) -> int:
self._clean_old(user_id)
used = sum(tokens for t, tokens in self.history[user_id])
return max(0, self.max_tokens - used)
def check_and_consume(self, user_id: int, tokens_to_consume: int = 450) -> int:
self._clean_old(user_id)
used = sum(tokens for t, tokens in self.history[user_id])
if used + tokens_to_consume > self.max_tokens:
remaining = max(0, self.max_tokens - used)
raise HTTPException(
status_code=429,
detail=f"Token rate limit exceeded (5,000 tokens / 1 hour). You have {remaining} tokens remaining in this 1-hour window. Please wait a moment!"
)
self.history[user_id].append((time.time(), tokens_to_consume))
return max(0, self.max_tokens - (used + tokens_to_consume))
_fallback_token_limiter = SlidingWindowTokenLimiter(max_tokens=MAX_TOKENS_PER_WINDOW, window_seconds=WINDOW_SECONDS)
def _get_remaining_tokens(user_id: int) -> int:
"""Get remaining tokens β€” Redis first, in-memory fallback."""
try:
r = _get_redis()
if r is not None:
rem = token_bucket_remaining(f"user:{user_id}", MAX_TOKENS_PER_WINDOW, WINDOW_SECONDS)
if rem < MAX_TOKENS_PER_WINDOW:
return rem
except Exception as e:
print(f"[TokenBucket] Redis remaining check error: {e}")
return _fallback_token_limiter.get_remaining_tokens(user_id)
def _consume_tokens(user_id: int, tokens: int = 450) -> int:
"""Consume tokens β€” Redis first, in-memory fallback."""
mem_rem = MAX_TOKENS_PER_WINDOW
try:
mem_rem = _fallback_token_limiter.check_and_consume(user_id, tokens)
except HTTPException:
pass
try:
r = _get_redis()
if r is not None:
allowed, remaining = token_bucket_consume(f"user:{user_id}", tokens, MAX_TOKENS_PER_WINDOW, WINDOW_SECONDS)
if not allowed:
raise HTTPException(
status_code=429,
detail=f"Token rate limit exceeded (5,000 tokens / 30 mins). You have {remaining} tokens remaining in this 30-minute window. Please wait a moment!"
)
return remaining
except HTTPException:
raise
except Exception as e:
print(f"[TokenBucket] Redis consume error: {e}")
return mem_rem
scheduler = BackgroundScheduler()
@asynccontextmanager
async def lifespan(app: FastAPI):
try:
print("[APScheduler] Initializing 3-day background job refresh...")
scheduler.add_job(
refresh_and_import_jobs,
trigger=IntervalTrigger(days=3),
args=[50],
id='auto_3day_job_refresh',
name='3-Day Job Refresh (Adzuna)',
replace_existing=True,
)
scheduler.start()
print("[APScheduler] Background scheduler active. Jobs will auto-refresh every 3 days.")
except Exception as e:
print(f"[APScheduler] Warning starting background scheduler: {e}")
yield
try:
print("[APScheduler] Shutting down background scheduler...")
scheduler.shutdown()
except Exception as e:
print(f"[APScheduler] Warning shutting down scheduler: {e}")
app = FastAPI(lifespan=lifespan)
cors_origins = [
"https://job-aggregator-umber.vercel.app",
"http://localhost:5173",
"http://localhost:3000",
"http://127.0.0.1:5173",
"http://127.0.0.1:3000",
]
extra_origins = os.getenv("CORS_ORIGINS")
if extra_origins:
cors_origins.extend([o.strip() for o in extra_origins.split(",") if o.strip()])
app.add_middleware(
CORSMiddleware,
allow_origins=cors_origins,
allow_origin_regex=r"https://.*\.vercel\.app",
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
def health_check():
return {"status": "ok"}
@app.post("/jobs/refresh")
def manual_jobs_refresh(background_tasks: BackgroundTasks, user_id: int = Depends(get_current_user)):
background_tasks.add_task(refresh_and_import_jobs, 50)
return {"message": "Job refresh initiated in background. Outdated jobs will be deleted and 50 fresh ones loaded."}
@app.api_route("/", methods=["GET", "HEAD"])
def home():
return {
"message": "Welcome to the Job Aggregator service",
"status": "online"
}
@app.get("/debug/test-smtp")
def debug_test_smtp(to: str = "parasff0007@gmail.com"):
return test_smtp_diagnostic(to)
@app.get("/debug/events")
def debug_events(email: str = "vaishnavshinde186@gmail.com", limit: int = 10):
"""Check Brevo delivery events for an email address without sending anything."""
from alerts import _brevo_api_get
return {
"email": email,
"events": _brevo_api_get(f"smtp/statistics/events?limit={limit}&email={email}")
}
@app.get("/debug/email-status")
def debug_email_status(to: str = None):
"""Check active email sending method, environment variables, and optionally send a test OTP email."""
return get_email_status_diagnostic(to)
@app.get("/debug/send-otp-test")
def debug_send_otp_test(to: str = "vaishnavshinde186@gmail.com"):
"""Send a test OTP email using the exact same priority logic as real registration/forgot-password."""
otp_code = f"{random.randint(100000, 999999)}"
success = send_password_reset_otp_email(to, otp_code)
return {"otp_code": otp_code, "to": to, "send_success": success}
# ─── OTP Helper Storage Functions ────────────────────────────────────
_memory_otps = {}
def _store_otp(email: str, otp: str, username: str, password_hash: str):
email_clean = email.strip().lower()
data = {"otp": str(otp), "username": username, "password_hash": password_hash}
cache_set(f"otp:{email_clean}", data, ttl_seconds=600)
_memory_otps[email_clean] = (str(otp), data, time.time() + 600)
def _verify_otp_data(email: str, otp: str) -> dict:
email_clean = email.strip().lower()
data = cache_get(f"otp:{email_clean}")
if not data and email_clean in _memory_otps:
stored_otp, stored_data, expires_at = _memory_otps[email_clean]
if time.time() < expires_at:
data = stored_data
if not data:
return None
if str(data.get("otp")).strip() == str(otp).strip():
cache_delete(f"otp:{email_clean}")
_memory_otps.pop(email_clean, None)
return data
return None
@app.post("/register/send-otp")
@app.post("/register")
def send_registration_otp(user_data: SendOtpInput):
email_clean = user_data.email.strip().lower()
# Check if user email is already registered
try:
existing_user = (supabase.table("users").select("id").eq("email", email_clean).execute())
if existing_user.data:
raise HTTPException(
status_code=400,
detail="Email is already registered. Please login instead."
)
except HTTPException:
raise
except Exception as e:
print(f"[Register] Supabase lookup error: {e}")
otp = f"{random.randint(100000, 999999)}"
pwd_hash = hash_password(user_data.password)
_store_otp(email_clean, otp, user_data.username, pwd_hash)
sent = send_otp_email(email_clean, otp)
if not sent:
print(f"[OTP Warning] Failed to send email to {email_clean}. Verification OTP is: {otp}")
raise HTTPException(
status_code=500,
detail="Failed to send verification email. Please check email address or try again."
)
return {
"message": f"Verification code sent to {email_clean}",
"email": email_clean
}
@app.post("/register/verify-otp")
def verify_registration_otp(verify_data: VerifyOtpInput):
email_clean = verify_data.email.strip().lower()
otp_data = _verify_otp_data(email_clean, verify_data.otp)
if not otp_data:
raise HTTPException(
status_code=400,
detail="Invalid or expired 6-digit verification code. Please check your inbox or click Resend."
)
# Double check if user account was created concurrently
res_check = supabase.table("users").select("id, email").eq("email", email_clean).execute()
if res_check.data:
user = res_check.data[0]
else:
username = otp_data.get("username") or verify_data.username
pwd_hash = otp_data.get("password_hash") or hash_password(verify_data.password)
insert_res = (supabase.table("users").insert({
"username": username,
"email": email_clean,
"password_hash": pwd_hash
}).execute())
if not insert_res.data:
raise HTTPException(status_code=500, detail="Failed to create user account.")
user = insert_res.data[0]
token = create_token({"user_id": user["id"], "email": user["email"]})
return {
"message": "Account verified and registered successfully!",
"access_token": token,
"token_type": "bearer",
"user": user
}
@app.post("/register/resend-otp")
def resend_registration_otp(data: ResendOtpInput):
email_clean = data.email.strip().lower()
otp_data = cache_get(f"otp:{email_clean}")
username = "User"
pwd_hash = ""
if otp_data:
username = otp_data.get("username", "User")
pwd_hash = otp_data.get("password_hash", "")
elif email_clean in _memory_otps:
_, stored_data, _ = _memory_otps[email_clean]
username = stored_data.get("username", "User")
pwd_hash = stored_data.get("password_hash", "")
otp = f"{random.randint(100000, 999999)}"
_store_otp(email_clean, otp, username, pwd_hash)
sent = send_otp_email(email_clean, otp)
if not sent:
raise HTTPException(status_code=500, detail="Failed to send verification code email.")
return {"message": f"New verification code sent to {email_clean}"}
@app.post("/auth/forgot-password/send-otp")
@app.post("/auth/forgot-password")
@app.post("/forgot-password")
@app.post("/forgot-password/send-otp")
def forgot_password_send_otp(data: ForgotPasswordSendOtpInput):
email_clean = data.email.strip().lower()
print(f"[ForgotPassword] Request received for email: '{email_clean}'")
# Check if user exists
try:
res = supabase.table("users").select("id, email").eq("email", email_clean).execute()
if not res.data:
print(f"[ForgotPassword] Email '{email_clean}' not found in Supabase users database.")
raise HTTPException(
status_code=404,
detail="No account found with this email address. Please check your spelling or register."
)
except HTTPException:
raise
except Exception as e:
print(f"[ForgotPassword] Supabase check error: {e}")
otp = f"{random.randint(100000, 999999)}"
try:
cache_set(f"reset_otp:{email_clean}", {"otp": str(otp)}, ttl_seconds=600)
except Exception as ce:
print(f"[ForgotPassword] Redis cache error: {ce}")
_memory_otps[f"reset:{email_clean}"] = (str(otp), {"otp": str(otp)}, time.time() + 600)
sent = send_password_reset_otp_email(email_clean, otp)
print(f"[ForgotPassword] send_password_reset_otp_email result for {email_clean}: {sent}")
if not sent:
err_msg = get_last_email_error()
print(f"[Password Reset Warning] Failed to send email to {email_clean}. Detail: {err_msg}")
raise HTTPException(
status_code=500,
detail=f"Failed to deliver password reset OTP email: {err_msg}"
)
return {
"message": f"Password reset verification code sent to {email_clean}",
"email": email_clean
}
@app.post("/auth/forgot-password/reset")
def forgot_password_reset(data: ForgotPasswordResetInput):
email_clean = data.email.strip().lower()
cached_otp_data = cache_get(f"reset_otp:{email_clean}")
stored_otp = None
if cached_otp_data:
stored_otp = str(cached_otp_data.get("otp"))
elif f"reset:{email_clean}" in _memory_otps:
otp_val, _, expires_at = _memory_otps[f"reset:{email_clean}"]
if time.time() < expires_at:
stored_otp = otp_val
if not stored_otp or stored_otp != data.otp.strip():
raise HTTPException(
status_code=400,
detail="Invalid or expired 6-digit verification code. Please request a new OTP."
)
# Clean up OTP after single use
cache_delete(f"reset_otp:{email_clean}")
_memory_otps.pop(f"reset:{email_clean}", None)
# Hash new password and update in Supabase
new_pwd_hash = hash_password(data.new_password)
res_update = supabase.table("users").update({"password_hash": new_pwd_hash}).eq("email", email_clean).execute()
if not res_update.data:
# Fetch user
res_user = supabase.table("users").select("id, email").eq("email", email_clean).execute()
if not res_user.data:
raise HTTPException(status_code=404, detail="User account not found.")
user = res_user.data[0]
else:
user = res_update.data[0]
token = create_token({"user_id": user["id"], "email": user["email"]})
return {
"message": "Password reset successfully!",
"access_token": token,
"token_type": "bearer"
}
@app.post("/login")
def login(user: LoginUser):
res = (supabase.table("users").select("id, email, password_hash").eq("email", user.email).execute())
if not res.data:
raise HTTPException(
status_code=401,
detail="Invalid Email or Password"
)
db_user = res.data[0]
if not verify_password(user.password, db_user["password_hash"]):
raise HTTPException(
status_code=401,
detail="Invalid Email or Password"
)
token = create_token({"user_id": db_user["id"], "email": db_user["email"]})
return {
"message": "Login successful",
"access_token": token,
"token_type": "bearer"
}
@app.get("/profile")
def profile(user_id: int = Depends(get_current_user)):
# Check Redis cache first (30-minute TTL)
cache_key = f"user:{user_id}:profile"
cached = cache_get(cache_key)
if cached:
return cached
user = (supabase.table("users").select("id, username, email, created_at").eq("id", user_id).execute())
if not user.data:
raise HTTPException(
status_code=404,
detail="User not found"
)
result = user.data[0]
cache_set(cache_key, result, ttl_seconds=30 * 60)
return result
@app.post("/job_sources")
def create_source(source: SourceInput, user_id: int = Depends(get_current_user)):
new_source = (supabase.table("job_sources").insert({"source_name": source.source_name, "source_url": source.source_url}).execute())
return {
"message": "Source created successfully",
"source": new_source.data[0]
}
@app.post("/jobs")
def create_job(job: JobsInput, user_id: int = Depends(get_current_user)):
new_job = (supabase.table("jobs").insert({"title": job.title, "company": job.company, "location": job.location, "salary": job.salary, "job_type": job.job_type, "description": job.description, "job_url": job.job_url, "source_id": job.source_id}).execute())
return {
"message": "Job created successfully",
"job": new_job.data[0]
}
@app.get("/jobs/{job_id}")
def get_job(job_id: int, user_id: int = Depends(get_current_user)):
# Check Redis cache first (24-hour TTL β€” jobs are immutable once scraped)
cache_key = f"job:{job_id}"
cached = cache_get(cache_key)
if cached:
return cached
job = (supabase.table("jobs").select("*").eq("id", job_id).execute())
if not job.data:
raise HTTPException(
status_code=404,
detail="Job not found"
)
result = job.data[0]
cache_set(cache_key, result, ttl_seconds=24 * 3600)
return result
@app.get("/get_jobs")
def get_jobs(page: int = 1, per_page: int = 10, user_id: int = Depends(get_current_user)):
# Check Redis cache first (3-hour TTL β€” invalidated on scraper refresh)
cache_key = f"jobs:feed:page:{page}:limit:{per_page}"
cached = cache_get(cache_key)
if cached:
return cached
offset = (page - 1) * per_page
jobs = supabase.table("jobs").select("*").range(offset, offset + per_page - 1).execute()
result = {"jobs": jobs.data, "page": page, "per_page": per_page}
cache_set(cache_key, result, ttl_seconds=3 * 3600)
return result
@app.get("/locations")
def get_locations(user_id: int = Depends(get_current_user)):
# Check Redis cache first (12-hour TTL β€” invalidated on scraper refresh)
cache_key = "jobs:locations"
cached = cache_get(cache_key)
if cached:
return cached
res = supabase.table("jobs").select("location").execute()
locations = sorted(list(set(item["location"].strip() for item in res.data if item.get("location"))))
cache_set(cache_key, locations, ttl_seconds=12 * 3600)
return locations
@app.post("/search_jobs")
def search_jobs(search: SearchJob, page: int = 1, per_page: int = 10, user_id: int = Depends(get_current_user)):
query = supabase.table("jobs").select("*")
if search.location:
query = query.ilike("location", f"%{search.location}%")
if search.company:
query = query.ilike("company", f"%{search.company}%")
if search.job_type:
query = query.eq("job_type", search.job_type)
jobs = query.execute()
results = jobs.data
if search.keyword:
keywords = search.keyword.lower().split()
filtered = []
for job in results:
title = (job.get("title") or "").lower()
company = (job.get("company") or "").lower()
desc = (job.get("description") or "").lower()
if all(any(kw in field for field in (title, company, desc)) for kw in keywords):
filtered.append(job)
results = filtered
if search.min_salary:
filtered_results = []
for job in results:
job_salary = job.get("salary")
if job_salary is not None:
try:
salary_int = int(job_salary)
if salary_int >= search.min_salary:
filtered_results.append(job)
except (ValueError, TypeError):
pass
results = filtered_results
offset = (page - 1) * per_page
paginated_results = results[offset:offset + per_page]
return {"jobs": paginated_results, "page": page, "per_page": per_page, "total": len(results)}
@app.post("/save_job")
def save_job(saved_job: SavedJob, user_id: int = Depends(get_current_user)):
existing = (supabase.table("saved_jobs").select("*").eq("user_id", user_id).eq("job_id", saved_job.job_id).execute())
if existing.data:
raise HTTPException(
status_code=400,
detail="Job already saved"
)
saved = (supabase.table("saved_jobs").insert({"user_id": user_id,"job_id": saved_job.job_id}).execute())
# Invalidate saved_jobs cache for this user
cache_delete(f"user:{user_id}:saved_jobs")
return {
"message": "Job saved successfully",
"saved_job": saved.data[0]
}
@app.get("/saved_jobs")
def get_saved_jobs(user_id: int = Depends(get_current_user)):
# Check Redis cache first (1-hour TTL β€” invalidated on save/unsave)
cache_key = f"user:{user_id}:saved_jobs"
cached = cache_get(cache_key)
if cached is not None:
return cached
saved = (supabase.table("saved_jobs").select("*, jobs(*)").eq("user_id", user_id).execute())
result = saved.data
cache_set(cache_key, result, ttl_seconds=3600)
return result
@app.delete("/saved_jobs/{job_id}")
def unsave_job(job_id: int, user_id: int = Depends(get_current_user)):
deleted = (supabase.table("saved_jobs").delete().eq("user_id", user_id).eq("job_id", job_id).execute())
# Invalidate saved_jobs cache for this user
cache_delete(f"user:{user_id}:saved_jobs")
return {"message": "Job unsaved successfully"}
@app.get("/job_sources")
def get_sources(user_id: int = Depends(get_current_user)):
sources = (supabase.table("job_sources").select("*").execute())
return sources.data
@app.delete("/job_sources/{source_id}")
def delete_source(source_id: int, user_id: int = Depends(get_current_user)):
deleted = (supabase.table("job_sources").delete().eq("id", source_id).execute())
return {"message": "Job source deleted successfully"}
@app.post("/alert_preferences")
def create_alert(alert: AlertPreference, background_tasks: BackgroundTasks, user_id: int = Depends(get_current_user)):
new_alert = (supabase.table("alert_preferences").insert({
"user_id": user_id,
"keyword": alert.keyword,
"location": alert.location,
"min_salary": alert.min_salary,
"email_enabled": alert.email_enabled
}).execute()
)
if alert.email_enabled:
from alerts import send_immediate_alerts
background_tasks.add_task(
send_immediate_alerts,
user_id,
alert.keyword,
alert.location,
alert.min_salary
)
return {"message": "Alert preference created successfully", "alert": new_alert.data[0]}
@app.get("/alert_preferences")
def get_alerts(user_id: int = Depends(get_current_user)):
alerts = (supabase.table("alert_preferences").select("*").eq("user_id", user_id).execute())
return alerts.data
@app.delete("/alert_preferences/{alert_id}")
def delete_alert(alert_id: int, user_id: int = Depends(get_current_user)):
deleted = (supabase.table("alert_preferences").delete().eq("user_id", user_id).eq("id", alert_id).execute())
return {"message": "Alert preference deleted successfully"}
@app.post("/resume/upload")
def upload_resume(file: UploadFile = File(...), user_id: int = Depends(get_current_user)):
with tempfile.NamedTemporaryFile(delete=False, suffix=f"_{file.filename}") as temp:
shutil.copyfileobj(file.file, temp)
temp_path = temp.name
try:
text = extract_text(temp_path)
# 1. ALWAYS save raw resume text to user_resumes table first
embedding = None
try:
embedding = get_embedding(text)
except Exception as e:
print(f"Notice embedding generation error: {e}")
try:
save_resume(user_id, text, embedding)
except Exception as e:
print(f"Notice save_resume error: {e}")
try:
# 2. Chunk document and save vector chunks to pgvector
chunks = chunk_resume_text(text, chunk_size=800, chunk_overlap=100)
chunk_embeddings = get_embeddings_batch(chunks)
save_resume_chunks(user_id, chunks, chunk_embeddings)
except Exception as e:
print(f"Notice save_resume_chunks error: {e}")
# 3. Invalidate all user caches after resume upload
invalidate_user_cache(user_id)
matched = []
if embedding:
try:
matched = match_jobs(embedding, limit=10)
except Exception as e:
print(f"Notice match_jobs error: {e}")
try:
# 4. LLM Parallel Execution: run analysis & profile extraction concurrently
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=2) as executor:
future_analysis = executor.submit(analyze_resume_data, text, matched)
future_profile = executor.submit(extract_ai_profile, text)
analysis = future_analysis.result()
profile = future_profile.result()
profile["job_fit_score"] = analysis.get("match_score", 80)
profile["recommendation"] = analysis.get("recommendation", "")
if analysis.get("extracted_skills"):
profile["top_skills"] = analysis["extracted_skills"]
save_ai_profile(user_id, profile)
return {"message": "Resume uploaded and vector chunks stored successfully", "analysis": analysis, "profile": profile}
except Exception as e:
print(f"Error processing AI profile analysis: {e}")
skills = extract_skills_local(text)
analysis = {
"match_score": 75,
"extracted_skills": skills,
"recommendation": "Your resume has been parsed. You can now chat with HirePulse Pivot AI for personalized recommendations.",
}
profile = {"top_skills": skills, "experience_level": "fresher", "preferred_roles": [], "education": "", "projects_summary": ""}
save_ai_profile(user_id, profile)
return {"message": "Resume parsed successfully", "analysis": analysis, "profile": profile}
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
def _job_to_dict(job_data, reason="Matches your profile."):
return {
"id": job_data["id"], "title": job_data["title"],
"company": job_data["company"], "location": job_data["location"],
"salary": job_data["salary"], "description": job_data["description"],
"job_url": job_data.get("job_url", "#"), "match_reason": reason,
}
def _fill_job_urls(matched_jobs_dict):
if not matched_jobs_dict:
return
try:
job_ids = list(matched_jobs_dict.keys())
db_jobs = supabase.table("jobs").select("id, job_url").in_("id", job_ids).execute().data or []
url_map = {j["id"]: j.get("job_url") for j in db_jobs}
for j_id, job in matched_jobs_dict.items():
job.setdefault("job_url", url_map.get(j_id, "#"))
except Exception as e:
print(f"Error fetching job URLs: {e}")
@app.post("/resume/chat")
def chat_with_resume(chat_input: ResumeChatInput, user_id: int = Depends(get_current_user)):
resume = get_resume(user_id)
ai_profile = get_ai_profile(user_id)
resume_text = resume["resume_text"] if resume else ""
user_skills = (ai_profile.get("top_skills") or []) if ai_profile else (extract_skills_local(resume_text) if resume_text else [])
if not resume_text and not user_skills:
return {
"response": "Please upload your resume using the box on the left first. Once uploaded, I can match jobs to your skills and assist with your career!",
"matches": [],
"remaining_tokens": _get_remaining_tokens(user_id),
"max_tokens_window": MAX_TOKENS_PER_WINDOW,
"window_minutes": WINDOW_MINUTES
}
# Instant greeting short-circuit (0ms, 0 tokens consumed)
msg_clean = chat_input.message.strip().lower()
greetings = {"hi", "hii", "hello", "hey", "hi there", "hello there", "good morning", "good evening"}
if msg_clean in greetings or any(msg_clean.startswith(g) for g in ["hi ", "hii ", "hello ", "hey "]):
skills_fmt = ", ".join(user_skills[:5]) if user_skills else "your technical domain"
return {
"response": f"Hello! How can I assist you with your career or resume optimization today? Feel free to ask for job recommendations, resume feedback, or skill guidance tailored to your background in {skills_fmt}.",
"matches": [],
"remaining_tokens": _get_remaining_tokens(user_id),
"max_tokens_window": MAX_TOKENS_PER_WINDOW,
"window_minutes": WINDOW_MINUTES
}
# Estimate token cost (~450 tokens per AI query execution) and check rate limit
rem_tokens = _consume_tokens(user_id, tokens=450)
# Format context of last 3 conversation messages
history_str = ""
if chat_input.history:
recent_3 = chat_input.history[-3:]
history_lines = []
for item in recent_3:
role = "User" if item.sender.lower() == "user" else "Assistant"
history_lines.append(f"{role}: {item.text}")
if history_lines:
history_str = "RECENT CHAT HISTORY:\n" + "\n".join(history_lines)
skills_str = ", ".join(user_skills[:4])
roles_str = ", ".join(ai_profile.get("preferred_roles") or []) if ai_profile else ""
context = f"Candidate Skills: {skills_str}. Roles: {roles_str}. Query: {chat_input.message}"
if history_str:
context = f"{history_str}\n{context}"
matched_jobs = []
try:
search_emb = get_embedding(context, task="RETRIEVAL_QUERY")
matched_jobs = match_jobs(search_emb, limit=8)
except Exception as e:
print(f"Notice matching jobs error: {e}")
ai_result = generate_answer(
resume=resume_text, jobs=matched_jobs, query=chat_input.message,
total_jobs=100, saved_jobs_count=0, user_skills=user_skills,
history=history_str
)
matched_jobs_dict = {j["id"]: j for j in matched_jobs}
_fill_job_urls(matched_jobs_dict)
structured = []
for item in ai_result.get("jobs", []):
if item.get("id") in matched_jobs_dict:
structured.append(_job_to_dict(matched_jobs_dict[item["id"]], item.get("match_reason", "A suitable match.")))
return {
"response": ai_result.get("text", "Here are the recommendations for your profile:"),
"matches": structured,
"remaining_tokens": rem_tokens,
"max_tokens_window": MAX_TOKENS_PER_WINDOW,
"window_minutes": WINDOW_MINUTES
}
@app.get("/resume/analysis")
def get_user_resume_analysis(user_id: int = Depends(get_current_user)):
# Check Redis cache first (24-hour TTL β€” invalidated on resume upload)
analysis_cache_key = f"user:{user_id}:resume_analysis"
cached_analysis = cache_get(analysis_cache_key)
if cached_analysis:
cached_analysis["remaining_tokens"] = _get_remaining_tokens(user_id)
return cached_analysis
resume = get_resume(user_id)
ai_profile = get_ai_profile(user_id)
rem_tokens = _get_remaining_tokens(user_id)
if not resume and not ai_profile:
return {
"has_resume": False,
"remaining_tokens": rem_tokens,
"max_tokens_window": MAX_TOKENS_PER_WINDOW,
"window_minutes": WINDOW_MINUTES
}
rec = None
if ai_profile:
rec = ai_profile.get("recommendation")
if not rec and ai_profile.get("missing_skills"):
ms = ai_profile["missing_skills"]
if isinstance(ms, list) and len(ms) > 0:
rec = ms[0]
elif isinstance(ms, str):
rec = ms
resume_text = resume["resume_text"] if resume else ""
if (not rec or "Consider highlighting core project metrics" in rec or "Your resume has been processed" in rec) and resume_text:
try:
matched = match_jobs(get_embedding(resume_text), limit=5)
analysis = analyze_resume_data(resume_text, matched)
rec = analysis.get("recommendation")
if ai_profile and rec:
ai_profile["recommendation"] = rec
save_ai_profile(user_id, ai_profile)
except Exception as e:
rec = "Highlight cloud infrastructure (AWS/Docker) and system performance metrics in your projects to optimize your profile for target developer roles."
skills = (ai_profile.get("top_skills") if ai_profile else None) or (extract_skills_local(resume_text) if resume_text else [])
score = (ai_profile.get("job_fit_score") if ai_profile else None) or 85
result = {
"has_resume": True,
"analysis": {
"match_score": score,
"extracted_skills": skills,
"recommendation": rec or "Your resume has been processed. You can now chat with HirePulse Pivot AI for personalized guidance."
},
"profile": ai_profile or {"top_skills": skills, "job_fit_score": score},
"remaining_tokens": rem_tokens,
"max_tokens_window": MAX_TOKENS_PER_WINDOW,
"window_minutes": WINDOW_MINUTES
}
# Cache the result for 24 hours (minus the dynamic remaining_tokens)
cache_set(analysis_cache_key, result, ttl_seconds=24 * 3600)
return result
@app.post("/logout")
def logout_user_session(user_id: int = Depends(get_current_user)):
# User session logout: keep saved user resume and AI profile permanently in database
return {"message": "Logged out successfully."}
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", 7860))
uvicorn.run("main:app", host="0.0.0.0", port=port)