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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff8f2b3
 
ddbcbee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff8f2b3
 
 
ddbcbee
 
 
 
 
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
import json
import os
import re
import pandas as pd
import random
from typing import Dict, Optional, Any
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from dotenv import load_dotenv
from langchain_tavily import TavilySearch
import google.generativeai as genai

# Load environment variables
load_dotenv()
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")

# Configure Google AI
genai.configure(api_key=GOOGLE_API_KEY)

# Load LeetCode data
OUTPUT_FILE = "leetcode_downloaded.xlsx"
try:
    LEETCODE_DATA = pd.read_excel(OUTPUT_FILE)
    print(f"Loaded {len(LEETCODE_DATA)} LeetCode problems from local file.")
except FileNotFoundError:
    print("Warning: LeetCode data file not found. Some features may not work.")
    LEETCODE_DATA = pd.DataFrame()

# User sessions for mock interviews
user_sessions = {}

# β€”β€”β€” Pydantic Models β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
class ChatRequest(BaseModel):
    user_id: str = "default"
    session_id: str = "default"
    message: str

class ChatResponse(BaseModel):
    status: str
    response: str
    session_id: str

class HealthResponse(BaseModel):
    status: str
    google_api_configured: bool
    leetcode_problems_loaded: int
    tavily_search_available: bool

# β€”β€”β€” Utility Functions β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
def preprocess_query(query: str) -> str:
    """Preprocess user query for better understanding"""
    return query.strip()

# β€”β€”β€” Tool 1: Get Daily Coding Question β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
def get_daily_coding_question(query=""):
    """Get 3 random coding questions (one from each difficulty level)"""
    if LEETCODE_DATA.empty:
        return {"status": "error", "response": "LeetCode data not available. Please check the data file."}
    
    response = "Here are your coding challenges for today:\n\n"

    problem_match = re.search(r'problem[\s_]*(\d+)', query, re.IGNORECASE)
    if problem_match:
        problem_no = int(problem_match.group(1))
        specific_problem = LEETCODE_DATA[LEETCODE_DATA['problem_no'] == problem_no]
        if not specific_problem.empty:
            p = specific_problem.iloc[0]
            response = f"**Problem {p['problem_no']}: {p['problem_statement']}**\n"
            response += f"**Difficulty**: {p['problem_level']}\n"
            response += f"**Link**: {p['problem_link']}\n\n"
            response += "Good luck with this problem!"
            return {"status": "success", "response": response}
        else:
            return {"status": "error", "response": "Problem not found. Try a different number!"}

    easy = LEETCODE_DATA[LEETCODE_DATA['problem_level'] == 'Easy']
    medium = LEETCODE_DATA[LEETCODE_DATA['problem_level'] == 'Medium']
    hard = LEETCODE_DATA[LEETCODE_DATA['problem_level'] == 'Hard']

    for label, df in [("🟒 Easy", easy), ("🟑 Medium", medium), ("πŸ”΄ Hard", hard)]:
        if not df.empty:
            q = df.sample(1).iloc[0]
            response += f"**{label} Challenge**\n"
            response += f"Problem {q['problem_no']}: {q['problem_statement']}\n"
            response += f"Link: {q['problem_link']}\n\n"

    response += "Choose one that matches your skill level and start coding!"
    return {"status": "success", "response": response}

