Spaces:
Sleeping
Sleeping
Uldeon Intelligent Software commited on
Upload 2 files
Browse files- app.py +41 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pickle
|
| 4 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
|
| 7 |
+
# Must match the class used in Colab for unpickling
|
| 8 |
+
class CodePaulLite:
|
| 9 |
+
def __init__(self, dataset):
|
| 10 |
+
self.dataset = dataset
|
| 11 |
+
self.keys = list(dataset.keys())
|
| 12 |
+
self.vectorizer = TfidfVectorizer(stop_words='english')
|
| 13 |
+
self.matrix = self.vectorizer.fit_transform(self.keys)
|
| 14 |
+
|
| 15 |
+
def ask(self, query, threshold=0.1):
|
| 16 |
+
query_vec = self.vectorizer.transform([query])
|
| 17 |
+
similarities = cosine_similarity(query_vec, self.matrix).flatten()
|
| 18 |
+
best_idx = similarities.argsort()[-1]
|
| 19 |
+
if similarities[best_idx] < threshold:
|
| 20 |
+
return "I don't have enough context for that."
|
| 21 |
+
return self.dataset[self.keys[best_idx]]
|
| 22 |
+
|
| 23 |
+
@st.cache_resource
|
| 24 |
+
def load_model():
|
| 25 |
+
with open('codepaul_lite_100k.pkl', 'rb') as f:
|
| 26 |
+
return pickle.load(f)
|
| 27 |
+
|
| 28 |
+
st.set_page_config(page_title="CodePaul Lite Ultra", page_icon="🚀")
|
| 29 |
+
st.title("🚀 CodePaul Lite Ultra")
|
| 30 |
+
st.markdown("### 100,000 Algorithmic Knowledge Nodes")
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
model = load_model()
|
| 34 |
+
query = st.text_input("Ask a coding or logic question:", "Write a Python function for binary search")
|
| 35 |
+
|
| 36 |
+
if st.button("Generate Solution"):
|
| 37 |
+
with st.spinner('Searching knowledge nodes...'):
|
| 38 |
+
response = model.ask(query)
|
| 39 |
+
st.code(response)
|
| 40 |
+
except FileNotFoundError:
|
| 41 |
+
st.error("Model file 'codepaul_lite_100k.pkl' not found. Please upload it to the Space.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|
| 2 |
+
streamlit
|