Spaces:
Sleeping
Sleeping
voice + code evaluation
Browse files
app.py
CHANGED
@@ -11,16 +11,13 @@ import requests
|
|
11 |
# Set up OpenAI client
|
12 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
13 |
|
14 |
-
# Set up ElevenLabs API key
|
15 |
-
ELEVENLABS_API_KEY = OpenAI(api_key=os.getenv("VOICE_API_KEY"))
|
16 |
-
|
17 |
# Check if GPU is available
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
print(f"Using device: {device}")
|
20 |
|
21 |
# Load metadata and embeddings (ensure these files are in your working directory or update paths)
|
22 |
-
metadata_path = 'question_metadata.csv'
|
23 |
-
embeddings_path = 'question_dataset_embeddings.npy'
|
24 |
|
25 |
metadata = pd.read_csv(metadata_path)
|
26 |
embeddings = np.load(embeddings_path)
|
@@ -41,68 +38,73 @@ st.title("Real-World Programming Question Mock Interview")
|
|
41 |
if "messages" not in st.session_state:
|
42 |
st.session_state.messages = []
|
43 |
|
44 |
-
if "follow_up_mode" not in st.session_state:
|
45 |
-
st.session_state.follow_up_mode = False # Tracks whether we're in follow-up mode
|
46 |
-
|
47 |
if "generated_question" not in st.session_state:
|
48 |
-
st.session_state.generated_question = None
|
49 |
-
|
50 |
-
if "debug_logs" not in st.session_state:
|
51 |
-
st.session_state.debug_logs = [] # Stores debug logs for toggling
|
52 |
|
53 |
if "code_output" not in st.session_state:
|
54 |
-
st.session_state.code_output =
|
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 |
-
"stability": 0.5
|
91 |
-
}
|
92 |
-
}
|
93 |
-
|
94 |
-
response = requests.post(url, headers=headers, json=payload)
|
95 |
-
|
96 |
-
if response.status_code == 200:
|
97 |
-
audio_file_path = f"assistant_response.mp3"
|
98 |
-
with open(audio_file_path, "wb") as audio_file:
|
99 |
-
audio_file.write(response.content)
|
100 |
-
return audio_file_path
|
101 |
else:
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
with st.form(key="input_form"):
|
107 |
company = st.text_input("Company", value="Google")
|
108 |
difficulty = st.selectbox("Difficulty", ["Easy", "Medium", "Hard"], index=1)
|
@@ -111,10 +113,17 @@ with st.form(key="input_form"):
|
|
111 |
generate_button = st.form_submit_button(label="Generate")
|
112 |
|
113 |
if generate_button:
|
114 |
-
st.session_state.messages = []
|
115 |
-
st.session_state.follow_up_mode = False
|
116 |
-
|
117 |
query = f"{company} {difficulty} {topic}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
top_question = find_top_question(query)
|
119 |
|
120 |
detailed_prompt = (
|
@@ -127,68 +136,29 @@ if generate_button:
|
|
127 |
f"\nPlease create a real-world interview question based on this information."
|
128 |
)
|
129 |
|
130 |
-
response_text =
|
|
|
|
|
|
|
131 |
|
132 |
st.session_state.generated_question = response_text
|
133 |
|
134 |
-
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
135 |
-
|
136 |
-
st.session_state.follow_up_mode = True
|
137 |
-
|
138 |
for message in st.session_state.messages:
|
139 |
with st.chat_message(message["role"]):
|
140 |
st.markdown(message["content"])
|
141 |
|
142 |
-
if st.
|
143 |
-
|
144 |
-
|
145 |
-
st.markdown(user_input)
|
146 |
-
|
147 |
-
st.session_state.messages.append({"role": "user", "content": user_input})
|
148 |
-
|
149 |
-
assistant_response_text = generate_response(
|
150 |
-
[{"role": "assistant", "content": technical_interviewer_prompt}] + st.session_state.messages
|
151 |
-
)
|
152 |
-
|
153 |
-
assistant_audio_path = generate_audio(assistant_response_text)
|
154 |
-
|
155 |
-
with st.chat_message("assistant"):
|
156 |
-
st.markdown(assistant_response_text)
|
157 |
-
if assistant_audio_path:
|
158 |
-
audio_bytes = open(assistant_audio_path, "rb").read()
|
159 |
-
st.audio(audio_bytes, format="audio/mp3")
|
160 |
-
|
161 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_response_text})
|
162 |
-
|
163 |
-
# Left Sidebar: Generated Question and Code Box
|
164 |
-
with st.sidebar:
|
165 |
-
# Top Half: Generated Question
|
166 |
-
st.markdown("## Generated Question")
|
167 |
-
if st.session_state.generated_question:
|
168 |
-
st.markdown(st.session_state.generated_question)
|
169 |
-
else:
|
170 |
-
st.markdown("_No question generated yet._")
|
171 |
-
|
172 |
-
# Divider between sections
|
173 |
-
st.markdown("---")
|
174 |
-
|
175 |
-
# Bottom Half: Python Code Box
|
176 |
-
st.markdown("## Python Code Interpreter")
|
177 |
|
178 |
-
|
|
|
|
|
|
|
179 |
|
180 |
-
|
|
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
exec_globals = {}
|
186 |
-
exec(code_input, exec_globals) # Execute user-provided code safely within its own scope.
|
187 |
-
output_key_values = {k: v for k, v in exec_globals.items() if k != "__builtins__"}
|
188 |
-
if output_key_values:
|
189 |
-
output_strs = [f"{key}: {value}" for key, value in output_key_values.items()]
|
190 |
-
output_display_strs = "\n".join(output_strs)
|
191 |
-
output_display_strs += "\nCode executed successfully!"
|
192 |
-
print(output_display_strs)
|
193 |
-
|
194 |
-
except Exception as e:
|
|
|
11 |
# Set up OpenAI client
|
12 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
13 |
|
|
|
|
|
|
|
14 |
# Check if GPU is available
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
print(f"Using device: {device}")
|
17 |
|
18 |
# Load metadata and embeddings (ensure these files are in your working directory or update paths)
|
19 |
+
metadata_path = 'question_metadata.csv'
|
20 |
+
embeddings_path = 'question_dataset_embeddings.npy'
|
21 |
|
22 |
metadata = pd.read_csv(metadata_path)
|
23 |
embeddings = np.load(embeddings_path)
|
|
|
38 |
if "messages" not in st.session_state:
|
39 |
st.session_state.messages = []
|
40 |
|
|
|
|
|
|
|
41 |
if "generated_question" not in st.session_state:
|
42 |
+
st.session_state.generated_question = None
|
|
|
|
|
|
|
43 |
|
44 |
if "code_output" not in st.session_state:
|
45 |
+
st.session_state.code_output = ""
|
46 |
+
|
47 |
+
if "evaluation_output" not in st.session_state:
|
48 |
+
st.session_state.evaluation_output = ""
|
49 |
+
|
50 |
+
# Sidebar layout for Generated Question and Code Box
|
51 |
+
st.sidebar.markdown("## Generated Question")
|
52 |
+
if st.session_state.generated_question:
|
53 |
+
st.sidebar.markdown(st.session_state.generated_question)
|
54 |
+
else:
|
55 |
+
st.sidebar.markdown("_No question generated yet._")
|
56 |
+
|
57 |
+
st.sidebar.markdown("---")
|
58 |
+
st.sidebar.markdown("## Code Box")
|
59 |
+
|
60 |
+
code_input = st.sidebar.text_area(
|
61 |
+
label="Write your Python code here:",
|
62 |
+
height=200,
|
63 |
+
placeholder="Enter your code...",
|
64 |
+
)
|
65 |
+
|
66 |
+
col1, col2 = st.sidebar.columns(2)
|
67 |
+
|
68 |
+
# Button to run code and display output
|
69 |
+
if col1.button("Run Code"):
|
70 |
+
try:
|
71 |
+
exec_globals = {}
|
72 |
+
exec(code_input, exec_globals)
|
73 |
+
st.session_state.code_output = exec_globals.get("output", "Code executed successfully.")
|
74 |
+
except Exception as e:
|
75 |
+
st.session_state.code_output = f"Error: {str(e)}"
|
76 |
+
|
77 |
+
# Button to evaluate code using OpenAI API
|
78 |
+
if col2.button("Evaluate Code"):
|
79 |
+
if not st.session_state.generated_question:
|
80 |
+
st.sidebar.error("Generate a question first!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
else:
|
82 |
+
try:
|
83 |
+
evaluation_prompt = (
|
84 |
+
f"Question: {st.session_state.generated_question}\n\n"
|
85 |
+
f"Code:\n{code_input}\n\n"
|
86 |
+
f"Evaluate this code's correctness, efficiency, and style."
|
87 |
+
)
|
88 |
+
response = client.chat.completions.create(
|
89 |
+
model="gpt-4",
|
90 |
+
messages=[{"role": "user", "content": evaluation_prompt}],
|
91 |
+
)
|
92 |
+
evaluation_response = response.choices[0].message.content
|
93 |
+
st.session_state.evaluation_output = evaluation_response
|
94 |
+
|
95 |
+
# Add evaluation output to follow-up conversation
|
96 |
+
st.session_state.messages.append({"role": "assistant", "content": evaluation_response})
|
97 |
+
except Exception as e:
|
98 |
+
st.sidebar.error(f"Error during evaluation: {str(e)}")
|
99 |
+
|
100 |
+
# Display outputs below the main app content
|
101 |
+
st.subheader("Code Output")
|
102 |
+
st.text(st.session_state.code_output)
|
103 |
+
|
104 |
+
st.subheader("Evaluation Output")
|
105 |
+
st.text(st.session_state.evaluation_output)
|
106 |
+
|
107 |
+
# Main app logic for generating questions and follow-up conversation remains unchanged.
|
108 |
with st.form(key="input_form"):
|
109 |
company = st.text_input("Company", value="Google")
|
110 |
difficulty = st.selectbox("Difficulty", ["Easy", "Medium", "Hard"], index=1)
|
|
|
113 |
generate_button = st.form_submit_button(label="Generate")
|
114 |
|
115 |
if generate_button:
|
|
|
|
|
|
|
116 |
query = f"{company} {difficulty} {topic}"
|
117 |
+
|
118 |
+
def find_top_question(query):
|
119 |
+
query_embedding = model.encode(query, convert_to_tensor=True, device=device).cpu().numpy()
|
120 |
+
query_embedding = query_embedding.reshape(1, -1)
|
121 |
+
similarities = cosine_similarity(query_embedding, embeddings).flatten()
|
122 |
+
top_index = similarities.argsort()[-1]
|
123 |
+
top_result = metadata.iloc[top_index].copy()
|
124 |
+
top_result['similarity_score'] = similarities[top_index]
|
125 |
+
return top_result
|
126 |
+
|
127 |
top_question = find_top_question(query)
|
128 |
|
129 |
detailed_prompt = (
|
|
|
136 |
f"\nPlease create a real-world interview question based on this information."
|
137 |
)
|
138 |
|
139 |
+
response_text = client.chat.completions.create(
|
140 |
+
model="gpt-4",
|
141 |
+
messages=[{"role": "assistant", "content": question_generation_prompt}, {"role": "user", "content": detailed_prompt}],
|
142 |
+
).choices[0].message.content
|
143 |
|
144 |
st.session_state.generated_question = response_text
|
145 |
|
|
|
|
|
|
|
|
|
146 |
for message in st.session_state.messages:
|
147 |
with st.chat_message(message["role"]):
|
148 |
st.markdown(message["content"])
|
149 |
|
150 |
+
if user_input := st.chat_input("Continue your conversation or ask follow-up questions here:"):
|
151 |
+
with st.chat_message("user"):
|
152 |
+
st.markdown(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
+
assistant_response_text = client.chat.completions.create(
|
155 |
+
model="gpt-4",
|
156 |
+
messages=[{"role": "assistant", "content": technical_interviewer_prompt}] + [{"role": msg["role"], "content": msg["content"]} for msg in st.session_state.messages],
|
157 |
+
).choices[0].message.content
|
158 |
|
159 |
+
with st.chat_message("assistant"):
|
160 |
+
st.markdown(assistant_response_text)
|
161 |
|
162 |
+
# Append to session state messages for persistence
|
163 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
164 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response_text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|