# β€”β€”β€” Tool 2: Fetch Interview Questions β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
def fetch_interview_questions(query):
    if not TAVILY_API_KEY:
        return {"status": "error", "response": "Tavily API key not configured."}

    try:
        tavily = TavilySearch(api_key=TAVILY_API_KEY, max_results=3)
        search_response = tavily.invoke(f"{query} interview questions")

        # Extract the results list from the response dictionary
        results = search_response.get("results", []) if isinstance(search_response, dict) else search_response

        if not results:
            return {"status": "success", "response": f"No results found for '{query}' interview questions."}

        search_results = f"Here are the top 3 resources for {query} interview questions:\n\n"
        for i, res in enumerate(results[:3], 1):
            t = res.get('title', 'No title')
            u = res.get('url', 'No URL')
            c = res.get('content', '')
            snippet = c[:200] + '...' if len(c) > 200 else c
            search_results += f"**{i}. {t}**\nURL: {u}\nPreview: {snippet}\n\n"

        model = genai.GenerativeModel('gemini-1.5-flash')
        guidance = model.generate_content(f"""
            Based on the topic '{query}', provide practical advice on how to prepare for and tackle interview questions in this area.
            Include:
            1. Key concepts to focus on
            2. Common question types
            3. How to structure answers
            4. Tips for success

            Keep it concise and actionable.
        """).text

        final = search_results + "\n**πŸ’‘ How to Tackle These Interviews:**\n\n" + guidance
        return {"status": "success", "response": final}
    
    except Exception as e:
        return {"status": "error", "response": f"Error fetching interview questions: {str(e)}"}

# β€”β€”β€” Tool 3: Simulate Mock Interview β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
def simulate_mock_interview(query, user_id="default", session_id="default"):
    session_key = f"mock_{user_id}_{session_id}"
    if session_key not in user_sessions:
        user_sessions[session_key] = {
            "stage": "tech_stack",
            "tech_stack": "",
            "questions_asked": [],
            "answers_given": [],
            "current_question": "",
            "question_count": 0,
            "difficulty": "medium",
            "feedback_history": []
        }
    session = user_sessions[session_key]
    
    try:
        model = genai.GenerativeModel('gemini-1.5-flash')

        # Tech stack collection stage
        if session["stage"] == "tech_stack":
            session["stage"] = "waiting_tech_stack"
            return {"status": "success", "response": (
                "Welcome to your mock interview! 🎯\n\n"
                "Please tell me about your tech stack (e.g., Python, React, multi-agent systems) "
                "or the role you're preparing for (e.g., software engineer, ML engineer)."
            )}

        elif session["stage"] == "waiting_tech_stack":
            session["tech_stack"] = query
            session["stage"] = "interviewing"
            difficulty_options = " (easy/medium/hard)"
            q = model.generate_content(f"""
                Generate a relevant interview question for tech stack: {query}
                Ensure it tests technical knowledge and problem-solving.
                Keep it concise and return only the question.
            """).text.strip()
            
            session.update({
                "current_question": q,
                "questions_asked": [q],
                "question_count": 1
            })
            
            return {"status": "success", "response": (
                f"Great! Based on your tech stack ({query}), let's start your mock interview.\n\n"
                f"**Question 1:** {q}\n"
                f"Set difficulty level{difficulty_options} or proceed. Type 'quit' to end and get your summary."
            )}

        elif session["stage"] == "interviewing":
            if query.lower().strip() in ["easy", "medium", "hard"]:
                session["difficulty"] = query.lower().strip()
                return {"status": "success", "response": (
                    f"Difficulty set to {session['difficulty']}. Let's continue!\n\n"
                    f"**Question {session['question_count']}:** {session['current_question']}\n\n"
                    "Take your time to answer. Type 'quit' to end and get your summary."
                )}

            if query.lower().strip() == "quit":
                return end_mock_interview(session_key)

            # Store answer and provide feedback
            session["answers_given"].append(query)
            feedback = model.generate_content(f"""
                Question: {session['current_question']}
                Answer: {query}
                Tech Stack: {session['tech_stack']}
                Difficulty: {session['difficulty']}

                Provide concise, constructive feedback:
                - What went well
                - Areas to improve
                - Missing points or better approach
                - Suggested follow-up topic
            """).text.strip()
            session["feedback_history"].append(feedback)

            # Generate next question with context
            next_q = model.generate_content(f"""
                Tech stack: {session['tech_stack']}
                Difficulty: {session['difficulty']}
                Previous questions: {session['questions_asked']}
                Follow-up topic suggestion: {feedback.split('\n')[-1] if feedback else ''}

                Generate a new, relevant interview question unseen before.
                Ensure it aligns with the tech stack and difficulty.
                Return only the question.
            """).text.strip()

            session["questions_asked"].append(next_q)
            session["current_question"] = next_q
            session["question_count"] += 1

            return {"status": "success", "response": (
                f"**Feedback on your previous answer:**\n{feedback}\n\n"
                f"**Question {session['question_count']}:** {next_q}\n\n"
                "Type 'quit' to end the interview and get your summary, or set a new difficulty (easy/medium/hard)."
            )}
    
    except Exception as e:
        return {"status": "error", "response": f"Error in mock interview: {str(e)}"}

