Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,7 +28,6 @@ model = SentenceTransformer("all-MiniLM-L6-v2").to(device)
|
|
| 28 |
with open("technical_interviewer_prompt.txt", "r") as file:
|
| 29 |
technical_interviewer_prompt = file.read()
|
| 30 |
|
| 31 |
-
# Load prompts from files
|
| 32 |
with open("question_generation_prompt.txt", "r") as file:
|
| 33 |
question_generation_prompt = file.read()
|
| 34 |
|
|
@@ -39,61 +38,44 @@ if "messages" not in st.session_state:
|
|
| 39 |
st.session_state.messages = []
|
| 40 |
|
| 41 |
if "follow_up_mode" not in st.session_state:
|
| 42 |
-
st.session_state.follow_up_mode = False
|
| 43 |
|
| 44 |
if "generated_question" not in st.session_state:
|
| 45 |
-
st.session_state.generated_question = None
|
| 46 |
|
| 47 |
if "debug_logs" not in st.session_state:
|
| 48 |
-
st.session_state.debug_logs = []
|
| 49 |
|
| 50 |
# Function to find the top 1 most similar question based on user input
|
| 51 |
def find_top_question(query):
|
| 52 |
-
# Generate embedding for the query
|
| 53 |
query_embedding = model.encode(query, convert_to_tensor=True, device=device).cpu().numpy()
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Compute cosine similarity between query embedding and dataset embeddings
|
| 59 |
-
similarities = cosine_similarity(query_embedding, embeddings).flatten() # Flatten to get a 1D array of similarities
|
| 60 |
-
|
| 61 |
-
# Get the index of the most similar result (top 1)
|
| 62 |
-
top_index = similarities.argsort()[-1] # Index of highest similarity
|
| 63 |
-
|
| 64 |
-
# Retrieve metadata for the top result
|
| 65 |
top_result = metadata.iloc[top_index].copy()
|
| 66 |
top_result['similarity_score'] = similarities[top_index]
|
| 67 |
-
|
| 68 |
return top_result
|
| 69 |
|
| 70 |
-
# Function to generate response using OpenAI API
|
| 71 |
def generate_response(messages):
|
| 72 |
-
debug_log_entry = {"messages": messages}
|
| 73 |
-
st.session_state.debug_logs.append(debug_log_entry) # Store debug log
|
| 74 |
-
|
| 75 |
response = client.chat.completions.create(
|
| 76 |
model="o1-mini",
|
| 77 |
messages=messages,
|
| 78 |
)
|
| 79 |
-
|
| 80 |
return response.choices[0].message.content
|
| 81 |
|
| 82 |
# User input form for generating a new question
|
| 83 |
with st.form(key="input_form"):
|
| 84 |
-
company = st.text_input("Company", value="Google")
|
| 85 |
-
difficulty = st.selectbox("Difficulty", ["Easy", "Medium", "Hard"], index=1)
|
| 86 |
-
topic = st.text_input("Topic (e.g., Backtracking)", value="Backtracking")
|
| 87 |
-
|
| 88 |
generate_button = st.form_submit_button(label="Generate")
|
| 89 |
|
| 90 |
if generate_button:
|
| 91 |
-
# Clear session state and
|
| 92 |
st.session_state.messages = []
|
| 93 |
st.session_state.follow_up_mode = False
|
| 94 |
|
| 95 |
-
st.session_state.messages.append({"role": "user", "content": question_generation_prompt})
|
| 96 |
-
|
| 97 |
# Create a query from user inputs and find the most relevant question
|
| 98 |
query = f"{company} {difficulty} {topic}"
|
| 99 |
top_question = find_top_question(query)
|
|
@@ -109,61 +91,55 @@ if generate_button:
|
|
| 109 |
f"\nPlease create a real-world interview question based on this information."
|
| 110 |
)
|
| 111 |
|
| 112 |
-
# Generate response using GPT-4
|
| 113 |
-
response = generate_response([{"role": "user", "content": detailed_prompt}])
|
| 114 |
-
|
| 115 |
-
# Store generated question
|
| 116 |
st.session_state.generated_question = response
|
| 117 |
-
|
| 118 |
-
# Add the generated question to the conversation history as an assistant message (but omit the prompt)
|
| 119 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 120 |
|
| 121 |
-
# Enable follow-up mode
|
| 122 |
st.session_state.follow_up_mode = True
|
| 123 |
|
| 124 |
-
# Display
|
| 125 |
for message in st.session_state.messages:
|
| 126 |
with st.chat_message(message["role"]):
|
| 127 |
st.markdown(message["content"])
|
| 128 |
|
| 129 |
-
# Chatbox for subsequent conversations with assistant (follow-up mode)
|
| 130 |
if st.session_state.follow_up_mode:
|
| 131 |
if user_input := st.chat_input("Continue your conversation or ask follow-up questions here:"):
|
| 132 |
-
# Display user message in chat message container and add to session history
|
| 133 |
with st.chat_message("user"):
|
| 134 |
st.markdown(user_input)
|
| 135 |
|
| 136 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 137 |
|
| 138 |
-
# Generate
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
[{"role": "user", "content": technical_interviewer_prompt}] + st.session_state.messages
|
| 142 |
)
|
| 143 |
-
|
| 144 |
with st.chat_message("assistant"):
|
| 145 |
-
st.markdown(
|
| 146 |
|
| 147 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
| 148 |
|
| 149 |
-
|
| 150 |
-
## About
|
| 151 |
-
This is a Real-World Interview Question Generator powered by AI.
|
| 152 |
-
Enter a company name, topic, and level of difficulty, and it will transform a relevant question into a real-world interview scenario!
|
| 153 |
-
Continue chatting with the AI interviewer in the chatbox.
|
| 154 |
-
""")
|
| 155 |
-
|
| 156 |
-
# Sidebar content to display persistent generated question (left sidebar)
|
| 157 |
st.sidebar.markdown("## Generated Question")
|
| 158 |
if st.session_state.generated_question:
|
| 159 |
st.sidebar.markdown(st.session_state.generated_question)
|
| 160 |
else:
|
| 161 |
st.sidebar.markdown("_No question generated yet._")
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
with st.expander("Debug Logs (Toggle On/Off)", expanded=False):
|
| 165 |
if len(st.session_state.debug_logs) > 0:
|
| 166 |
-
for log_entry in reversed(st.session_state.debug_logs):
|
| 167 |
st.write(log_entry)
|
| 168 |
|
| 169 |
st.sidebar.markdown("---")
|
|
@@ -172,13 +148,12 @@ code_input = st.sidebar.text_area("Write your Python code here:")
|
|
| 172 |
if st.sidebar.button("Run Code"):
|
| 173 |
try:
|
| 174 |
exec_globals = {}
|
| 175 |
-
exec(code_input, exec_globals)
|
| 176 |
output_key = [k for k in exec_globals.keys() if k != "__builtins__"]
|
| 177 |
if output_key:
|
| 178 |
output_value = exec_globals[output_key[0]]
|
| 179 |
st.sidebar.success(f"Output: {output_value}")
|
| 180 |
else:
|
| 181 |
st.sidebar.success("Code executed successfully!")
|
| 182 |
-
|
| 183 |
except Exception as e:
|
| 184 |
-
st.sidebar.error(f"Error: {e}")
|
|
|
|
| 28 |
with open("technical_interviewer_prompt.txt", "r") as file:
|
| 29 |
technical_interviewer_prompt = file.read()
|
| 30 |
|
|
|
|
| 31 |
with open("question_generation_prompt.txt", "r") as file:
|
| 32 |
question_generation_prompt = file.read()
|
| 33 |
|
|
|
|
| 38 |
st.session_state.messages = []
|
| 39 |
|
| 40 |
if "follow_up_mode" not in st.session_state:
|
| 41 |
+
st.session_state.follow_up_mode = False
|
| 42 |
|
| 43 |
if "generated_question" not in st.session_state:
|
| 44 |
+
st.session_state.generated_question = None
|
| 45 |
|
| 46 |
if "debug_logs" not in st.session_state:
|
| 47 |
+
st.session_state.debug_logs = []
|
| 48 |
|
| 49 |
# Function to find the top 1 most similar question based on user input
|
| 50 |
def find_top_question(query):
|
|
|
|
| 51 |
query_embedding = model.encode(query, convert_to_tensor=True, device=device).cpu().numpy()
|
| 52 |
+
query_embedding = query_embedding.reshape(1, -1)
|
| 53 |
+
similarities = cosine_similarity(query_embedding, embeddings).flatten()
|
| 54 |
+
top_index = similarities.argsort()[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
top_result = metadata.iloc[top_index].copy()
|
| 56 |
top_result['similarity_score'] = similarities[top_index]
|
|
|
|
| 57 |
return top_result
|
| 58 |
|
| 59 |
+
# Function to generate response using OpenAI API
|
| 60 |
def generate_response(messages):
|
|
|
|
|
|
|
|
|
|
| 61 |
response = client.chat.completions.create(
|
| 62 |
model="o1-mini",
|
| 63 |
messages=messages,
|
| 64 |
)
|
|
|
|
| 65 |
return response.choices[0].message.content
|
| 66 |
|
| 67 |
# User input form for generating a new question
|
| 68 |
with st.form(key="input_form"):
|
| 69 |
+
company = st.text_input("Company", value="Google")
|
| 70 |
+
difficulty = st.selectbox("Difficulty", ["Easy", "Medium", "Hard"], index=1)
|
| 71 |
+
topic = st.text_input("Topic (e.g., Backtracking)", value="Backtracking")
|
|
|
|
| 72 |
generate_button = st.form_submit_button(label="Generate")
|
| 73 |
|
| 74 |
if generate_button:
|
| 75 |
+
# Clear session state and reset follow-up mode
|
| 76 |
st.session_state.messages = []
|
| 77 |
st.session_state.follow_up_mode = False
|
| 78 |
|
|
|
|
|
|
|
| 79 |
# Create a query from user inputs and find the most relevant question
|
| 80 |
query = f"{company} {difficulty} {topic}"
|
| 81 |
top_question = find_top_question(query)
|
|
|
|
| 91 |
f"\nPlease create a real-world interview question based on this information."
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# Generate response using GPT-4
|
| 95 |
+
response = generate_response([{"role": "user", "content": detailed_prompt}])
|
| 96 |
+
|
| 97 |
+
# Store the generated question for display but do not add the prompt to history
|
| 98 |
st.session_state.generated_question = response
|
|
|
|
|
|
|
| 99 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 100 |
|
| 101 |
+
# Enable follow-up mode
|
| 102 |
st.session_state.follow_up_mode = True
|
| 103 |
|
| 104 |
+
# Display the generated question and follow-up chat
|
| 105 |
for message in st.session_state.messages:
|
| 106 |
with st.chat_message(message["role"]):
|
| 107 |
st.markdown(message["content"])
|
| 108 |
|
|
|
|
| 109 |
if st.session_state.follow_up_mode:
|
| 110 |
if user_input := st.chat_input("Continue your conversation or ask follow-up questions here:"):
|
|
|
|
| 111 |
with st.chat_message("user"):
|
| 112 |
st.markdown(user_input)
|
| 113 |
|
| 114 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 115 |
|
| 116 |
+
# Generate follow-up response using the interviewer prompt but exclude it from history
|
| 117 |
+
follow_up_response = generate_response(
|
| 118 |
+
[{"role": "user", "content": user_input}]
|
|
|
|
| 119 |
)
|
| 120 |
+
|
| 121 |
with st.chat_message("assistant"):
|
| 122 |
+
st.markdown(follow_up_response)
|
| 123 |
|
| 124 |
+
st.session_state.messages.append({"role": "assistant", "content": follow_up_response})
|
| 125 |
|
| 126 |
+
# Sidebar content to display the generated question
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
st.sidebar.markdown("## Generated Question")
|
| 128 |
if st.session_state.generated_question:
|
| 129 |
st.sidebar.markdown(st.session_state.generated_question)
|
| 130 |
else:
|
| 131 |
st.sidebar.markdown("_No question generated yet._")
|
| 132 |
|
| 133 |
+
st.sidebar.markdown("""
|
| 134 |
+
## About
|
| 135 |
+
This is a Real-World Interview Question Generator powered by AI.
|
| 136 |
+
Enter a company name, topic, and level of difficulty, and it will transform a relevant question into a real-world interview scenario.
|
| 137 |
+
""")
|
| 138 |
+
|
| 139 |
+
# Debug logs and code interpreter section
|
| 140 |
with st.expander("Debug Logs (Toggle On/Off)", expanded=False):
|
| 141 |
if len(st.session_state.debug_logs) > 0:
|
| 142 |
+
for log_entry in reversed(st.session_state.debug_logs):
|
| 143 |
st.write(log_entry)
|
| 144 |
|
| 145 |
st.sidebar.markdown("---")
|
|
|
|
| 148 |
if st.sidebar.button("Run Code"):
|
| 149 |
try:
|
| 150 |
exec_globals = {}
|
| 151 |
+
exec(code_input, exec_globals)
|
| 152 |
output_key = [k for k in exec_globals.keys() if k != "__builtins__"]
|
| 153 |
if output_key:
|
| 154 |
output_value = exec_globals[output_key[0]]
|
| 155 |
st.sidebar.success(f"Output: {output_value}")
|
| 156 |
else:
|
| 157 |
st.sidebar.success("Code executed successfully!")
|
|
|
|
| 158 |
except Exception as e:
|
| 159 |
+
st.sidebar.error(f"Error: {e}")
|