Update app.py
Browse files
app.py
CHANGED
@@ -136,51 +136,70 @@ def get_llm_response(prompt: str, model_name: str = "meta-llama/Llama-2-7b-chat-
|
|
136 |
# Gradio Interface Functions
|
137 |
############################################
|
138 |
|
|
|
|
|
139 |
def load_repo_contents(github_url: str):
|
140 |
try:
|
141 |
-
|
|
|
|
|
142 |
except Exception as e:
|
143 |
-
|
144 |
-
|
145 |
-
default_branch = repo_data.get("default_branch", "main")
|
146 |
-
tree_data = get_repo_tree(owner, repo, default_branch)
|
147 |
-
if "tree" not in tree_data:
|
148 |
-
return "Error: Could not fetch repository tree."
|
149 |
-
file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
|
150 |
-
return file_list
|
151 |
|
152 |
-
|
|
|
153 |
try:
|
154 |
owner, repo = extract_repo_info(github_url)
|
155 |
except Exception as e:
|
156 |
-
return str(e)
|
157 |
repo_data = get_repo_metadata(owner, repo)
|
158 |
default_branch = repo_data.get("default_branch", "main")
|
159 |
tree_data = get_repo_tree(owner, repo, default_branch)
|
160 |
if "tree" not in tree_data:
|
161 |
return "Error: Could not fetch repository tree."
|
162 |
file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
|
163 |
-
|
164 |
-
return "Error: Invalid file choice."
|
165 |
-
selected_file = file_list[file_choice - 1]
|
166 |
-
content = get_file_content(owner, repo, selected_file)
|
167 |
-
return content, selected_file
|
168 |
-
|
169 |
-
|
170 |
-
def chat_with_file(github_url: str, file_choice: int, user_query: str):
|
171 |
-
result = get_file_content_for_choice(github_url, file_choice)
|
172 |
-
if isinstance(result, str):
|
173 |
-
return result # Error message
|
174 |
-
file_content, selected_file = result
|
175 |
-
preprocessed = preprocess_text(file_content)
|
176 |
-
context_snippet = preprocessed[:1000] # use first 1000 characters as context
|
177 |
-
prompt = generate_prompt(user_query, [context_snippet])
|
178 |
-
llm_response = get_llm_response(prompt)
|
179 |
-
return f"File: {selected_file}\n\nLLM Response:\n{llm_response}"
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
############################################
|
185 |
# Gradio Interface Setup
|
186 |
############################################
|
|
|
136 |
# Gradio Interface Functions
|
137 |
############################################
|
138 |
|
139 |
+
|
140 |
+
# Debug: Ensure load_repo_contents returns the file list properly.
|
141 |
def load_repo_contents(github_url: str):
|
142 |
try:
|
143 |
+
files = load_repo_contents_backend(github_url)
|
144 |
+
print("Files loaded:", files) # Debug print
|
145 |
+
return files
|
146 |
except Exception as e:
|
147 |
+
print("Error loading repo contents:", str(e))
|
148 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
+
# Original function renamed to avoid conflict.
|
151 |
+
def load_repo_contents_backend(github_url: str):
|
152 |
try:
|
153 |
owner, repo = extract_repo_info(github_url)
|
154 |
except Exception as e:
|
155 |
+
return f"Error: {str(e)}"
|
156 |
repo_data = get_repo_metadata(owner, repo)
|
157 |
default_branch = repo_data.get("default_branch", "main")
|
158 |
tree_data = get_repo_tree(owner, repo, default_branch)
|
159 |
if "tree" not in tree_data:
|
160 |
return "Error: Could not fetch repository tree."
|
161 |
file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
|
162 |
+
return file_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
+
# Update the file dropdown without allow_custom_value.
|
165 |
+
with gr.Blocks() as demo:
|
166 |
+
gr.Markdown("# RepoChat - Chat with Repository Files")
|
167 |
+
|
168 |
+
with gr.Row():
|
169 |
+
with gr.Column(scale=1):
|
170 |
+
gr.Markdown("### Repository Information")
|
171 |
+
github_url_input = gr.Textbox(label="GitHub Repository URL", placeholder="https://github.com/username/repository")
|
172 |
+
load_repo_btn = gr.Button("Load Repository Contents")
|
173 |
+
file_dropdown = gr.Dropdown(label="Select a File", interactive=True) # Removed allow_custom_value
|
174 |
+
repo_content_output = gr.Textbox(label="File Content", interactive=False, lines=10)
|
175 |
+
with gr.Column(scale=2):
|
176 |
+
gr.Markdown("### Chat Interface")
|
177 |
+
chat_query_input = gr.Textbox(label="Your Query", placeholder="Type your query here")
|
178 |
+
chat_output = gr.Textbox(label="Chatbot Response", interactive=False, lines=10)
|
179 |
+
chat_btn = gr.Button("Send Query")
|
180 |
+
|
181 |
+
# Update file dropdown based on GitHub URL.
|
182 |
+
load_repo_btn.click(fn=load_repo_contents, inputs=[github_url_input], outputs=[file_dropdown])
|
183 |
+
|
184 |
+
def update_repo_content(github_url, file_choice):
|
185 |
+
if not file_choice:
|
186 |
+
return "No file selected."
|
187 |
+
try:
|
188 |
+
file_index = int(file_choice)
|
189 |
+
except Exception as e:
|
190 |
+
print("Error converting file choice:", str(e))
|
191 |
+
file_index = 1
|
192 |
+
content, _ = get_file_content_for_choice(github_url, file_index)
|
193 |
+
return content
|
194 |
+
|
195 |
+
file_dropdown.change(fn=update_repo_content, inputs=[github_url_input, file_dropdown], outputs=[repo_content_output])
|
196 |
+
|
197 |
+
def process_chat(github_url, file_choice, chat_query):
|
198 |
+
return chat_with_file(github_url, int(file_choice), chat_query)
|
199 |
+
|
200 |
+
chat_btn.click(fn=process_chat, inputs=[github_url_input, file_dropdown, chat_query_input], outputs=[chat_output])
|
201 |
+
|
202 |
+
demo.launch(share=True)
|
203 |
############################################
|
204 |
# Gradio Interface Setup
|
205 |
############################################
|