jyo01 commited on
Commit
b6c6354
·
verified ·
1 Parent(s): 737b2e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -80
app.py CHANGED
@@ -191,51 +191,51 @@ def get_gemini_flash_response(prompt: str) -> str:
191
 
192
 
193
 
194
- ############################################
195
- # Gradio Interface Functions
196
- ############################################
197
-
198
- # For file content retrieval, we now use the file path directly.
199
- def get_file_content_for_choice(github_url: str, file_path: str):
200
- try:
201
- owner, repo = extract_repo_info(github_url)
202
- except Exception as e:
203
- return str(e)
204
- content = get_file_content(owner, repo, file_path)
205
- return content, file_path
206
-
207
- def chat_with_file(github_url: str, file_path: str, user_query: str):
208
- # Retrieve file content using the file path directly.
209
- result = get_file_content_for_choice(github_url, file_path)
210
- if isinstance(result, str):
211
- return result # Return error message if occurred.
212
- file_content, selected_file = result
213
-
214
- # Preprocess file content and extract context.
215
- preprocessed = preprocess_text(file_content)
216
- context_snippet = preprocessed[:5000] # Use first 1000 characters as context.
217
-
218
- # Generate the prompt based on context and user query.
219
- prompt = generate_prompt(user_query, [context_snippet])
220
-
221
- # Use Gemini Flash to generate a response.
222
- llm_response = get_gemini_flash_response(prompt)
223
-
224
- return f"File: {selected_file}\n\nLLM Response:\n{llm_response}"
225
-
226
-
227
- def load_repo_contents_backend(github_url: str):
228
- try:
229
- owner, repo = extract_repo_info(github_url)
230
- except Exception as e:
231
- return f"Error: {str(e)}"
232
- repo_data = get_repo_metadata(owner, repo)
233
- default_branch = repo_data.get("default_branch", "main")
234
- tree_data = get_repo_tree(owner, repo, default_branch)
235
- if "tree" not in tree_data:
236
- return "Error: Could not fetch repository tree."
237
- file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
238
- return file_list
239
 
240
  ############################################
241
  # Gradio Interface Setup
@@ -291,7 +291,6 @@ def load_repo_contents_backend(github_url: str):
291
  # demo.launch(share=True)
292
 
293
 
294
- # Retrieve file content using file path.
295
  def get_file_content_for_choice(github_url: str, file_path: str):
296
  try:
297
  owner, repo = extract_repo_info(github_url)
@@ -306,8 +305,7 @@ def chat_with_file(github_url: str, file_path: str, user_query: str):
306
  return result # Return error message if occurred.
307
  file_content, selected_file = result
308
  preprocessed = preprocess_text(file_content)
309
- # Use the first 1000 characters as context (adjust as needed).
310
- context_snippet = preprocessed[:1000]
311
  prompt = generate_prompt(user_query, [context_snippet])
312
  llm_response = get_gemini_flash_response(prompt)
313
  return f"File: {selected_file}\n\nLLM Response:\n{llm_response}"
@@ -320,7 +318,7 @@ def load_repo_contents_backend(github_url: str):
320
  repo_data = get_repo_metadata(owner, repo)
321
  default_branch = repo_data.get("default_branch", "main")
322
  tree_data = get_repo_tree(owner, repo, default_branch)
323
- if "tree" not in tree_data:
324
  return "Error: Could not fetch repository tree."
325
  file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
326
  return file_list
@@ -331,44 +329,36 @@ def load_repo_contents_backend(github_url: str):
331
 
332
  with gr.Blocks() as demo:
333
  gr.Markdown("# RepoChat - Chat with Repository Files")
334
-
335
  with gr.Row():
336
  with gr.Column(scale=1):
337
  gr.Markdown("### Repository Information")
338
  github_url_input = gr.Textbox(label="GitHub Repository URL", placeholder="https://github.com/username/repository")
