Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
|
6 |
+
# --- Constants ---
|
7 |
+
GITHUB_API_URL = "https://api.github.com"
|
8 |
+
GITHUB_REPO_OWNER = "canstralian"
|
9 |
+
GITHUB_REPO_NAME = "CodeShieldAI"
|
10 |
+
GITHUB_HEADERS = {
|
11 |
+
"Authorization": f"Bearer {os.environ.get('GITHUB_TOKEN')}",
|
12 |
+
"Accept": "application/vnd.github+json",
|
13 |
+
"X-GitHub-Api-Version": "2022-11-28",
|
14 |
+
}
|
15 |
+
|
16 |
+
# --- Functions ---
|
17 |
+
def get_repo_contents(path=""):
|
18 |
+
url = f"{GITHUB_API_URL}/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/contents/{path}"
|
19 |
+
response = requests.get(url, headers=GITHUB_HEADERS)
|
20 |
+
if response.status_code == 200:
|
21 |
+
return response.json()
|
22 |
+
else:
|
23 |
+
st.error(f"Failed to fetch contents. Status code: {response.status_code}")
|
24 |
+
return None
|
25 |
+
|
26 |
+
def get_file_content(file_path):
|
27 |
+
url = f"{GITHUB_API_URL}/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/contents/{file_path}"
|
28 |
+
response = requests.get(url, headers=GITHUB_HEADERS)
|
29 |
+
if response.status_code == 200:
|
30 |
+
content = response.json().get("content")
|
31 |
+
if content:
|
32 |
+
import base64
|
33 |
+
return base64.b64decode(content).decode("utf-8")
|
34 |
+
else:
|
35 |
+
st.error("File content not found.")
|
36 |
+
return None
|
37 |
+
else:
|
38 |
+
st.error(f"Failed to fetch file content. Status code: {response.status_code}")
|
39 |
+
return None
|
40 |
+
|
41 |
+
# --- Streamlit App ---
|
42 |
+
st.title("CodeShieldAI Browser")
|
43 |
+
|
44 |
+
if not os.environ.get('GITHUB_TOKEN'):
|
45 |
+
st.warning("Please set the GITHUB_TOKEN environment variable.")
|
46 |
+
else:
|
47 |
+
path = st.text_input("Enter path (e.g., 'src'):", "")
|
48 |
+
|
49 |
+
contents = get_repo_contents(path)
|
50 |
+
|
51 |
+
if contents:
|
52 |
+
files = [item for item in contents if item["type"] == "file"]
|
53 |
+
directories = [item for item in contents if item["type"] == "dir"]
|
54 |
+
|
55 |
+
st.subheader("Directories:")
|
56 |
+
for directory in directories:
|
57 |
+
if st.button(f"Open {directory['name']}"):
|
58 |
+
st.experimental_rerun()
|
59 |
+
path = f"{path}/{directory['name']}" if path else directory['name']
|
60 |
+
st.text_input("Enter path (e.g., 'src'):", path)
|
61 |
+
|
62 |
+
st.subheader("Files:")
|
63 |
+
for file in files:
|
64 |
+
if st.button(f"View {file['name']}"):
|
65 |
+
file_path = f"{path}/{file['name']}" if path else file['name']
|
66 |
+
file_content = get_file_content(file_path)
|
67 |
+
if file_content:
|
68 |
+
st.code(file_content, language=file['name'].split('.')[-1] if '.' in file['name'] else '')
|