Spaces:
Runtime error
Runtime error
update main app file with new github streamlit code
Browse files- app.py +54 -78
- requirements.txt +5 -6
app.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
|
| 17 |
@st.cache_resource
|
|
@@ -20,80 +20,56 @@ def get_hugging_face_model():
|
|
| 20 |
hf = HuggingFaceEmbeddings(model_name=model_name)
|
| 21 |
return hf
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
with open("codesearchdb.pickle", "rb") as f:
|
| 27 |
-
db = CPU_Unpickler(f).load()
|
| 28 |
-
print("Loaded db")
|
| 29 |
-
# save_as_json(db, "codesearchdb.json") # Save as JSON
|
| 30 |
-
return db
|
| 31 |
-
|
| 32 |
-
def save_as_json(data, filename):
|
| 33 |
-
# Convert the data to a JSON serializable format
|
| 34 |
-
serializable_data = data_to_serializable(data)
|
| 35 |
-
with open(filename, "w") as json_file:
|
| 36 |
-
json.dump(serializable_data, json_file)
|
| 37 |
-
|
| 38 |
-
def data_to_serializable(data):
|
| 39 |
-
if isinstance(data, dict):
|
| 40 |
-
return {k: data_to_serializable(v) for k, v in data.items() if not callable(v) and not isinstance(v, type)}
|
| 41 |
-
elif isinstance(data, list):
|
| 42 |
-
return [data_to_serializable(item) for item in data]
|
| 43 |
-
elif isinstance(data, (str, int, float, bool)) or data is None:
|
| 44 |
-
return data
|
| 45 |
-
elif hasattr(data, '__dict__'):
|
| 46 |
-
return data_to_serializable(data.__dict__)
|
| 47 |
-
elif hasattr(data, '__slots__'):
|
| 48 |
-
return {slot: data_to_serializable(getattr(data, slot)) for slot in data.__slots__}
|
| 49 |
-
else:
|
| 50 |
-
return str(data) # Convert any other types to string
|
| 51 |
-
|
| 52 |
-
def get_similar_links(query, db, embeddings):
|
| 53 |
-
embedding_vector = embeddings.embed_query(query)
|
| 54 |
-
docs_and_scores = db.similarity_search_by_vector(embedding_vector, k = 10)
|
| 55 |
-
hrefs = []
|
| 56 |
-
for docs in docs_and_scores:
|
| 57 |
-
html_doc = docs.page_content
|
| 58 |
-
soup = BeautifulSoup(html_doc, 'html.parser')
|
| 59 |
-
href = [a['href'] for a in soup.find_all('a', href=True)]
|
| 60 |
-
hrefs.append(href)
|
| 61 |
-
links = []
|
| 62 |
-
for href_list in hrefs:
|
| 63 |
-
for link in href_list:
|
| 64 |
-
links.append(link)
|
| 65 |
-
return links
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
st.title("Find Similar Code")
|
| 73 |
text_input = st.text_area("Enter a Code Example", value =
|
| 74 |
"""
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
outputs.append(subSet[:])
|
| 81 |
-
return
|
| 82 |
-
for i in range(index, len(nums)):
|
| 83 |
-
backtrack(k, i + 1, subSet + [nums[i]])
|
| 84 |
-
for j in range(len(nums) + 1):
|
| 85 |
-
backtrack(j, 0, [])
|
| 86 |
-
return outputs
|
| 87 |
""", height = 330
|
| 88 |
)
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
if button:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
query = text_input
|
| 92 |
-
|
| 93 |
-
for
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
else:
|
| 97 |
-
st.info("Please Input Valid Text")
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from langchain.document_loaders import GithubFileLoader
|
| 5 |
+
# from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
+
from langchain_community.vectorstores import FAISS
|
| 8 |
+
from langchain_text_splitters import CharacterTextSplitter
|
| 9 |
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
#get the GITHUB_ACCESS_TOKEN from the .env file
|
| 13 |
+
GITHUB_ACCESS_TOKEN = os.getenv("GITHUB_ACCESS_TOKEN")
|
| 14 |
+
GITHUB_BASE_URL = "https://github.com/"
|
| 15 |
|
| 16 |
|
| 17 |
@st.cache_resource
|
|
|
|
| 20 |
hf = HuggingFaceEmbeddings(model_name=model_name)
|
| 21 |
return hf
|
| 22 |
|
| 23 |
+
def get_similar_files(query, db, embeddings):
|
| 24 |
+
docs_and_scores = db.similarity_search_with_score(query)
|
| 25 |
+
return docs_and_scores
|
| 26 |
|
| 27 |
+
# STREAMLIT INTERFACE
|
| 28 |
+
st.title("Find Similar Code")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
USER = st.text_input("Enter the Github User", value = "heaversm")
|
| 31 |
+
REPO = st.text_input("Enter the Github Repository", value = "gdrive-docker")
|
| 32 |
+
FILE_TYPES_TO_LOAD = st.multiselect("Select File Types", [".py", ".ts",".js",".css",".html"], default = [".py"])
|
| 33 |
|
|
|
|
| 34 |
text_input = st.text_area("Enter a Code Example", value =
|
| 35 |
"""
|
| 36 |
+
def create_app():
|
| 37 |
+
app = connexion.FlaskApp(__name__, specification_dir="../.openapi")
|
| 38 |
+
app.add_api(
|
| 39 |
+
API_VERSION, resolver=connexion.resolver.RelativeResolver("provider.app")
|
| 40 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
""", height = 330
|
| 42 |
)
|
| 43 |
+
|
| 44 |
+
button = st.button("Find Similar Code")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
if button:
|
| 48 |
+
loader = GithubFileLoader(
|
| 49 |
+
repo=f"{USER}/{REPO}",
|
| 50 |
+
access_token=GITHUB_ACCESS_TOKEN,
|
| 51 |
+
github_api_url="https://api.github.com",
|
| 52 |
+
file_filter=lambda file_path: file_path.endswith(
|
| 53 |
+
tuple(FILE_TYPES_TO_LOAD)
|
| 54 |
+
)
|
| 55 |
+
)
|
| 56 |
+
documents = loader.load()
|
| 57 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
| 58 |
+
docs = text_splitter.split_documents(documents)
|
| 59 |
+
embedding_vector = get_hugging_face_model()
|
| 60 |
+
db = FAISS.from_documents(docs, embedding_vector)
|
| 61 |
query = text_input
|
| 62 |
+
results_with_scores = get_similar_files(query, db, embedding_vector)
|
| 63 |
+
for doc, score in results_with_scores:
|
| 64 |
+
print(f"Path: {doc.metadata['path']}, Score: {score}")
|
| 65 |
+
|
| 66 |
+
top_file_path = results_with_scores[0][0].metadata['path']
|
| 67 |
+
top_file_content = results_with_scores[0][0].page_content
|
| 68 |
+
top_file_score = results_with_scores[0][1]
|
| 69 |
+
top_file_link = f"{GITHUB_BASE_URL}{USER}/{REPO}/blob/main/{top_file_path}"
|
| 70 |
+
# write a clickable link in streamlit
|
| 71 |
+
st.markdown(f"[Top file link]({top_file_link})")
|
| 72 |
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
else:
|
| 75 |
+
st.info("Please Submit a Code Sample to Find Similar Code")
|
requirements.txt
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
|
|
|
|
|
| 1 |
langchain
|
| 2 |
-
sentence-transformers
|
| 3 |
-
bs4
|
| 4 |
-
faiss-cpu
|
| 5 |
-
altair==4.0
|
| 6 |
langchain-community
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
python-dotenv
|
| 3 |
langchain
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
langchain-community
|
| 5 |
+
langchain_huggingface
|
| 6 |
+
langchain_text_splitters
|
| 7 |
+
sentence-transformers
|