339
  load_repo_btn = gr.Button("Load Repository Contents")
340
- # File dropdown without preselection.
341
- file_dropdown = gr.Dropdown(label="Select a File", interactive=True, value="", choices=[])
342
- # Display file content in a textbox.
343
- repo_content_output = gr.Textbox(label="File Content", interactive=False, lines=30)
344
  with gr.Column(scale=2):
345
  gr.Markdown("### Chat Interface")
346
- # Use Chatbot component for conversation history.
347
  chat_output = gr.Chatbot(label="Chat Conversation")
348
  chat_query_input = gr.Textbox(label="Your Query", placeholder="Type your query here")
349
  chat_btn = gr.Button("Send Query")
350
-
351
- # Callback: Update file dropdown choices.
 
 
352
  def update_file_dropdown(github_url):
353
  files = load_repo_contents_backend(github_url)
354
- if isinstance(files, str): # If an error message was returned.
355
- print("Error loading files:", files)
356
- return {"choices": [], "value": ""}
357
- print("Files loaded:", files)
358
- return {"choices": files, "value": ""}
359
-
360
- load_repo_btn.click(fn=update_file_dropdown, inputs=[github_url_input], outputs=[file_dropdown])
361
-
362
- # Callback: Update repository content display when a file is selected.
363
  def update_repo_content(github_url, file_choice):
364
  if not file_choice:
365
  return "No file selected."
366
  content, _ = get_file_content_for_choice(github_url, file_choice)
367
  return content
368
-
369
- file_dropdown.change(fn=update_repo_content, inputs=[github_url_input, file_dropdown], outputs=[repo_content_output])
370
-
371
- # Callback: Process chat query and update conversation history.
372
  def process_chat(github_url, file_choice, chat_query, history):
373
  if not file_choice:
374
  history.append(("System", "Please select a file first."))
@@ -376,13 +366,9 @@ with gr.Blocks() as demo:
376
  response = chat_with_file(github_url, file_choice, chat_query)
377
  history.append((chat_query, response))
378
  return history, history
379
-
380
- conversation_history = gr.State([])
381
-
382
- chat_btn.click(
383
- fn=process_chat,
384
- inputs=[github_url_input, file_dropdown, chat_query_input, conversation_history],
385
- outputs=[chat_output, conversation_history]
386
- )
387
-
388
- demo.launch(share=True, server_port=7862)
 
191
 
192
 
193
 
194
+ # ############################################
195
+ # # Gradio Interface Functions
196
+ # ############################################
197
+
198
+ # # For file content retrieval, we now use the file path directly.
199
+ # def get_file_content_for_choice(github_url: str, file_path: str):
200
+ # try:
201
+ # owner, repo = extract_repo_info(github_url)
202
+ # except Exception as e:
203
+ # return str(e)
204
+ # content = get_file_content(owner, repo, file_path)
205
+ # return content, file_path
206
+
207
+ # def chat_with_file(github_url: str, file_path: str, user_query: str):
208
+ # # Retrieve file content using the file path directly.
209
+ # result = get_file_content_for_choice(github_url, file_path)
210
+ # if isinstance(result, str):
211
+ # return result # Return error message if occurred.
212
+ # file_content, selected_file = result
213
+
214
+ # # Preprocess file content and extract context.
215
+ # preprocessed = preprocess_text(file_content)
216
+ # context_snippet = preprocessed[:5000] # Use first 1000 characters as context.
217
+
218
+ # # Generate the prompt based on context and user query.
219
+ # prompt = generate_prompt(user_query, [context_snippet])
220
+
221
+ # # Use Gemini Flash to generate a response.
222
+ # llm_response = get_gemini_flash_response(prompt)
223
+
224
+ # return f"File: {selected_file}\n\nLLM Response:\n{llm_response}"
225
+
226
+
227
+ # def load_repo_contents_backend(github_url: str):
228
+ # try:
229
+ # owner, repo = extract_repo_info(github_url)
230
+ # except Exception as e:
231
+ # return f"Error: {str(e)}"
232
+ # repo_data = get_repo_metadata(owner, repo)
233
+ # default_branch = repo_data.get("default_branch", "main")
234
+ # tree_data = get_repo_tree(owner, repo, default_branch)
235
+ # if "tree" not in tree_data:
236
+ # return "Error: Could not fetch repository tree."
237
+ # file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
238
+ # return file_list
239
 