def end_mock_interview(session_key):
    session = user_sessions[session_key]
    
    try:
        model = genai.GenerativeModel('gemini-1.5-flash')

        summary = model.generate_content(f"""
            Mock Interview Summary:
            Tech Stack: {session['tech_stack']}
            Difficulty: {session['difficulty']}
            Questions Asked: {session['questions_asked']}
            Answers Given: {session['answers_given']}
            Feedback History: {session['feedback_history']}

            Provide a concise overall assessment:
            - Strengths
            - Areas for improvement
            - Key recommendations
            - Common mistakes to avoid
        """).text.strip()

        # Store session data before deletion for response
        tech_stack = session['tech_stack']
        difficulty = session['difficulty']
        questions_count = len(session['questions_asked'])
        
        del user_sessions[session_key]

        return {"status": "success", "response": (
            "🎯 **Mock Interview Complete!**\n\n"
            f"**Interview Summary:**\n"
            f"- Tech Stack: {tech_stack}\n"
            f"- Difficulty: {difficulty}\n"
            f"- Questions Asked: {questions_count}\n\n"
            "**Overall Assessment:**\n" + summary + "\n\n"
            "Great jobβ€”use this feedback to level up! πŸ’ͺ"
        )}
    
    except Exception as e:
        return {"status": "error", "response": f"Error generating interview summary: {str(e)}"}

# β€”β€”β€” Main Agent Class β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
class InterviewPrepAgent:
    def __init__(self):
        if GOOGLE_API_KEY:
            self.model = genai.GenerativeModel('gemini-1.5-flash')
        else:
            self.model = None
        self.tools = {
            "get_daily_coding_question": get_daily_coding_question,
            "fetch_interview_questions": fetch_interview_questions,
            "simulate_mock_interview": simulate_mock_interview
        }

    def classify_query(self, query):
        if not self.model:
            # Fallback classification without AI
            query_lower = query.lower()
            if any(keyword in query_lower for keyword in ['mock', 'interview', 'simulate', 'practice']):
                return "simulate_mock_interview", {"query": query}
            elif any(keyword in query_lower for keyword in ['coding', 'leetcode', 'daily', 'problem']):
                return "get_daily_coding_question", {"query": query}
            else:
                return "fetch_interview_questions", {"query": query}
        
        try:
            prompt = f"""
            Analyze this user query and determine which tool to use:

            Query: "{query}"

            Tools:
            1. get_daily_coding_question – for coding problems, leetcode, daily challenges
            2. fetch_interview_questions – for topic-specific interview question resources
            3. simulate_mock_interview – for mock interview practice or behavioral interviews

            Rules:
            - If query mentions 'mock', 'interview', 'simulate', or 'practice', choose simulate_mock_interview
            - If query mentions 'coding', 'leetcode', 'daily', 'problem', choose get_daily_coding_question
            - If query asks for interview questions on a specific technology (like 'Python interview questions'), choose fetch_interview_questions
            - If unclear, default to simulate_mock_interview

            Respond with JSON: {{"tool": "tool_name", "args": {{"query": "query_text"}}}}
            """
            resp = self.model.generate_content(prompt).text.strip()
            if resp.startswith("```json"):
                resp = resp.replace("```json", "").replace("```", "").strip()
            j = json.loads(resp)
            return j.get("tool"), j.get("args", {})
        except Exception as e:
            # Fallback to simple classification
            return "simulate_mock_interview", {"query": query}

    def process_query(self, query, user_id="default", session_id="default"):
        tool, args = self.classify_query(query)
        if tool not in self.tools:
            return {"status": "error", "response": "Sorry, I didn't get that. Ask for coding practice, interview questions, or mock interview!"}

        if tool == "simulate_mock_interview":
            result = self.tools[tool](args.get("query", query), user_id, session_id)
        else:
            result = self.tools[tool](args.get("query", query))
        
        return result.get("response", "Something went wrong, try again.")

