Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,6 +28,7 @@ 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 |
with open("question_generation_prompt.txt", "r") as file:
|
| 32 |
question_generation_prompt = file.read()
|
| 33 |
|
|
@@ -38,41 +39,56 @@ if "messages" not in st.session_state:
|
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 76 |
st.session_state.messages = []
|
| 77 |
st.session_state.follow_up_mode = False
|
| 78 |
|
|
@@ -91,55 +107,61 @@ if generate_button:
|
|
| 91 |
f"\nPlease create a real-world interview question based on this information."
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# Generate response using GPT
|
| 95 |
response = generate_response([{"role": "user", "content": detailed_prompt}])
|
| 96 |
-
|
| 97 |
-
# Store
|
| 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
|
| 105 |
for message in st.session_state.messages:
|
| 106 |
-
|
| 107 |
-
st.
|
|
|
|
| 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
|
| 117 |
-
|
| 118 |
[{"role": "user", "content": user_input}]
|
| 119 |
)
|
| 120 |
-
|
| 121 |
with st.chat_message("assistant"):
|
| 122 |
-
st.markdown(
|
| 123 |
|
| 124 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
# Sidebar content to display
|
| 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 |
-
|
| 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,12 +170,13 @@ code_input = st.sidebar.text_area("Write your Python code here:")
|
|
| 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}")
|
|
|
|
| 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 |
st.session_state.messages = []
|
| 40 |
|
| 41 |
if "follow_up_mode" not in st.session_state:
|
| 42 |
+
st.session_state.follow_up_mode = False # Tracks whether we're in follow-up mode
|
| 43 |
|
| 44 |
if "generated_question" not in st.session_state:
|
| 45 |
+
st.session_state.generated_question = None # Stores the generated question for persistence
|
| 46 |
|
| 47 |
if "debug_logs" not in st.session_state:
|
| 48 |
+
st.session_state.debug_logs = [] # Stores debug logs for toggling
|
| 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 |
+
# Reshape query_embedding to ensure it is a 2D array
|
| 56 |
+
query_embedding = query_embedding.reshape(1, -1) # Reshape to (1, n_features)
|
| 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 with debugging logs
|
| 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") # Default value: Google
|
| 85 |
+
difficulty = st.selectbox("Difficulty", ["Easy", "Medium", "Hard"], index=1) # Default: Medium
|
| 86 |
+
topic = st.text_input("Topic (e.g., Backtracking)", value="Backtracking") # Default: Backtracking
|
| 87 |
+
|
| 88 |
generate_button = st.form_submit_button(label="Generate")
|
| 89 |
|
| 90 |
if generate_button:
|
| 91 |
+
# Clear session state and start fresh with follow-up mode disabled
|
| 92 |
st.session_state.messages = []
|
| 93 |
st.session_state.follow_up_mode = False
|
| 94 |
|
|
|
|
| 107 |
f"\nPlease create a real-world interview question based on this information."
|
| 108 |
)
|
| 109 |
|
| 110 |
+
# Generate response using GPT with detailed prompt (question generation prompt excluded here)
|
| 111 |
response = generate_response([{"role": "user", "content": detailed_prompt}])
|
| 112 |
+
|
| 113 |
+
# Store generated question in session state for persistence in sidebar and follow-up conversation state
|
| 114 |
st.session_state.generated_question = response
|
| 115 |
+
|
| 116 |
+
# Add the generated question to the conversation history as an assistant message (omit prompts in the chat)
|
| 117 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 118 |
|
| 119 |
+
# Enable follow-up mode after generating the initial question
|
| 120 |
st.session_state.follow_up_mode = True
|
| 121 |
|
| 122 |
+
# Display chat messages from history on app rerun (for subsequent conversation)
|
| 123 |
for message in st.session_state.messages:
|
| 124 |
+
if not message["content"].startswith("Please create a real-world interview question"): # Exclude prompts
|
| 125 |
+
with st.chat_message(message["role"]):
|
| 126 |
+
st.markdown(message["content"])
|
| 127 |
|
| 128 |
+
# Chatbox for subsequent conversations with assistant (follow-up mode)
|
| 129 |
if st.session_state.follow_up_mode:
|
| 130 |
if user_input := st.chat_input("Continue your conversation or ask follow-up questions here:"):
|
| 131 |
+
# Display user message in chat message container and add to session history
|
| 132 |
with st.chat_message("user"):
|
| 133 |
st.markdown(user_input)
|
| 134 |
|
| 135 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 136 |
|
| 137 |
+
# Generate assistant's response based on follow-up input using technical_interviewer_prompt as system prompt
|
| 138 |
+
assistant_response = generate_response(
|
| 139 |
[{"role": "user", "content": user_input}]
|
| 140 |
)
|
| 141 |
+
|
| 142 |
with st.chat_message("assistant"):
|
| 143 |
+
st.markdown(assistant_response)
|
| 144 |
|
| 145 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 146 |
+
|
| 147 |
+
st.sidebar.markdown("""
|
| 148 |
+
## About
|
| 149 |
+
This is a Real-World Interview Question Generator powered by AI.
|
| 150 |
+
Enter a company name, topic, and level of difficulty, and it will transform a relevant question into a real-world interview scenario!
|
| 151 |
+
Continue chatting with the AI interviewer in the chatbox.
|
| 152 |
+
""")
|
| 153 |
|
| 154 |
+
# Sidebar content to display persistent generated question (left sidebar)
|
| 155 |
st.sidebar.markdown("## Generated Question")
|
| 156 |
if st.session_state.generated_question:
|
| 157 |
st.sidebar.markdown(st.session_state.generated_question)
|
| 158 |
else:
|
| 159 |
st.sidebar.markdown("_No question generated yet._")
|
| 160 |
|
| 161 |
+
# Right sidebar toggleable debug logs and code interpreter section
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
with st.expander("Debug Logs (Toggle On/Off)", expanded=False):
|
| 163 |
if len(st.session_state.debug_logs) > 0:
|
| 164 |
+
for log_entry in reversed(st.session_state.debug_logs): # Show most recent logs first
|
| 165 |
st.write(log_entry)
|
| 166 |
|
| 167 |
st.sidebar.markdown("---")
|
|
|
|
| 170 |
if st.sidebar.button("Run Code"):
|
| 171 |
try:
|
| 172 |
exec_globals = {}
|
| 173 |
+
exec(code_input, exec_globals) # Execute user-provided code safely within its own scope.
|
| 174 |
output_key = [k for k in exec_globals.keys() if k != "__builtins__"]
|
| 175 |
if output_key:
|
| 176 |
output_value = exec_globals[output_key[0]]
|
| 177 |
st.sidebar.success(f"Output: {output_value}")
|
| 178 |
else:
|
| 179 |
st.sidebar.success("Code executed successfully!")
|
| 180 |
+
|
| 181 |
except Exception as e:
|
| 182 |
st.sidebar.error(f"Error: {e}")
|