240
  ############################################
241
  # Gradio Interface Setup
 
291
  # demo.launch(share=True)
292
 
293
 
 
294
  def get_file_content_for_choice(github_url: str, file_path: str):
295
  try:
296
  owner, repo = extract_repo_info(github_url)
 
305
  return result # Return error message if occurred.
306
  file_content, selected_file = result
307
  preprocessed = preprocess_text(file_content)
308
+ context_snippet = preprocessed[:1000] # Use the first 1000 characters as context
 
309
  prompt = generate_prompt(user_query, [context_snippet])
310
  llm_response = get_gemini_flash_response(prompt)
311
  return f"File: {selected_file}\n\nLLM Response:\n{llm_response}"
 
318
  repo_data = get_repo_metadata(owner, repo)
319
  default_branch = repo_data.get("default_branch", "main")
320
  tree_data = get_repo_tree(owner, repo, default_branch)
321
+ if not isinstance(tree_data, dict) or "tree" not in tree_data:
322
  return "Error: Could not fetch repository tree."
323
  file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
324
  return file_list
 
329
 
330
  with gr.Blocks() as demo:
331
  gr.Markdown("# RepoChat - Chat with Repository Files")
332
+
333
  with gr.Row():
334
  with gr.Column(scale=1):
335
  gr.Markdown("### Repository Information")
336
  github_url_input = gr.Textbox(label="GitHub Repository URL", placeholder="https://github.com/username/repository")
337
  load_repo_btn = gr.Button("Load Repository Contents")
338
+ file_dropdown = gr.Dropdown(label="Select a File", interactive=True, choices=[])
339
+ repo_content_output = gr.Textbox(label="File Content", interactive=False, lines=20)
340
+
 
341
  with gr.Column(scale=2):
342
  gr.Markdown("### Chat Interface")
 
343
  chat_output = gr.Chatbot(label="Chat Conversation")
344
  chat_query_input = gr.Textbox(label="Your Query", placeholder="Type your query here")
345
  chat_btn = gr.Button("Send Query")
346
+
347
+ # State to hold conversation history
348
+ conversation_history = gr.State([])
349
+
350
  def update_file_dropdown(github_url):
351
  files = load_repo_contents_backend(github_url)
352
+ if isinstance(files, str): # Error message
353
+ return gr.update(choices=[]), files
354
+ return gr.update(choices=files), ""
355
+
 
 
 
 
 
356
  def update_repo_content(github_url, file_choice):
357
  if not file_choice:
358
  return "No file selected."
359
  content, _ = get_file_content_for_choice(github_url, file_choice)
360
  return content
361
+
 
 
 
362
  def process_chat(github_url, file_choice, chat_query, history):
363
  if not file_choice:
364
  history.append(("System", "Please select a file first."))
 
366
  response = chat_with_file(github_url, file_choice, chat_query)
367
  history.append((chat_query, response))
368
  return history, history
369
+
370
+ load_repo_btn.click(fn=update_file_dropdown, inputs=[github_url_input], outputs=[file_dropdown, repo_content_output])
371
+ file_dropdown.change(fn=update_repo_content, inputs=[github_url_input, file_dropdown], outputs=[repo_content_output])
372
+ chat_btn.click(fn=process_chat, inputs=[github_url_input, file_dropdown, chat_query_input, conversation_history], outputs=[chat_output, conversation_history])
373
+
374
+ demo.launch(server_port=7862)