CintraAI commited on
Commit
9358586
·
1 Parent(s): 4644b40

added code

Browse files
Files changed (1) hide show
  1. app.py +42 -48
app.py CHANGED
@@ -1,59 +1,53 @@
1
  import streamlit as st
2
- from utils import load_json, count_tokens
3
  import json
4
  import os
 
 
5
 
6
- # Set up the Streamlit page configuration
7
- st.set_page_config(page_title="Cintra Code Chunker", layout="wide")
 
 
8
 
9
- # Slider to select a value
10
- x = st.slider("Select a value")
11
- st.write(x, "squared is", x * x)
12
 
 
 
 
 
13
 
14
- code_files_directory = "example_code_files"
15
- code_files = os.listdir(code_files_directory)
16
 
17
- # Dropdown menu for the user to select a code file
18
- selected_file = st.selectbox("Select a code file", code_files)
19
 
20
- file_path = os.path.join(code_files_directory, selected_file)
 
 
21
  with open(file_path, "r") as file:
22
  code_content = file.read()
23
- st.code(code_content, language="python")
24
-
25
-
26
- def main():
27
- # Streamlit widgets for file selection
28
- st.title("Cintra Code Chunker")
29
- uploaded_file = st.file_uploader("Choose a file")
30
- if uploaded_file is not None:
31
- # Displaying the original file content
32
- file_content = uploaded_file.getvalue().decode("utf-8")
33
- st.text_area("File content", value=file_content, height=250, max_chars=50000)
34
-
35
- # Input for token chunk size target
36
- token_chunk_size = st.slider(
37
- "Select token chunk size target", min_value=10, max_value=500, value=100
38
- )
39
-
40
- # Button to trigger chunking process
41
- if st.button("Chunk Code"):
42
- # Assuming the existence of a function to chunk code based on token size
43
- # This is a placeholder for the actual chunking logic which would likely involve
44
- # the 'count_tokens' function from utils.py and some logic to split the code into chunks
45
- # For demonstration, we'll just show a message
46
- st.success(
47
- f"Code has been chunked with a target of {token_chunk_size} tokens per chunk."
48
- )
49
- # Displaying the chunked code - this would be replaced with actual chunked code display logic
50
- st.text_area(
51
- "Chunked Code",
52
- value="Chunked code would appear here...",
53
- height=250,
54
- max_chars=50000,
55
- )
56
-
57
-
58
- if __name__ == "__main__":
59
- main()
 
1
  import streamlit as st
 
2
  import json
3
  import os
4
+ from Chunker import CodeChunker
5
+ from utils import count_tokens
6
 
7
+ # Load JSON data for code file paths
8
+ def load_json_file(file_path):
9
+ with open(file_path, 'r') as file:
10
+ return json.load(file)
11
 
12
+ # Setup Streamlit page
13
+ st.set_page_config(page_title="Cintra Code Chunker", layout="wide")
 
14
 
15
+ # Assuming app.py and mock_codefiles.json are in the same directory
16
+ json_file_path = os.path.join(os.path.dirname(__file__), 'mock_codefiles.json')
17
+ code_files_data = load_json_file(json_file_path)
18
+ code_files = code_files_data['files']
19
 
20
+ # UI Elements
21
+ st.title('Cintra Code Chunker')
22
 
23
+ # File selection
24
+ selected_file_name = st.selectbox("Select a code file", code_files)
25
 
26
+ # Assuming you have the path or the content in the JSON, adjust accordingly
27
+ # This example assumes paths are stored in the JSON
28
+ file_path = os.path.join(os.path.dirname(__file__), 'example_code_files', selected_file_name)
29
  with open(file_path, "r") as file:
30
  code_content = file.read()
31
+
32
+ col1, col2 = st.columns(2)
33
+
34
+ with col1:
35
+ st.subheader('Original File')
36
+ st.code(code_content, language='python') # Adjust language dynamically based on file extension if necessary
37
+
38
+ with col2:
39
+ token_chunk_size = st.sidebar.slider('Token Chunk Size Target', min_value=5, max_value=50, value=25)
40
+ if st.sidebar.button("Chunk Code"):
41
+ # Initialize the code chunker, assuming it takes file extension and encoding name
42
+ file_extension = selected_file_name.split('.')[-1]
43
+ code_chunker = CodeChunker(file_extension=file_extension)
44
+
45
+ # Chunk the code content
46
+ chunked_code_dict = code_chunker.chunk(code_content, token_chunk_size)
47
+
48
+ # Select a chunk to display
49
+ chunk_keys = list(chunked_code_dict.keys())
50
+ selected_chunk_key = st.selectbox("Select Chunk", options=chunk_keys)
51
+
52
+ st.subheader('Chunked Code')
53
+ st.code(chunked_code_dict[selected_chunk_key], language='python') # Adjust language dynamically based on file extension if necessary