Spaces:
Runtime error
Runtime error
github with streamlit - need to integrate api key retrieval and dynamic repo specification
Browse files- github_st.py +78 -0
github_st.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
USER = "heaversm"
|
15 |
+
REPO = "gdrive-docker"
|
16 |
+
GITHUB_BASE_URL = "https://github.com/"
|
17 |
+
FILE_TYPES_TO_LOAD = (".py", ".ts")
|
18 |
+
|
19 |
+
@st.cache_resource
|
20 |
+
def get_hugging_face_model():
|
21 |
+
model_name = "mchochlov/codebert-base-cd-ft"
|
22 |
+
hf = HuggingFaceEmbeddings(model_name=model_name)
|
23 |
+
return hf
|
24 |
+
|
25 |
+
def get_similar_files(query, db, embeddings):
|
26 |
+
# embedding_vector = embeddings.embed_query(query)
|
27 |
+
# docs_and_scores = db.similarity_search_by_vector(embedding_vector, k = 10)
|
28 |
+
docs_and_scores = db.similarity_search_with_score(query)
|
29 |
+
return docs_and_scores
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
st.title("Find Similar Code")
|
35 |
+
text_input = st.text_area("Enter a Code Example", value =
|
36 |
+
"""
|
37 |
+
def create_app():
|
38 |
+
app = connexion.FlaskApp(__name__, specification_dir="../.openapi")
|
39 |
+
app.add_api(
|
40 |
+
API_VERSION, resolver=connexion.resolver.RelativeResolver("provider.app")
|
41 |
+
)
|
42 |
+
""", height = 330
|
43 |
+
)
|
44 |
+
|
45 |
+
button = st.button("Find Similar Code")
|
46 |
+
if button:
|
47 |
+
loader = GithubFileLoader(
|
48 |
+
#repo is USER/REPO
|
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 |
+
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"Metadata: {doc.metadata}, 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")
|
76 |
+
|
77 |
+
|
78 |
+
|