Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import os
|
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
-
from sklearn.metrics.
|
8 |
import torch
|
9 |
|
10 |
# Set up OpenAI client
|
@@ -15,8 +15,8 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
15 |
print(f"Using device: {device}")
|
16 |
|
17 |
# Load metadata and embeddings (ensure these files are in your working directory or update paths)
|
18 |
-
metadata_path = '
|
19 |
-
embeddings_path = '
|
20 |
|
21 |
metadata = pd.read_csv(metadata_path)
|
22 |
embeddings = np.load(embeddings_path)
|
@@ -45,9 +45,7 @@ def find_top_question(query):
|
|
45 |
query_embedding = model.encode(query, convert_to_tensor=True, device=device).cpu().numpy()
|
46 |
|
47 |
# Compute cosine similarity between query embedding and dataset embeddings using scikit-learn's pairwise_distances_reduction
|
48 |
-
similarities =
|
49 |
-
X=query_embedding.reshape(1, -1), Y=embeddings, reduce_func="argmax"
|
50 |
-
)
|
51 |
|
52 |
# Get the index of the most similar result (top 1)
|
53 |
top_index = similarities.indices[0] # Index of highest similarity
|
@@ -64,8 +62,8 @@ def generate_response(prompt):
|
|
64 |
st.write(prompt) # Log the prompt being sent to GPT for debugging
|
65 |
|
66 |
response = client.chat.completions.create(
|
67 |
-
model="
|
68 |
-
messages=st.session_state.messages + [{"role": "
|
69 |
)
|
70 |
return response.choices[0].message.content
|
71 |
|
@@ -103,6 +101,6 @@ if prompt := st.chat_input("Enter a LeetCode-related query (e.g., 'google backtr
|
|
103 |
|
104 |
st.sidebar.markdown("""
|
105 |
## About
|
106 |
-
This is a LeetCode to Real-World Interview Question Generator powered by OpenAI's
|
107 |
Enter a LeetCode-related query, and it will transform a relevant question into a real-world interview scenario!
|
108 |
""")
|
|
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
8 |
import torch
|
9 |
|
10 |
# Set up OpenAI client
|
|
|
15 |
print(f"Using device: {device}")
|
16 |
|
17 |
# Load metadata and embeddings (ensure these files are in your working directory or update paths)
|
18 |
+
metadata_path = 'question_metadata.csv' # Update this path if needed
|
19 |
+
embeddings_path = 'question_dataset_embeddings.npy' # Update this path if needed
|
20 |
|
21 |
metadata = pd.read_csv(metadata_path)
|
22 |
embeddings = np.load(embeddings_path)
|
|
|
45 |
query_embedding = model.encode(query, convert_to_tensor=True, device=device).cpu().numpy()
|
46 |
|
47 |
# Compute cosine similarity between query embedding and dataset embeddings using scikit-learn's pairwise_distances_reduction
|
48 |
+
similarities = cosine_similarity(query_embedding, embeddings).flatten()
|
|
|
|
|
49 |
|
50 |
# Get the index of the most similar result (top 1)
|
51 |
top_index = similarities.indices[0] # Index of highest similarity
|
|
|
62 |
st.write(prompt) # Log the prompt being sent to GPT for debugging
|
63 |
|
64 |
response = client.chat.completions.create(
|
65 |
+
model="o1-mini",
|
66 |
+
messages=st.session_state.messages + [{"role": "assistant", "content": prompt}]
|
67 |
)
|
68 |
return response.choices[0].message.content
|
69 |
|
|
|
101 |
|
102 |
st.sidebar.markdown("""
|
103 |
## About
|
104 |
+
This is a LeetCode to Real-World Interview Question Generator powered by OpenAI's API.
|
105 |
Enter a LeetCode-related query, and it will transform a relevant question into a real-world interview scenario!
|
106 |
""")
|