rcs3 commited on
Commit
44f31ea
·
verified ·
1 Parent(s): da33af7

update utlis.py

Browse files
Files changed (1) hide show
  1. repochat/utils.py +29 -53
repochat/utils.py CHANGED
@@ -1,66 +1,42 @@
1
  import os
2
- import re
3
  import shutil
4
  import subprocess
5
  import streamlit as st
6
 
7
- def init_session_state():
8
- SESSION_DEFAULTS = {
9
- "messages": [],
10
- "chroma_db": None,
11
- "db_loaded": False,
12
- "repo_path": './cloned_repo',
13
- "git_form": False,
14
- "qa": None,
15
- "db_name": None
16
- }
17
-
18
- for keys, values in SESSION_DEFAULTS.items():
19
- if keys not in st.session_state:
20
- st.session_state[keys] = values
21
 
22
- def url_name(url):
23
- pattern = r"https?://github.com/([^/]+)/([^/]+)"
24
- match = re.match(pattern, url)
25
- if match:
26
- owner = match.group(1)
27
- repo = match.group(2)
28
- return f"{owner}_{repo}"
29
- else:
30
- st.error("Enter valid GitHub URL")
31
- st.stop()
32
 
33
- def clone_repo(git_url, repo_path):
34
- if os.path.exists(repo_path):
35
- shutil.rmtree(repo_path)
36
-
37
- git_url = git_url.replace(".git", "")
38
 
39
- command = f'git clone {git_url}.git {repo_path} && rm -rf {repo_path}/.git'
40
- subprocess.run(command, shell=True)
 
 
41
 
42
- def prompt_format(system_prompt, instruction):
43
- B_INST, E_INST = "[INST]", "[/INST]"
44
- B_SYS, E_SYS = "<SYS>>\n", "\n<</SYS>>\n\n"
45
- SYSTEM_PROMPT = B_SYS + system_prompt + E_SYS
46
- prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
47
- return prompt_template
48
 
49
- def model_prompt():
50
- system_prompt = """You are a helpful assistant, you have good knowledge in coding and you will use the provided context to answer user questions with detailed explanations.
51
- Read the given context before answering questions and think step by step. If you can not answer a user question based on the provided context, inform the user. Do not use any other information for answering user"""
52
- instruction = """
53
- Context: {context}
54
- User: {question}"""
55
- return prompt_format(system_prompt, instruction)
56
 
57
- def custom_que_prompt():
58
- que_system_prompt = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question and give only the standalone question as output in the tags <question> and </question>.
59
- """
 
 
60
 
61
- instr_prompt = """Chat History:
62
- {chat_history}
63
- Follow Up Input: {question}
64
- Standalone question:"""
65
 
66
- return prompt_format(que_system_prompt, instr_prompt)
 
 
 
 
1
  import os
 
2
  import shutil
3
  import subprocess
4
  import streamlit as st
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ SESSION_DEFAULTS = {
8
+ "messages": [],
9
+ "chroma_db": None,
10
+ "db_loaded": False,
11
+ "repo_path": "./cloned_repo",
12
+ "git_form": False,
13
+ "qa": None,
14
+ "db_name": None,
15
+ }
 
16
 
 
 
 
 
 
17
 
18
+ def init_session_state():
19
+ for key, default in SESSION_DEFAULTS.items():
20
+ if key not in st.session_state:
21
+ st.session_state[key] = default
22
 
 
 
 
 
 
 
23
 
24
+ def clone_repo(git_url: str, repo_path: str):
25
+ """Clone a public GitHub repo, stripping the .git metadata afterwards."""
26
+ if os.path.exists(repo_path):
27
+ shutil.rmtree(repo_path)
 
 
 
28
 
29
+ result = subprocess.run(
30
+ ["git", "clone", "--depth", "1", git_url, repo_path],
31
+ capture_output=True,
32
+ text=True,
33
+ )
34
 
35
+ if result.returncode != 0:
36
+ st.error(f"Git clone failed:\n```\n{result.stderr}\n```")
37
+ st.stop()
 
38
 
39
+ # Remove .git to save disk space
40
+ git_dir = os.path.join(repo_path, ".git")
41
+ if os.path.exists(git_dir):
42
+ shutil.rmtree(git_dir)