# β€”β€”β€” FastAPI Application β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
app = FastAPI(title="Interview Prep API", version="2.0.0", description="AI-powered interview practice companion")

# Initialize the agent
agent = InterviewPrepAgent()

@app.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
    """
    Process a chat message and return a response
    """
    try:
        query = preprocess_query(request.message)
        response = agent.process_query(query, request.user_id, request.session_id)
        
        return ChatResponse(
            status="success",
            response=response,
            session_id=request.session_id
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing chat: {str(e)}")

@app.get("/health", response_model=HealthResponse)
async def health_check():
    """
    Health check endpoint
    """
    return HealthResponse(
        status="healthy",
        google_api_configured=bool(GOOGLE_API_KEY),
        leetcode_problems_loaded=len(LEETCODE_DATA),
        tavily_search_available=bool(TAVILY_API_KEY)
    )

@app.get("/")
async def root():
    """
    Root endpoint with API information
    """
    return {
        "message": "Interview Prep API v2.0.0",
        "description": "AI-powered interview practice companion",
        "endpoints": {
            "/chat": "POST - Send chat messages",
            "/health": "GET - Health check",
            "/docs": "GET - API documentation",
            "/examples": "GET - Example requests"
        }
    }

@app.get("/examples")
async def get_examples():
    """
    Get example requests for the API
    """
    return {
        "examples": [
            {
                "description": "Get daily coding questions",
                "request": {
                    "user_id": "user123",
                    "session_id": "session456",
                    "message": "Give me daily coding questions"
                }
            },
            {
                "description": "Start a mock interview",
                "request": {
                    "user_id": "user123",
                    "session_id": "session456",
                    "message": "Start a mock interview"
                }
            },
            {
                "description": "Get Python interview questions",
                "request": {
                    "user_id": "user123",
                    "session_id": "session456",
                    "message": "Python interview questions"
                }
            },
            {
                "description": "Get specific LeetCode problem",
                "request": {
                    "user_id": "user123",
                    "session_id": "session456",
                    "message": "Show me problem 1"
                }
            }
        ]
    }

@app.delete("/session/{user_id}/{session_id}")
async def clear_session(user_id: str, session_id: str):
    """
    Clear a specific user session
    """
    session_key = f"mock_{user_id}_{session_id}"
    if session_key in user_sessions:
        del user_sessions[session_key]
        return {"message": f"Session {session_id} for user {user_id} cleared successfully"}
    else:
        raise HTTPException(status_code=404, detail="Session not found")

@app.get("/sessions/{user_id}")
async def get_user_sessions(user_id: str):
    """
    Get all sessions for a specific user
    """
    user_session_keys = [key for key in user_sessions.keys() if key.startswith(f"mock_{user_id}_")]
    sessions = []
    for key in user_session_keys:
        session_id = key.split("_")[-1]
        session_data = user_sessions[key]
        sessions.append({
            "session_id": session_id,
            "stage": session_data.get("stage"),
            "tech_stack": session_data.get("tech_stack"),
            "question_count": session_data.get("question_count", 0),
            "difficulty": session_data.get("difficulty")
        })
    return {"user_id": user_id, "sessions": sessions}

if __name__ == "__main__":
    import uvicorn
    print("Starting Interview Prep FastAPI server...")
    print(f"Google API configured: {bool(GOOGLE_API_KEY)}")
    print(f"LeetCode problems loaded: {len(LEETCODE_DATA)}")
    print(f"Tavily search available: {bool(TAVILY_API_KEY)}")
    uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)