Spaces:
Build error
Build error
| """ | |
| 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 | |
| if not SHARED_STATE["valid_flag"]: | |
| st.error("You need to upload a Job Description & CV to use this feature.") | |
| else: | |
| 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)} | |
| ] | |
| 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") | |