CoolFace
Apppublic

MOSES3377/ai-interview-app

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
interview.py85 linesDownload Raw Back to root
1import os2import json3from openai import OpenAI4 5client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))6 7# Stable JSON extraction8def extract_first_json_block(text: str):9    for open_char, close_char in [("{", "}"), ("[", "]")]:10        stack = []11        start_idx = None12        for i, ch in enumerate(text):13            if ch == open_char:14                if not stack:15                    start_idx = i16                stack.append(ch)17            elif ch == close_char and stack:18                stack.pop()19                if not stack and start_idx is not None:20                    candidate = text[start_idx:i+1]21                    try:22                        return json.loads(candidate)23                    except Exception:24                        continue25    return None26 27# Generate interview questions28def generate_questions(resume_text, role, num_questions=5):29    prompt = (30        f"Generate {num_questions} interview questions for a {role} "31        f"based on this resume:\n{resume_text}\n"32        "Return a JSON list of questions with fields: text, topic, difficulty."33    )34    try:35        resp = client.chat.completions.create(36            model="gpt-4o-mini",37            messages=[{"role":"user","content":prompt}]38        )39        content = resp.choices[0].message.content40        questions = extract_first_json_block(content)41        return questions or []42    except Exception as e:43        return []44 45# Evaluate candidate answer46def evaluate_answer(question, answer, resume):47    prompt = (48        f"Given the resume:\n{resume}\n"49        f"Evaluate the answer to the question:\n{question}\n"50        f"Candidate answer:\n{answer}\n"51        "Return a JSON: {score (1-10), feedback, better_answer}"52    )53    try:54        resp = client.chat.completions.create(55            model="gpt-4o-mini",56            messages=[{"role":"user","content":prompt}]57        )58        eval_json = extract_first_json_block(resp.choices[0].message.content)59        if not isinstance(eval_json, dict):60            return {"score":0,"feedback":"Evaluation failed","better_answer":""}61        return eval_json62    except Exception:63        return {"score":0,"feedback":"Error during evaluation","better_answer":""}64 65# Summarize interview66def summarize_interview(questions, answers, resume):67    transcript_parts = []68    for q, a in zip(questions, answers):69        transcript_parts.append(f"Q: {q.get('text','')}\nA: {a.get('answer','')}\nScore: {a.get('score',0)}/10")70    transcript = "\n\n".join(transcript_parts)71    prompt = (72        f"Summarize the interview based on resume:\n{resume}\n"73        f"Transcript:\n{transcript}\n"74        "Return JSON: {overall_score, strengths (list), weaknesses (list), recommendation}"75    )76    try:77        resp = client.chat.completions.create(78            model="gpt-4o-mini",79            messages=[{"role":"user","content":prompt}]80        )81        summary = extract_first_json_block(resp.choices[0].message.content)82        return summary or {}83    except Exception:84        return {}85