Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,35 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Set up OpenAI client
|
| 6 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Load the system prompt from the file
|
| 11 |
with open("prompt.txt", "r") as file:
|
| 12 |
system_prompt = file.read()
|
| 13 |
|
|
|
|
|
|
|
| 14 |
# Initialize chat history
|
| 15 |
if "messages" not in st.session_state:
|
| 16 |
st.session_state.messages = [{"role": "assistant", "content": system_prompt}]
|
|
@@ -20,8 +39,30 @@ for message in st.session_state.messages[1:]: # Skip the system message
|
|
| 20 |
with st.chat_message(message["role"]):
|
| 21 |
st.markdown(message["content"])
|
| 22 |
|
| 23 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def generate_response(prompt):
|
|
|
|
|
|
|
|
|
|
| 25 |
response = client.chat.completions.create(
|
| 26 |
model="gpt-4o",
|
| 27 |
messages=st.session_state.messages + [{"role": "system", "content": prompt}]
|
|
@@ -29,23 +70,39 @@ def generate_response(prompt):
|
|
| 29 |
return response.choices[0].message.content
|
| 30 |
|
| 31 |
# React to user input
|
| 32 |
-
if prompt := st.chat_input("Enter a LeetCode
|
| 33 |
# Display user message in chat message container
|
| 34 |
st.chat_message("user").markdown(prompt)
|
| 35 |
# Add user message to chat history
|
| 36 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Display assistant response in chat message container
|
| 42 |
with st.chat_message("assistant"):
|
| 43 |
st.markdown(response)
|
|
|
|
| 44 |
# Add assistant response to chat history
|
| 45 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 46 |
|
| 47 |
st.sidebar.markdown("""
|
| 48 |
## About
|
| 49 |
This is a LeetCode to Real-World Interview Question Generator powered by OpenAI's GPT-4.
|
| 50 |
-
Enter a LeetCode
|
| 51 |
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from sklearn.metrics.pairwise_distances_reduction import cosine_similarity_reduction
|
| 8 |
+
import torch
|
| 9 |
|
| 10 |
# Set up OpenAI client
|
| 11 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 12 |
|
| 13 |
+
# Check if GPU is available
|
| 14 |
+
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 = '/kaggle/working/leetcode_metadata.csv' # Update this path if needed
|
| 19 |
+
embeddings_path = '/kaggle/working/leetcode_embeddings2.npy' # Update this path if needed
|
| 20 |
+
|
| 21 |
+
metadata = pd.read_csv(metadata_path)
|
| 22 |
+
embeddings = np.load(embeddings_path)
|
| 23 |
+
|
| 24 |
+
# Load the SentenceTransformer model
|
| 25 |
+
model = SentenceTransformer("all-MiniLM-L6-v2").to(device)
|
| 26 |
|
| 27 |
# Load the system prompt from the file
|
| 28 |
with open("prompt.txt", "r") as file:
|
| 29 |
system_prompt = file.read()
|
| 30 |
|
| 31 |
+
st.title("LeetCode to Real-World Interview Question Generator")
|
| 32 |
+
|
| 33 |
# Initialize chat history
|
| 34 |
if "messages" not in st.session_state:
|
| 35 |
st.session_state.messages = [{"role": "assistant", "content": system_prompt}]
|
|
|
|
| 39 |
with st.chat_message(message["role"]):
|
| 40 |
st.markdown(message["content"])
|
| 41 |
|
| 42 |
+
# Function to find the top 1 most similar question based on user input
|
| 43 |
+
def find_top_question(query):
|
| 44 |
+
# Generate embedding for the 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 = cosine_similarity_reduction(
|
| 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
|
| 54 |
+
|
| 55 |
+
# Retrieve metadata for the top result
|
| 56 |
+
top_result = metadata.iloc[top_index].copy()
|
| 57 |
+
top_result['similarity_score'] = similarities.distances[0]
|
| 58 |
+
|
| 59 |
+
return top_result
|
| 60 |
+
|
| 61 |
+
# Function to generate response using OpenAI API with debugging logs
|
| 62 |
def generate_response(prompt):
|
| 63 |
+
st.write("### Debugging Log: Data Sent to GPT")
|
| 64 |
+
st.write(prompt) # Log the prompt being sent to GPT for debugging
|
| 65 |
+
|
| 66 |
response = client.chat.completions.create(
|
| 67 |
model="gpt-4o",
|
| 68 |
messages=st.session_state.messages + [{"role": "system", "content": prompt}]
|
|
|
|
| 70 |
return response.choices[0].message.content
|
| 71 |
|
| 72 |
# React to user input
|
| 73 |
+
if prompt := st.chat_input("Enter a LeetCode-related query (e.g., 'google backtracking'):"):
|
| 74 |
# Display user message in chat message container
|
| 75 |
st.chat_message("user").markdown(prompt)
|
| 76 |
# Add user message to chat history
|
| 77 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 78 |
|
| 79 |
+
# Find the top question based on user input
|
| 80 |
+
top_question = find_top_question(prompt)
|
| 81 |
+
|
| 82 |
+
# Prepare a detailed prompt for GPT using the top question's details
|
| 83 |
+
detailed_prompt = (
|
| 84 |
+
f"Transform this LeetCode question into a real-world interview scenario:\n\n"
|
| 85 |
+
f"**Company**: {top_question['company']}\n"
|
| 86 |
+
f"**Question ID**: {top_question['questionId']}\n"
|
| 87 |
+
f"**Question Name**: {top_question['questionName']}\n"
|
| 88 |
+
f"**Difficulty Level**: {top_question['difficulty level']}\n"
|
| 89 |
+
f"**Tags**: {top_question['Tags']}\n"
|
| 90 |
+
f"**Content**: {top_question['Content']}\n"
|
| 91 |
+
f"\nPlease create a real-world interview question based on this information."
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Generate response using GPT-4 with detailed prompt and debugging logs
|
| 95 |
+
response = generate_response(detailed_prompt)
|
| 96 |
|
| 97 |
# Display assistant response in chat message container
|
| 98 |
with st.chat_message("assistant"):
|
| 99 |
st.markdown(response)
|
| 100 |
+
|
| 101 |
# Add assistant response to chat history
|
| 102 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 103 |
|
| 104 |
st.sidebar.markdown("""
|
| 105 |
## About
|
| 106 |
This is a LeetCode to Real-World Interview Question Generator powered by OpenAI's GPT-4.
|
| 107 |
+
Enter a LeetCode-related query, and it will transform a relevant question into a real-world interview scenario!
|
| 108 |
""")
|