Skip to content

Commit 7b0c443

Browse files
committed
Merge branch 'main' into linda-uwsgi-with-more
2 parents a485225 + 15c0c65 commit 7b0c443

File tree

10 files changed

+675
-341
lines changed

10 files changed

+675
-341
lines changed

.devcontainer/devcontainer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55
},
66
"features": {
77
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8-
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
9+
"ghcr.io/devcontainers/features/node:1": {
10+
"version": "20"
11+
},
12+
"ghcr.io/devcontainers/features/go:1": {
13+
"version": "1.23"
14+
}
915
},
1016
"mounts": [
11-
// "source=/Users/user/.aws,target=/home/vscode/.aws,type=bind,consistency=cached",
17+
"source=${localEnv:HOME}/.aws,target=/home/vscode/.aws,type=bind,consistency=cached",
18+
"source=${localEnv:HOME}/.gitconfig,target=/home/vscode/.gitconfig,type=bind,consistency=cached",
19+
"source=${localEnv:HOME}/.ssh,target=/home/vscode/.ssh,type=bind,consistency=cached"
1220
],
1321
"forwardPorts": [
1422
5050,

.github/workflows/build.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build
2+
on:
3+
push:
4+
branches:
5+
- '**'
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
13+
steps:
14+
- name: Install Docker Buildx
15+
uses: docker/setup-buildx-action@v2
16+
- name: Checkout Repo
17+
uses: actions/checkout@v4
18+
- name: Build Docker image
19+
working-directory: ./app
20+
run: |
21+
docker buildx build \
22+
--platform linux/amd64 \
23+
.

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.env
22
__pycache__
33
sentence-transformers
4-
.tmp/*
5-
!.tmp/prebuild.sh
4+
.tmp/
65
node_modules
76
venv

.vscode/launch.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
"env": {
1414
"FLASK_APP": "app.py",
1515
"FLASK_DEBUG": "1",
16+
"FLASK_ENV": "development",
1617
"SECRET_KEY": "supersecret",
18+
"REDIS_URL": "redis://redis:6379/0",
1719
"SESSION_COOKIE_SECURE": "0",
18-
"OPENAI_BASE_URL": "http://localhost:8080/api/v1",
19-
"OPENAI_API_KEY": "secret",
20-
"MODEL": "anthropic.claude-3-sonnet-20240229-v1:0"
20+
"OPENAI_BASE_URL": "http://127.0.0.1:5051/api/v1",
21+
"OPENAI_API_KEY": "",
22+
"MODEL": "default"
2123
},
2224
"args": [
2325
"run",

app/.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
myenv
2+
venv
23
.direnv
34
.envrc
45
__pycache__
5-
venv
66
sentence-transformers
7+
.tmp

app/.tmp/prebuild.sh

Lines changed: 0 additions & 31 deletions
This file was deleted.

app/Dockerfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ RUN pip install --no-cache-dir -r requirements.txt
3636
# Copy the application source code into the container
3737
COPY . /app
3838

39-
# Make the prebuild.sh script executable
40-
RUN chmod +x .tmp/prebuild.sh
41-
4239
# Expose port 5050 for the Flask application
4340
EXPOSE 5050
4441

app/data/knowledge_base.json

Lines changed: 587 additions & 203 deletions
Large diffs are not rendered by default.

app/get_knowledge_base.py

Lines changed: 43 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77

88
kb_file_path = './data/knowledge_base.json'
99

10-
def clean_tmp(dir_path):
11-
""" Clears out all contents of the specified directory except for prebuild.sh """
12-
for item in os.listdir(dir_path):
13-
item_path = os.path.join(dir_path, item)
14-
if item != "prebuild.sh": # Keep prebuild.sh
15-
if os.path.isdir(item_path):
16-
shutil.rmtree(item_path)
17-
else:
18-
os.remove(item_path)
19-
2010
def clone_repository(repo_url, local_dir):
2111
""" Clone or pull the repository based on its existence. """
2212
if not os.path.exists(local_dir):
@@ -30,73 +20,42 @@ def clone_repository(repo_url, local_dir):
3020
def setup_repositories():
3121
tmp_dir = ".tmp"
3222
os.makedirs(tmp_dir, exist_ok=True)
33-
clean_tmp(tmp_dir) # Clean the temporary directory before setting up
3423

3524
# Define repositories and their URLs
3625
repos = {
3726
"defang-docs": "https://github.com/DefangLabs/defang-docs.git",
38-
"defang": "https://github.com/DefangLabs/defang.git"
27+
"defang": "https://github.com/DefangLabs/defang.git",
28+
"samples": "https://github.com/DefangLabs/samples.git"
3929
}
4030

41-
# Change to the temporary directory
42-
original_dir = os.getcwd()
43-
os.chdir(tmp_dir)
44-
4531
# Clone each repository
4632
for repo_name, repo_url in repos.items():
47-
clone_repository(repo_url, repo_name)
48-
49-
# Return to the original directory
50-
os.chdir(original_dir)
33+
clone_repository(repo_url, os.path.join(tmp_dir, repo_name))
5134

5235
def run_prebuild_script():
53-
""" Run the 'prebuild.sh' script located in the .tmp directory. """
54-
os.chdir(".tmp")
55-
script_path = os.path.join("./", "prebuild.sh") # Ensure the path is correct
56-
if os.path.exists(script_path):
57-
print("Running prebuild.sh...")
58-
try:
59-
subprocess.run(["bash", script_path], check=True)
60-
except subprocess.CalledProcessError as e:
61-
print(f"Error running prebuild.sh: {e}")
62-
else:
63-
print("prebuild.sh not found.")
64-
65-
def cleanup():
66-
""" Clean up unneeded files, preserving only 'docs' and 'blog' directories """
67-
os.chdir("./defang-docs")
68-
for item in os.listdir('.'):
69-
if item not in ['docs', 'blog']: # Check if the item is not one of the directories to keep
70-
item_path = os.path.join('.', item) # Construct the full path
71-
if os.path.isdir(item_path):
72-
shutil.rmtree(item_path) # Remove the directory and all its contents
73-
else:
74-
os.remove(item_path) # Remove the file
75-
print("Cleanup completed successfully.")
36+
""" Run the defang-docs repo prebuild script"""
37+
38+
subprocess.run(
39+
["npm", "-C", ".tmp/defang-docs", "install"],
40+
check=True,
41+
stdout=subprocess.PIPE,
42+
stderr=subprocess.PIPE
43+
)
44+
45+
subprocess.run(
46+
["npm", "-C", ".tmp/defang-docs", "run", "prebuild"],
47+
check=True,
48+
stdout=subprocess.PIPE,
49+
stderr=subprocess.PIPE
50+
)
7651

7752
def parse_markdown():
7853
""" Parse markdown files in the current directory into JSON """
79-
reset_knowledge_base() # Reset the JSON database file
8054
recursive_parse_directory('./.tmp/defang-docs') # Parse markdown files in the current directory
8155
print("Markdown parsing completed successfully.")
8256

83-
def reset_knowledge_base():
84-
""" Resets or initializes the knowledge base JSON file. """
85-
with open(kb_file_path, 'w') as output_file:
86-
json.dump([], output_file)
87-
88-
def parse_markdown_file_to_json(file_path):
57+
def parse_markdown_file_to_json(json_output, current_id, file_path):
8958
""" Parses individual markdown file and adds its content to JSON """
90-
try:
91-
# Load existing content if the file exists
92-
with open(kb_file_path, 'r') as existing_file:
93-
json_output = json.load(existing_file)
94-
current_id = len(json_output) + 1 # Start ID from the next available number
95-
except (FileNotFoundError, json.JSONDecodeError):
96-
# If the file doesn't exist or is empty, start fresh
97-
json_output = []
98-
current_id = 1
99-
10059
with open(file_path, 'r', encoding='utf-8') as file:
10160
lines = file.readlines()
10261

@@ -147,28 +106,17 @@ def parse_markdown_file_to_json(file_path):
147106
"text": text,
148107
"path": adjust_knowledge_base_entry_path(file_path) # Adjust path format
149108
})
150-
current_id += 1
151-
152-
# Write the augmented JSON output to ./data/knowledge_base.json
153-
with open(kb_file_path, 'w', encoding='utf-8') as output_file:
154-
json.dump(json_output, output_file, indent=2, ensure_ascii=False)
155109

156110
def adjust_knowledge_base_entry_path(file_path):
157111
""" Adjusts the file path format for storage. """
158-
return re.sub(r'\/(\d{4})-(\d{2})-(\d{2})-', r'/\1/\2/\3/', file_path.replace("./.tmp/defang-docs", "").replace(".mdx", "").replace(".md", ""))
112+
return re.sub(r'\/(\d{4})-(\d{2})-(\d{2})-', r'/\1/\2/\3/', normalize_docs_path(file_path))
159113

160-
def parse_cli_markdown(file_path):
161-
""" Parses CLI-specific markdown files """
162-
try:
163-
# Load existing content if the file exists
164-
with open(kb_file_path, 'r') as existing_file:
165-
json_output = json.load(existing_file)
166-
current_id = len(json_output) + 1 # Start ID from the next available number
167-
except (FileNotFoundError, json.JSONDecodeError):
168-
# If the file doesn't exist or is empty, start fresh
169-
json_output = []
170-
current_id = 1
114+
def normalize_docs_path(path):
115+
""" Normalizes the file path to ensure consistent formatting. """
116+
return path.replace("./.tmp/defang-docs", "").replace(".mdx", "").replace(".md", "")
171117

118+
def parse_cli_markdown(json_output, current_id, file_path):
119+
""" Parses CLI-specific markdown files """
172120
with open(file_path, 'r', encoding='utf-8') as file:
173121
lines = file.readlines()
174122

@@ -189,32 +137,32 @@ def parse_cli_markdown(file_path):
189137
"id": current_id,
190138
"about": about,
191139
"text": text,
192-
"path": file_path.replace("./.tmp/defang-docs", "").replace(".mdx", "").replace(".md", "")
140+
"path": normalize_docs_path(file_path)
193141
})
194-
current_id += 1
195-
196-
# Write the augmented JSON output to data/knowledge_base.json
197-
with open(kb_file_path, 'w', encoding='utf-8') as output_file:
198-
json.dump(json_output, output_file, indent=2, ensure_ascii=False)
199142

200143
def recursive_parse_directory(root_dir):
201144
""" Recursively parses all markdown files in the directory. """
202-
for dirpath, dirnames, filenames in os.walk(root_dir):
145+
paths = []
146+
for dirpath, _dirnames, filenames in os.walk(root_dir):
203147
for filename in filenames:
204-
if filename.lower().endswith('.md') or filename.lower().endswith('.mdx'):
205-
file_path = os.path.join(dirpath, filename)
206-
if 'cli' in dirpath.lower() or 'cli' in filename.lower():
207-
parse_cli_markdown(file_path)
208-
else:
209-
parse_markdown_file_to_json(file_path)
148+
lower_filename = filename.lower()
149+
if lower_filename.endswith('.md') or lower_filename.endswith('.mdx'):
150+
paths.append(os.path.join(dirpath, filename))
151+
152+
with open(kb_file_path, 'r') as kb_file:
153+
kb_data = json.load(kb_file)
154+
155+
for id, file_path in enumerate(paths, start=1):
156+
if 'cli' in dirpath.lower() or 'cli' in filename.lower():
157+
parse_cli_markdown(kb_data, id, file_path)
158+
else:
159+
parse_markdown_file_to_json(kb_data, id, file_path)
160+
161+
with open(kb_file_path, 'w') as kb_file:
162+
json.dump(kb_data, kb_file, indent=2)
210163

211164
if __name__ == "__main__":
212165
setup_repositories()
213166
run_prebuild_script()
214-
cleanup()
215-
os.chdir('../../')
216-
print(os.listdir('.'))
217167
parse_markdown() # Start parsing logic after all setups
218-
print(os.listdir('.'))
219-
clean_tmp('./.tmp')
220168
print("All processes completed successfully.")

compose.dev.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ services:
88
environment:
99
ASK_TOKEN: asktoken
1010
FLASK_APP: app.py
11+
FLASK_ENV: development # Set Flask environment to development
12+
FLASK_DEBUG: 1 # Enable debug mode
13+
DEBUG: 1 # Ensure DEBUG is set to 1
1114
REBUILD_TOKEN: rebuildtoken
1215
SECRET_KEY: supersecret
1316
SEGMENT_WRITE_KEY: ${SEGMENT_WRITE_KEY} # Set your Segment write key here or in the .env file
@@ -46,7 +49,7 @@ services:
4649
target: /root/.aws
4750

4851
discord-bot:
49-
restart: unless-stopped
52+
restart: "no"
5053
extends:
5154
file: compose.yaml
5255
service: discord-bot

0 commit comments

Comments
 (0)