File size: 2,559 Bytes
0c94c61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
    Simulates an interview, using uploaded CV and Job Description
"""

import random
import streamlit as st
from httpx import LocalProtocolError

from cohere.core.api_error import ApiError


from utils.gpt import stream


def InterviewPage():
    """Source Code for the Interview Simulation Page"""

    initial_questions = [
        "Ready for me to grill you?",
        "Please let me know when you're ready to begin the interview",
        "Ready to rumble?",
    ]

    # the initial message will be a random choice, initiating the conversation
    if "messages" not in st.session_state:
        st.session_state["messages"] = [
            {"role": "assistant", "message": random.choice(initial_questions)}
        ]

    MESSAGES = st.session_state.messages
    SHARED_STATE = st.session_state.shared_materials
    API_KEY = st.session_state.api_key

    clear_conversation = st.button("Clear Conversation")

    # Clear conversation will clear message state, and initialize with a new random question
    if clear_conversation:
        st.session_state["messages"] = [
            {"role": "assistant", "message": random.choice(initial_questions)}
        ]

    if not SHARED_STATE["valid_flag"]:
        st.error("You need to upload a Job Description & CV to use this feature.")
    else:
        try:
            # Populate the chat with historic messages
            for msg in MESSAGES:
                st.chat_message(msg["role"]).write(msg["message"])

            if user_input := st.chat_input():

                # Write the user question to UI
                st.chat_message("user").write(user_input)

                assistant_message = st.chat_message("assistant")

                # Stream assistant message, using relevant background information
                response = assistant_message.write_stream(
                    stream(
                        background_info={
                            "cv": SHARED_STATE["cv"],
                            "job_posting": SHARED_STATE["job_posting"],
                        },
                        chat_history=MESSAGES,
                        api_key=API_KEY,
                    )
                )

                # Append messages to chat history
                MESSAGES.append({"role": "user", "message": user_input})
                MESSAGES.append({"role": "assistant", "message": response})
        except LocalProtocolError:
            st.error("You need to enter a Cohere API Key.")
        except ApiError:
            st.error("You need a valid Cohere API Key")