Spaces:
Running
Running
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 5 |
from langchain_community.document_loaders import DirectoryLoader
|
| 6 |
from langchain_text_splitters import RecursiveCharacterTextSplitter, Language
|
|
@@ -64,8 +66,10 @@ device_status = "π’ GPU Active" if torch.cuda.is_available() else "π‘ CPU Mo
|
|
| 64 |
llm = load_llm()
|
| 65 |
vector_db, file_count = setup_vector_db()
|
| 66 |
|
| 67 |
-
prompt_template = """
|
| 68 |
-
If
|
|
|
|
|
|
|
| 69 |
|
| 70 |
Context: {context}
|
| 71 |
|
|
@@ -77,9 +81,11 @@ prompt = PromptTemplate.from_template(prompt_template)
|
|
| 77 |
def format_docs(docs):
|
| 78 |
return "\n\n".join(doc.page_content for doc in docs)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
{"context": retriever, "input": RunnablePassthrough()}
|
| 84 |
| RunnablePassthrough.assign(
|
| 85 |
answer=(
|
|
@@ -90,13 +96,53 @@ if vector_db:
|
|
| 90 |
)
|
| 91 |
)
|
| 92 |
)
|
| 93 |
-
else:
|
| 94 |
-
qa_chain = None
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
def respond(message, chat_history):
|
| 98 |
if not vector_db:
|
| 99 |
-
bot_message = "π Welcome! Please
|
| 100 |
chat_history.append((message, bot_message))
|
| 101 |
return "", chat_history
|
| 102 |
|
|
@@ -118,12 +164,17 @@ def respond(message, chat_history):
|
|
| 118 |
chat_history.append((message, final_answer))
|
| 119 |
return "", chat_history
|
| 120 |
|
| 121 |
-
#
|
| 122 |
custom_css = """
|
| 123 |
.status-box { padding: 10px; border-radius: 8px; background-color: #f0f0f0; margin-bottom: 10px; }
|
| 124 |
.dark .status-box { background-color: #1e293b; color: #cbd5e1; }
|
| 125 |
"""
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
with gr.Blocks(title="Codebase Assistant", css=custom_css) as demo:
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column(scale=1):
|
|
@@ -133,10 +184,18 @@ with gr.Blocks(title="Codebase Assistant", css=custom_css) as demo:
|
|
| 133 |
with gr.Column(elem_classes=["status-box"]):
|
| 134 |
gr.Markdown("### System Status")
|
| 135 |
gr.Markdown(f"**Hardware:** {device_status}")
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
with gr.Column(scale=3):
|
| 142 |
gr.Markdown("### π» Chat with your Codebase\nAsk architecture questions, find bugs, or request code explanations.")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
+
import shutil
|
| 5 |
+
import subprocess
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 7 |
from langchain_community.document_loaders import DirectoryLoader
|
| 8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter, Language
|
|
|
|
| 66 |
llm = load_llm()
|
| 67 |
vector_db, file_count = setup_vector_db()
|
| 68 |
|
| 69 |
+
prompt_template = """You are a specialized Codebase Assistant. Your ONLY purpose is to answer questions related to the provided codebase or general programming/coding questions.
|
| 70 |
+
If the user asks a question that is NOT related to coding, programming, or the provided codebase, you must politely refuse to answer and remind them that you are a code-focused assistant.
|
| 71 |
+
|
| 72 |
+
Use the following codebase context to answer the question. If you don't know the answer, just say that you don't know, don't try to make up code.
|
| 73 |
|
| 74 |
Context: {context}
|
| 75 |
|
|
|
|
| 81 |
def format_docs(docs):
|
| 82 |
return "\n\n".join(doc.page_content for doc in docs)
|
| 83 |
|
| 84 |
+
def build_qa_chain(db):
|
| 85 |
+
if not db:
|
| 86 |
+
return None
|
| 87 |
+
retriever = db.as_retriever(search_kwargs={"k": 3})
|
| 88 |
+
return (
|
| 89 |
{"context": retriever, "input": RunnablePassthrough()}
|
| 90 |
| RunnablePassthrough.assign(
|
| 91 |
answer=(
|
|
|
|
| 96 |
)
|
| 97 |
)
|
| 98 |
)
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
qa_chain = build_qa_chain(vector_db)
|
| 101 |
+
|
| 102 |
+
# 4. INGESTION FUNCTIONS
|
| 103 |
+
def clone_and_index(repo_url):
|
| 104 |
+
global vector_db, file_count, qa_chain
|
| 105 |
+
if os.path.exists('./repo'):
|
| 106 |
+
shutil.rmtree('./repo')
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
subprocess.run(["git", "clone", repo_url, "./repo"], check=True)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
return f"**Repo Status:** Failed to clone repo: {str(e)} β"
|
| 112 |
+
|
| 113 |
+
vector_db, file_count = setup_vector_db()
|
| 114 |
+
qa_chain = build_qa_chain(vector_db)
|
| 115 |
+
|
| 116 |
+
if vector_db:
|
| 117 |
+
return f"**Repo Status:** {file_count} files indexed from `{repo_url}` β
"
|
| 118 |
+
else:
|
| 119 |
+
return f"**Repo Status:** No Python files found in `{repo_url}` β"
|
| 120 |
+
|
| 121 |
+
def upload_and_index(files):
|
| 122 |
+
global vector_db, file_count, qa_chain
|
| 123 |
+
if os.path.exists('./repo'):
|
| 124 |
+
shutil.rmtree('./repo')
|
| 125 |
+
os.makedirs('./repo', exist_ok=True)
|
| 126 |
+
|
| 127 |
+
if not files:
|
| 128 |
+
return "**Repo Status:** No files uploaded β"
|
| 129 |
+
|
| 130 |
+
for file in files:
|
| 131 |
+
dest_path = os.path.join('./repo', os.path.basename(file.name))
|
| 132 |
+
shutil.copy(file.name, dest_path)
|
| 133 |
+
|
| 134 |
+
vector_db, file_count = setup_vector_db()
|
| 135 |
+
qa_chain = build_qa_chain(vector_db)
|
| 136 |
+
|
| 137 |
+
if vector_db:
|
| 138 |
+
return f"**Repo Status:** {file_count} files indexed from local upload β
"
|
| 139 |
+
else:
|
| 140 |
+
return "**Repo Status:** No Python files found in local upload β"
|
| 141 |
+
|
| 142 |
+
# 5. CHAT LOGIC
|
| 143 |
def respond(message, chat_history):
|
| 144 |
if not vector_db:
|
| 145 |
+
bot_message = "π Welcome! Please provide a repo link or upload Python files to start chatting."
|
| 146 |
chat_history.append((message, bot_message))
|
| 147 |
return "", chat_history
|
| 148 |
|
|
|
|
| 164 |
chat_history.append((message, final_answer))
|
| 165 |
return "", chat_history
|
| 166 |
|
| 167 |
+
# 6. GRADIO UI
|
| 168 |
custom_css = """
|
| 169 |
.status-box { padding: 10px; border-radius: 8px; background-color: #f0f0f0; margin-bottom: 10px; }
|
| 170 |
.dark .status-box { background-color: #1e293b; color: #cbd5e1; }
|
| 171 |
"""
|
| 172 |
|
| 173 |
+
def get_initial_repo_status():
|
| 174 |
+
if vector_db:
|
| 175 |
+
return f"**Repo Status:** {file_count} files indexed β
"
|
| 176 |
+
return "**Repo Status:** Empty β\n\nProvide a repo link or upload files to begin analyzing."
|
| 177 |
+
|
| 178 |
with gr.Blocks(title="Codebase Assistant", css=custom_css) as demo:
|
| 179 |
with gr.Row():
|
| 180 |
with gr.Column(scale=1):
|
|
|
|
| 184 |
with gr.Column(elem_classes=["status-box"]):
|
| 185 |
gr.Markdown("### System Status")
|
| 186 |
gr.Markdown(f"**Hardware:** {device_status}")
|
| 187 |
+
repo_status = gr.Markdown(get_initial_repo_status())
|
| 188 |
+
|
| 189 |
+
gr.Markdown("### Add Codebase")
|
| 190 |
+
with gr.Tab("GitHub Repo"):
|
| 191 |
+
repo_url = gr.Textbox(placeholder="https://github.com/user/repo", show_label=False)
|
| 192 |
+
clone_btn = gr.Button("Clone & Index")
|
| 193 |
+
with gr.Tab("Local Upload"):
|
| 194 |
+
local_files = gr.File(file_count="multiple", label="Upload Local Files", file_types=[".py"])
|
| 195 |
+
upload_btn = gr.Button("Upload & Index")
|
| 196 |
+
|
| 197 |
+
clone_btn.click(fn=clone_and_index, inputs=[repo_url], outputs=[repo_status])
|
| 198 |
+
upload_btn.click(fn=upload_and_index, inputs=[local_files], outputs=[repo_status])
|
| 199 |
|
| 200 |
with gr.Column(scale=3):
|
| 201 |
gr.Markdown("### π» Chat with your Codebase\nAsk architecture questions, find bugs, or request code explanations.")
|