File size: 6,362 Bytes
685c012
e32a9cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ecd386
e32a9cc
6ecd386
685c012
e32a9cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ecd386
e32a9cc
6ecd386
e32a9cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ecd386
e32a9cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ecd386
 
e32a9cc
 
 
6ecd386
e32a9cc
 
6ecd386
e32a9cc
6ecd386
e32a9cc
 
 
6ecd386
e32a9cc
 
3e1dc3b
e32a9cc
 
 
3e1dc3b
e32a9cc
 
 
 
3e1dc3b
e32a9cc
 
 
3e1dc3b
e32a9cc
 
685c012
e32a9cc
685c012
e32a9cc
 
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
import streamlit as st
import requests
import os
from hashlib import sha256
import random

# ========== CONFIG ==========
GROQ_API_KEY = "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"  # Replace with your actual key
GROQ_MODEL = "llama3-8b-8192"       # Recommended current Groq model

# ========== STATE ==========
if "last_result" not in st.session_state:
    st.session_state.last_result = None
if "last_candidate" not in st.session_state:
    st.session_state.last_candidate = None

# ========== GROQ HELPERS ==========

def generate_questions(domain: str, round_type: str):
    prompt = f"""
    Generate 3 {round_type} interview questions for a candidate in the domain of {domain}.
    Questions should be clear, concise, and assess relevant skills.
    """
    headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
    data = {
        "model": GROQ_MODEL,
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.7,
        "max_tokens": 400,
    }

    response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)

    try:
        res_json = response.json()
        return [q.strip("- ").strip() for q in res_json['choices'][0]['message']['content'].split("\n") if q.strip()]
    except Exception as e:
        st.error(f"Groq API Error: {e}")
        st.json(response.json())
        return ["Question 1", "Question 2", "Question 3"]

def generate_programming_question(domain: str, language: str):
    prompt = f"""
    Generate 1 beginner-to-intermediate level programming interview question in {language} for a candidate applying in the domain of {domain}.
    Provide only the question without solution or explanation.
    """
    headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
    data = {
        "model": GROQ_MODEL,
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.7,
        "max_tokens": 300,
    }

    response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)

    try:
        return response.json()['choices'][0]['message']['content']
    except Exception as e:
        st.error(f"Groq API Error: {e}")
        st.json(response.json())
        return "Write a function to reverse a string."

# ========== CORE FUNCTIONALITY ==========

def check_resume_originality(uploaded_file):
    content = uploaded_file.read()
    resume_hash = sha256(content).hexdigest()
    existing_hashes = ["abc123", "def456"]  # Dummy hashes - replace with real hashes database
    return 20 if resume_hash in existing_hashes else 95

def save_to_crm(name, domain, result):
    # Placeholder for CRM integration - replace with real CRM API calls
    print(f"[CRM] Candidate: {name}, Domain: {domain}, Result: {result}")

def show_dashboard():
    st.title("πŸ“Š Dashboard")
    if st.session_state.last_result:
        st.subheader("Latest Candidate Summary")
        total_score = sum(st.session_state.last_result.values()) / len(st.session_state.last_result)
        st.metric("Overall Score", f"{total_score:.2f}%")
        st.progress(int(total_score))
        for k, v in st.session_state.last_result.items():
            st.write(f"**{k.replace('_', ' ').title()}**: {v:.2f}%")
        st.info(f"Last candidate: {st.session_state.last_candidate}")
    else:
        st.warning("No interview data available yet.")

def start_interview(domain, language):
    result = {}

    st.subheader("πŸ”’ Round 1: Aptitude")
    aptitude_qs = generate_questions(domain, "aptitude")
    aptitude_score = 0
    for i, q in enumerate(aptitude_qs):
        ans = st.text_input(f"Aptitude Q{i+1}: {q}", key=f"apt{i}")
        if ans:
            aptitude_score += 1
    result['aptitude_score'] = (aptitude_score / len(aptitude_qs)) * 100

    st.subheader(f"πŸ’» Round 2: Programming in {language}")
    prog_q = generate_programming_question(domain, language)
    st.markdown(f"**Problem:** {prog_q}")
    code_ans = st.text_area(f"Write your solution in {language}", key="code")
    result['code_score'] = 90 if ("def" in code_ans or "class" in code_ans) and len(code_ans) > 20 else 40

    st.subheader("πŸ’¬ Round 3: HR Interview")
    hr_qs = generate_questions(domain, "HR")
    hr_score = 0
    for i, q in enumerate(hr_qs):
        ans = st.text_area(f"HR Q{i+1}: {q}", key=f"hr{i}")
        hr_score += len(ans.split()) > 15
    result['hr_score'] = (hr_score / len(hr_qs)) * 100

    st.subheader("πŸ—£οΈ Round 4: Communication")
    result['communication_score'] = random.randint(70, 90)

    # --- Show final marks summary ---
    st.markdown("---")
    st.header("πŸ“‹ Interview Summary")
    rounds = {
        "Aptitude": result['aptitude_score'],
        "Programming": result['code_score'],
        "HR Interview": result['hr_score'],
        "Communication": result['communication_score'],
    }

    total = sum(rounds.values()) / len(rounds)
    for round_name, score in rounds.items():
        st.write(f"**{round_name}:** {score:.2f}%")

    st.write(f"### Overall Score: {total:.2f}%")
    st.progress(int(total))

    return result

# ========== MAIN APP ==========
st.set_page_config(page_title="AI Interview System", layout="centered")
page = st.sidebar.radio("πŸ“Œ Navigate", ["Interview", "Dashboard"])

if page == "Interview":
    st.title("🧠 AI Interview System")

    uploaded_resume = st.file_uploader("πŸ“„ Upload Resume (PDF/DOCX)", type=['pdf', 'docx'])
    domain = st.selectbox("🎯 Select your domain", ["Software", "Data Science", "Networking", "AI/ML"])
    language = st.selectbox("πŸ’» Select programming language", ["Python", "Java", "C++", "JavaScript"])

    if uploaded_resume and domain and language:
        with st.expander("πŸ” Step 1: Resume Originality Check"):
            score = check_resume_originality(uploaded_resume)
            st.info(f"Resume Originality Score: **{score}%**")

        if st.button("πŸš€ Start Interview"):
            result = start_interview(domain, language)
            st.success("βœ… Interview Completed!")

            st.session_state.last_result = result
            st.session_state.last_candidate = uploaded_resume.name

            save_to_crm(uploaded_resume.name, domain, result)

elif page == "Dashboard":
    show_dashboard()