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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -15
app.py CHANGED
@@ -291,6 +291,44 @@ def load_repo_contents_backend(github_url: str):
291
  # demo.launch(share=True)
292
 
293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  with gr.Blocks() as demo:
295
  gr.Markdown("# RepoChat - Chat with Repository Files")
296
 
@@ -299,28 +337,29 @@ with gr.Blocks() as demo:
299
  gr.Markdown("### Repository Information")
300
  github_url_input = gr.Textbox(label="GitHub Repository URL", placeholder="https://github.com/username/repository")
301
  load_repo_btn = gr.Button("Load Repository Contents")
 
302
  file_dropdown = gr.Dropdown(label="Select a File", interactive=True, value="", choices=[])
303
- # For file content display, you can either use a Chatbot or a Textbox. Here we use Textbox.
304
  repo_content_output = gr.Textbox(label="File Content", interactive=False, lines=30)
305
  with gr.Column(scale=2):
306
  gr.Markdown("### Chat Interface")
307
- # Use Chatbot component for conversation history
308
  chat_output = gr.Chatbot(label="Chat Conversation")
309
  chat_query_input = gr.Textbox(label="Your Query", placeholder="Type your query here")
310
  chat_btn = gr.Button("Send Query")
311
 
312
- # Callback to update file dropdown choices.
313
  def update_file_dropdown(github_url):
314
  files = load_repo_contents_backend(github_url)
315
- if isinstance(files, str): # Error message
316
  print("Error loading files:", files)
317
- return gr.update(choices=[], value="")
318
  print("Files loaded:", files)
319
- return gr.update(choices=files, value="") # No pre-selection.
320
 
321
  load_repo_btn.click(fn=update_file_dropdown, inputs=[github_url_input], outputs=[file_dropdown])
322
 
323
- # Callback to update repository content when a file is selected.
324
  def update_repo_content(github_url, file_choice):
325
  if not file_choice:
326
  return "No file selected."
@@ -329,21 +368,21 @@ with gr.Blocks() as demo:
329
 
330
  file_dropdown.change(fn=update_repo_content, inputs=[github_url_input, file_dropdown], outputs=[repo_content_output])
331
 
332
- # Callback to process the chat query and update conversation history.
333
  def process_chat(github_url, file_choice, chat_query, history):
334
  if not file_choice:
335
  history.append(("System", "Please select a file first."))
336
  return history, history
337
  response = chat_with_file(github_url, file_choice, chat_query)
338
  history.append((chat_query, response))
339
- return history, history # Return updated conversation for both outputs.
340
 
341
- # Initialize conversation history state.
342
  conversation_history = gr.State([])
343
 
344
- chat_btn.click(fn=process_chat,
345
- inputs=[github_url_input, file_dropdown, chat_query_input, conversation_history],
346
- outputs=[chat_output, conversation_history])
 
 
347
 
348
- demo.launch(share=True)
349
-
 
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)
298
+ except Exception as e:
299
+ return str(e)
300
+ content = get_file_content(owner, repo, file_path)
301
+ return content, file_path
302
+
303
+ def chat_with_file(github_url: str, file_path: str, user_query: str):
304
+ result = get_file_content_for_choice(github_url, file_path)
305
+ if isinstance(result, 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}"
314
+
315
+ def load_repo_contents_backend(github_url: str):
316
+ try:
317
+ owner, repo = extract_repo_info(github_url)
318
+ except Exception as e:
319
+ return f"Error: {str(e)}"
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
327
+
328
+ ############################################
329
+ # Gradio Interface Setup
330
+ ############################################
331
+
332
  with gr.Blocks() as demo:
333
  gr.Markdown("# RepoChat - Chat with Repository Files")
334
 
 
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."
 
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."))
375
  return history, history
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)