import re import json import io import os from datetime import datetime from pathlib import Path from groq import Groq from fastapi import FastAPI, HTTPException, UploadFile, File, Form from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel, ValidationError from mcp.server.fastmcp import FastMCP from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse def load_local_env() -> None: env_paths = [ Path(__file__).with_name(".env"), Path(__file__).resolve().parent.parent / ".env", ] for env_path in env_paths: if not env_path.exists(): continue with env_path.open("r", encoding="utf-8") as f: for line in f: line = line.strip() if not line or line.startswith("#") or "=" not in line: continue name, value = line.split("=", 1) name = name.strip() value = value.strip().strip('"').strip("'") if name and name not in os.environ: os.environ[name] = value load_local_env() app = FastAPI() mcp = FastMCP("Naaviverse") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) # Health check for HF Spaces @app.get("/api/health") async def health_check(): return {"status": "ok"} # ── Groq API config ──────────────────────────────────────────────── GROQ_API_KEY = os.getenv("GROQ_API_KEY", "").strip() GROQ_MODEL = os.getenv("MODEL_NAME", "llama3-70b-8192").strip() USAGE_STATS_FILE = Path(os.getenv( "USAGE_STATS_FILE", Path(__file__).with_name("groq_usage_stats.json") )) # Initialize Groq client client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None def today_key() -> str: return datetime.now().strftime("%Y-%m-%d") def empty_day_stats() -> dict: return { "total_calls" : 0, "total_input_tokens" : 0, "total_output_tokens": 0, "total_tokens" : 0, "calls" : [] } def load_usage_stats() -> dict: if not USAGE_STATS_FILE.exists(): return {} try: with USAGE_STATS_FILE.open("r", encoding="utf-8") as f: return json.load(f) except (json.JSONDecodeError, OSError): return {} def save_usage_stats(stats: dict) -> None: USAGE_STATS_FILE.parent.mkdir(parents=True, exist_ok=True) with USAGE_STATS_FILE.open("w", encoding="utf-8") as f: json.dump(stats, f, indent=2) usage_stats = load_usage_stats() token_log = usage_stats.get(today_key(), empty_day_stats()) # ── Pydantic models ──────────────────────────────────────────────── class GoalRequest(BaseModel): goal: str class MacroView(BaseModel): title: str = "" why_it_matters: str = "" class MicroView(BaseModel): title: str = "" how_its_done: str = "" class NanoView(BaseModel): title: str = "" what_you_do: str = "" class PathStep(BaseModel): id: int title: str description: str macro: MacroView micro: MicroView nano: NanoView class CareerPathResponse(BaseModel): path_name: str path_description: str total_steps: int steps: list[PathStep] # ── Content filters ──────────────────────────────────────────────── BLOCKED_TOPIC_PATTERNS = [ r"\b(love|crush|dating|date|romance|romantic|relationship|breakup|ex)\b", r"\b(girlfriend|boyfriend|wife|husband|marriage|marry|proposal)\b", r"\b(flirt|kiss|sex|sexual|hookup|hot girl|hot boy)\b", ] EDUCATION_TOPIC_PATTERNS = [ r"\b(learn|study|course|class|school|college|university|degree|exam|certification)\b", r"\b(career|job|profession|internship|resume|interview|portfolio|skill|training)\b", r"\b(become|switch|break into|prepare|master|improve|build)\b", r"\b(engineer|developer|designer|analyst|scientist|manager|teacher|doctor|nurse|lawyer)\b", ] def ensure_education_goal(goal: str) -> None: normalized = goal.strip().lower() if not normalized: raise HTTPException(status_code=400, detail="Goal cannot be empty") if any(re.search(pattern, normalized) for pattern in BLOCKED_TOPIC_PATTERNS): raise HTTPException( status_code=400, detail="This agent only supports education, career, learning, and skill-building goals." ) if not any(re.search(pattern, normalized) for pattern in EDUCATION_TOPIC_PATTERNS): raise HTTPException( status_code=400, detail="Please enter an education or career goal, such as 'Become a Data Analyst' or 'Prepare for NEET'." ) def ensure_document_scope(content: str) -> None: normalized = content.strip().lower() has_blocked_topic = any(re.search(p, normalized) for p in BLOCKED_TOPIC_PATTERNS) has_education_topic = any(re.search(p, normalized) for p in EDUCATION_TOPIC_PATTERNS) if has_blocked_topic and not has_education_topic: raise HTTPException( status_code=400, detail="Uploaded content must be related to education, career, learning, or skills." ) def validate_path_response(data: dict) -> dict: try: if hasattr(CareerPathResponse, "model_validate"): validated = CareerPathResponse.model_validate(data) result = validated.model_dump() else: validated = CareerPathResponse.parse_obj(data) result = validated.dict() except ValidationError as e: raise HTTPException(status_code=500, detail=f"Invalid path format from model: {e}") if result["total_steps"] != len(result["steps"]): result["total_steps"] = len(result["steps"]) for index, step in enumerate(result["steps"], start=1): step["id"] = index return result # ── Prompt templates ─────────────────────────────────────────────── PROMPT_TEMPLATE = """You are the Naaviverse career path engine. Generate a structured career path for this goal: "{goal}" This agent is ONLY for education, career, learning, exam preparation, and skill-building goals. If the goal is about love, dating, romance, girlfriend, boyfriend, marriage, entertainment gossip, or any unrelated personal topic, respond with this exact JSON: {{ "path_name": "UNSUPPORTED GOAL", "path_description": "This agent only supports education, career, learning, and skill-building goals.", "total_steps": 0, "steps": [] }} Decide how many steps are genuinely needed: - Simple goals (e.g. Learn Excel): 2-3 steps - Mid-level goals (e.g. Become a UX Designer): 3-4 steps - Complex goals (e.g. Become a Data Scientist, career switches): 4-5 steps Respond ONLY with valid JSON. No markdown, no backticks, no explanation. {{ "path_name": "", "path_description": "<1 concise sentence describing who this path is for and what it achieves>", "total_steps": , "steps": [ {{ "id": 1, "title": "", "description": "<1 sentence describing what this step covers and achieves>", "macro": {{ "title": "", "why_it_matters": "<1 concise sentence on the strategic importance of this step>" }}, "micro": {{ "title": "", "how_its_done": "<1 concise sentence on exactly how to execute with specific tools, methods, platforms>" }}, "nano": {{ "title": "", "what_you_do": "<1 concise sentence describing the actual project or action to complete>" }} }} ] }} Rules: - path_name must be specific to the goal — never generic - Step titles must be meaningful phase names, NOT Step 1 or Phase 1 - macro = big picture strategy: why this step matters - micro = specific skill/method: exactly what to learn with real platform names - nano = hands-on deliverable: a real project or action to complete - Resources in micro must be real: Coursera, freeCodeCamp, specific book titles, YouTube channels """ FILE_PROMPT = """You are the Naaviverse career path engine. A user has uploaded a document. Here is the extracted text: --- DOCUMENT START --- {content} --- DOCUMENT END --- {goal_line} Read the document carefully. Identify background, skills, education, experience. Generate a HIGHLY PERSONALIZED career path based on their profile. Only generate paths related to education, career, learning, exam preparation, and skill-building. Respond ONLY with valid JSON. No markdown, no backticks, no explanation. {{ "path_name": "", "path_description": "<1 concise sentence personalized to their actual background and goal>", "total_steps": , "steps": [ {{ "id": 1, "title": "", "description": "<1 sentence personalized to their background>", "macro": {{ "title": "", "why_it_matters": "<1 concise sentence on why this matters given their starting point>" }}, "micro": {{ "title": "", "how_its_done": "<1 concise sentence on exactly how to learn it>" }}, "nano": {{ "title": "", "what_you_do": "<1 concise sentence on the actual hands-on thing to build or do>" }} }} ] }} """ # ── Core Groq caller ─────────────────────────────────────────────── def call_groq(prompt: str, label: str = "goal") -> dict: global token_log, client api_key = os.getenv("GROQ_API_KEY", "").strip() or GROQ_API_KEY if not api_key: raise HTTPException( status_code=500, detail="Groq API key missing. Set GROQ_API_KEY environment variable." ) # Re-initialize client if key changed after startup (e.g. HF Spaces secrets) if client is None or client.api_key != api_key: client = Groq(api_key=api_key) model = os.getenv("MODEL_NAME", GROQ_MODEL).strip() try: response = client.chat.completions.create( model=model, messages=[ { "role": "system", "content": ( "You are a career path engine. " "Respond ONLY with valid JSON. " "No markdown, no backticks, no explanation whatsoever." ) }, { "role": "user", "content": prompt } ], max_tokens=4096, temperature=0.7, ) except Exception as e: error_msg = str(e) if "401" in error_msg or "invalid_api_key" in error_msg.lower() or "unauthorized" in error_msg.lower(): raise HTTPException(status_code=401, detail="Invalid Groq API key. Check your GROQ_API_KEY.") if "429" in error_msg or "rate_limit" in error_msg.lower(): raise HTTPException(status_code=429, detail="Groq rate limit hit. Please wait and try again.") if "quota" in error_msg.lower() or "exceeded" in error_msg.lower(): raise HTTPException(status_code=429, detail="Groq quota exceeded. Check your plan.") raise HTTPException(status_code=500, detail=f"Groq API error: {error_msg}") # ── Extract token usage ──────────────────────────────────────── usage = response.usage input_tokens = usage.prompt_tokens if usage else 0 output_tokens = usage.completion_tokens if usage else 0 total_tokens = usage.total_tokens if usage else (input_tokens + output_tokens) current_day = today_key() token_log = usage_stats.get(current_day, empty_day_stats()) token_log["total_calls"] += 1 token_log["total_input_tokens"] += input_tokens token_log["total_output_tokens"] += output_tokens token_log["total_tokens"] += total_tokens token_log["calls"].append({ "call" : token_log["total_calls"], "type" : label, "input_tokens" : input_tokens, "output_tokens": output_tokens, "total_tokens" : total_tokens, }) usage_stats[current_day] = token_log save_usage_stats(usage_stats) # ── Terminal log ─────────────────────────────────────────────── print("\n" + "=" * 55) print(f" CALL #{token_log['total_calls']} [{label}]") print("=" * 55) print(f" Input tokens : {input_tokens:,}") print(f" Output tokens : {output_tokens:,}") print(f" Total this call: {total_tokens:,}") print("-" * 55) print(f" RUNNING TOTALS:") print(f" Total calls : {token_log['total_calls']}") print(f" Input tokens : {token_log['total_input_tokens']:,}") print(f" Output tokens : {token_log['total_output_tokens']:,}") print(f" Total tokens : {token_log['total_tokens']:,}") print(f" Provider : Groq") print(f" Model : {model}") print(f" Used this run : {token_log['total_calls']} calls") print("=" * 55 + "\n") try: raw = response.choices[0].message.content except (IndexError, AttributeError): raise HTTPException(status_code=500, detail="Groq returned no text content.") raw = re.sub(r"```(?:json)?", "", raw).strip().strip("`") result = validate_path_response(json.loads(raw)) result["_token_usage"] = { "this_call": { "input_tokens" : input_tokens, "output_tokens": output_tokens, "total_tokens" : total_tokens, }, "running_total": { "calls" : token_log["total_calls"], "input_tokens" : token_log["total_input_tokens"], "output_tokens": token_log["total_output_tokens"], "total_tokens" : token_log["total_tokens"], } } return result # ── File extractors ──────────────────────────────────────────────── def extract_text_from_pdf(file_bytes: bytes) -> str: try: import pdfplumber with pdfplumber.open(io.BytesIO(file_bytes)) as pdf: pages = [page.extract_text() or "" for page in pdf.pages] return "\n".join(pages).strip() except ImportError: raise HTTPException(status_code=500, detail="Run: pip install pdfplumber") def extract_text_from_docx(file_bytes: bytes) -> str: try: import docx doc = docx.Document(io.BytesIO(file_bytes)) return "\n".join([p.text for p in doc.paragraphs]).strip() except ImportError: raise HTTPException(status_code=500, detail="Run: pip install python-docx") def extract_text_from_image(file_bytes: bytes) -> str: try: from PIL import Image import pytesseract img = Image.open(io.BytesIO(file_bytes)) return pytesseract.image_to_string(img).strip() except ImportError: try: from PIL import Image img = Image.open(io.BytesIO(file_bytes)) w, h = img.size return f"[Image {w}x{h}px — OCR not available. Type your goal manually.]" except Exception: return "[Image could not be read.]" except Exception as e: return f"[Image extraction failed: {str(e)}]" # ── MCP tool ─────────────────────────────────────────────────────── @mcp.tool() async def generate_career_path(goal: str): """Generate a personalized career path.""" ensure_education_goal(goal) prompt = PROMPT_TEMPLATE.format(goal=goal.strip()) return call_groq(prompt, label=goal[:40]) # ── Endpoint 1: Text goal ────────────────────────────────────────── @app.post("/api/path") async def generate_path(req: GoalRequest): ensure_education_goal(req.goal) try: prompt = PROMPT_TEMPLATE.format(goal=req.goal.strip()) return call_groq(prompt, label=req.goal.strip()[:40]) except HTTPException: raise except json.JSONDecodeError as e: raise HTTPException(status_code=500, detail=f"Invalid JSON from model: {e}") except Exception as e: raise HTTPException(status_code=500, detail=f"⚠ {str(e)}") # ── Endpoint 2: File upload ──────────────────────────────────────── @app.post("/api/path-from-file") async def generate_path_from_file( file: UploadFile = File(...), goal: str = Form("") ): file_bytes = await file.read() content_type = file.content_type or "" filename = file.filename or "" content = "" if content_type == "application/pdf" or filename.endswith(".pdf"): content = extract_text_from_pdf(file_bytes) elif "wordprocessingml" in content_type or filename.endswith(".docx"): content = extract_text_from_docx(file_bytes) elif content_type == "text/plain" or filename.endswith(".txt"): content = file_bytes.decode("utf-8", errors="ignore") elif content_type.startswith("image/") or filename.lower().endswith((".png", ".jpg", ".jpeg")): content = extract_text_from_image(file_bytes) else: raise HTTPException(status_code=400, detail=f"Unsupported file type: {content_type}") if not content.strip(): raise HTTPException(status_code=400, detail="Could not extract text. Please type your goal manually.") if goal.strip(): ensure_education_goal(goal) else: ensure_document_scope(content) content = content[:3000] goal_line = ( f"The user's stated career goal is: {goal.strip()}" if goal.strip() else "Based on the document, infer the most suitable career goal for this person." ) try: prompt = FILE_PROMPT.format(content=content, goal_line=goal_line) return call_groq(prompt, label=f"file:{filename[:30]}") except HTTPException: raise except json.JSONDecodeError as e: raise HTTPException(status_code=500, detail=f"Invalid JSON from model: {e}") except Exception as e: raise HTTPException(status_code=500, detail=f"⚠ {str(e)}") # ── Endpoint 3: Token stats ──────────────────────────────────────── @app.get("/api/token-stats") async def get_token_stats(): all_usage_stats = load_usage_stats() today = today_key() today_stats = all_usage_stats.get( today, token_log if token_log["total_calls"] else empty_day_stats() ) return { "provider" : "groq", "model" : GROQ_MODEL, "date" : today, "stats_source" : str(USAGE_STATS_FILE), "total_calls" : today_stats["total_calls"], "total_input_tokens" : today_stats["total_input_tokens"], "total_output_tokens": today_stats["total_output_tokens"], "total_tokens_used" : today_stats["total_tokens"], "quota_note" : "Using Groq API. Token usage is tracked per call.", "call_history" : today_stats["calls"], "daily_usage_history": all_usage_stats, } # ── MCP + Frontend ───────────────────────────────────────────────── app.mount("/mcp", mcp.sse_app()) frontend_dist = Path(__file__).resolve().parent.parent / "Agent_Frontend" / "dist" if frontend_dist.exists(): app.mount("/assets", StaticFiles(directory=str(frontend_dist / "assets")), name="assets") @app.get("/{full_path:path}") async def serve_frontend(full_path: str): file_path = frontend_dist / full_path if file_path.exists() and file_path.is_file(): return FileResponse(file_path) return FileResponse(frontend_dist / "index.html")