-
Notifications
You must be signed in to change notification settings - Fork 0
Improvement #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Improvement #19
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1494e6d
bfs and better run script
RespectMathias 46b5298
fixed
RespectMathias d542002
Update launch.json
RespectMathias f105731
Update README.md
RespectMathias 992c7c0
Update README.md
RespectMathias 06a590c
Update run.py
RespectMathias b102d80
Automation
RespectMathias 2bd8992
Fix check
RespectMathias 51c11e4
Fix favicon
RespectMathias d4f0383
Fix cbf IO
RespectMathias fa196e3
bfs and better run script
RespectMathias d3ec8c5
fixed
RespectMathias 38bac1f
Update launch.json
RespectMathias 9982a26
Update README.md
RespectMathias f655eb0
Update README.md
RespectMathias 63e42ce
Update run.py
RespectMathias 431d4b5
Automation
RespectMathias e6d5cea
Fix check
RespectMathias 068def6
Fix favicon
RespectMathias 160a7c9
Fix cbf IO
RespectMathias 14713d5
Rebase and use django auth
RespectMathias c9e04d0
Fix styling
RespectMathias 123253e
Merge branch 'improvement' of https://github.com/SP-SDU/Undershelf inβ¦
RespectMathias 6291e4d
Admin panel button
RespectMathias eb840a7
Add missing tests
RespectMathias c3ff55a
Update src/presentation/templates/base.html
RespectMathias 65f23ca
Resolve review issues
RespectMathias 3aef881
Update src/presentation/templates/book_details.html
RespectMathias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| import argparse | ||
| import os | ||
| import platform | ||
| import subprocess | ||
| import urllib.request | ||
| import venv | ||
| import zipfile | ||
|
|
||
|
|
||
| def run_command(command, description=None): | ||
| if description: | ||
| print(f"\033[92m{description}...\033[0m") | ||
|
|
||
| process = subprocess.Popen( | ||
| command, | ||
| shell=True, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, | ||
| universal_newlines=True, | ||
| ) | ||
|
|
||
| if process.stdout: | ||
| for line in process.stdout: | ||
| print(line, end="") | ||
|
|
||
| process.wait() | ||
| if process.returncode != 0: | ||
| print(f"\033[91mCommand failed with return code {process.returncode}\033[0m") | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def get_venv_python_path(): | ||
| if platform.system() == "Windows": | ||
| python_exe = os.path.join(os.getcwd(), ".venv", "Scripts", "python.exe") | ||
| else: | ||
| python_exe = os.path.join(os.getcwd(), ".venv", "bin", "python") | ||
| return python_exe | ||
|
|
||
|
|
||
| def create_venv_if_not_exists(): | ||
| if not os.path.exists(".venv"): | ||
| print("\033[92mCreating virtual environment...\033[0m") | ||
| venv.create(".venv", with_pip=True) | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def parse_arguments(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--setup-only", | ||
| action="store_true", | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def create_superuser(python_exe): | ||
| print("\033[92mCreating superuser if needed...\033[0m") | ||
|
|
||
| os.environ.setdefault("DJANGO_SUPERUSER_USERNAME", "root") | ||
| os.environ.setdefault("DJANGO_SUPERUSER_PASSWORD", "root") | ||
| os.environ.setdefault("DJANGO_SUPERUSER_EMAIL", "root@undershelf.com") | ||
|
|
||
| run_command( | ||
| f"{python_exe} src/manage.py createsuperuser --noinput", "Creating superuser" | ||
| ) | ||
|
|
||
|
|
||
| def check_if_data_exists(python_exe): | ||
| check_script = ( | ||
| "import os; " | ||
| "os.environ.setdefault('DJANGO_SETTINGS_MODULE','config.settings'); " | ||
| "import django; django.setup(); " | ||
| "from data_access.models import Book; " | ||
| "print(1 if Book.objects.count()>0 else 0)" | ||
| ) | ||
|
|
||
| try: | ||
| result = subprocess.run( | ||
| [python_exe, "-c", check_script], | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=os.path.join(os.getcwd(), "src"), | ||
| ) | ||
|
|
||
| return result.returncode == 0 and result.stdout.strip() == "1" | ||
| except Exception as e: | ||
| print(f"\033[93mWarning: Could not check database: {e}\033[0m") | ||
| return False | ||
|
|
||
|
|
||
| def download_or_get_csv(): | ||
| csv_path = os.path.join("src", "data_access", "merged_dataframe.csv") | ||
|
|
||
| if os.path.exists(csv_path): | ||
| print("\033[92mCSV file found.\033[0m") | ||
| return csv_path | ||
|
|
||
| print("\033[93mCSV file not found. Downloading...\033[0m") | ||
| url = "https://drive.usercontent.google.com/download?id=1MVRHs_CwKTBR2Rpakx920f277IcJ0q6X&authuser=0&confirm=t" | ||
| zip_path = os.path.join("src", "data_access", "merged_dataframe.zip") | ||
|
|
||
| try: | ||
| os.makedirs(os.path.dirname(csv_path), exist_ok=True) | ||
|
|
||
| # Download the ZIP file | ||
| print("\033[92mDownloading ZIP file...\033[0m") | ||
| with urllib.request.urlopen(url) as response: | ||
| if response.getcode() != 200: | ||
| raise Exception(f"HTTP error: {response.getcode()}") | ||
|
|
||
| with open(zip_path, "wb") as file: | ||
| chunk_size = 8192 | ||
| while True: | ||
| chunk = response.read(chunk_size) | ||
| if not chunk: | ||
| break | ||
| file.write(chunk) | ||
|
|
||
| # Extract the ZIP file | ||
| print("\033[92mExtracting ZIP file...\033[0m") | ||
| data_access_dir = os.path.join("src", "data_access") | ||
| with zipfile.ZipFile(zip_path, "r") as zip_ref: | ||
| zip_ref.extractall(data_access_dir) | ||
|
|
||
| # Remove the ZIP file after extraction | ||
| os.remove(zip_path) | ||
|
|
||
| if os.path.exists(csv_path): | ||
| print("\033[92mCSV file extracted successfully.\033[0m") | ||
| return csv_path | ||
| else: | ||
| print("\033[91mNo CSV file found in the extracted ZIP.\033[0m") | ||
| return None | ||
|
|
||
| except Exception as e: | ||
| print(f"\033[91mError processing file: {e}\033[0m") | ||
| return None | ||
|
|
||
|
|
||
| def main(): | ||
| create_venv_if_not_exists() | ||
| python_exe = get_venv_python_path() | ||
|
|
||
| run_command( | ||
| f"{python_exe} -m pip install -r requirements.txt", | ||
| "Installing requirements", | ||
| ) | ||
| run_command( | ||
| f"{python_exe} src/manage.py makemigrations", | ||
| "Making migrations", | ||
| ) | ||
| run_command(f"{python_exe} src/manage.py migrate", "Running migrations") | ||
|
|
||
| create_superuser(python_exe) | ||
|
|
||
| print("\033[92mChecking if data needs to be imported...\033[0m") | ||
| if not check_if_data_exists(python_exe): | ||
| csv_path = download_or_get_csv() | ||
| if not csv_path: | ||
| print("\033[91mFailed to get CSV file. Exiting.\033[0m") | ||
| exit(1) | ||
| run_command( | ||
| f"{python_exe} src/manage.py import_data {csv_path}", | ||
| "Importing data from CSV", | ||
| ) | ||
| else: | ||
| print("\033[92mData already exists in the database. Skipping import.\033[0m") | ||
|
|
||
| args = parse_arguments() | ||
| if args.setup_only: | ||
| print("\033[92mSetup complete.\033[0m") | ||
| else: | ||
| print("\033[92mStarting development server...\033[0m") | ||
| print("\033[93mPress Ctrl+C to stop the server\033[0m") | ||
| subprocess.call([python_exe, "src/manage.py", "runserver"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| main() | ||
| except KeyboardInterrupt: | ||
| print("\n\033[93mInterrupted. Exiting.\033[0m") | ||
| except Exception as e: | ||
| print(f"\n\033[91mAn error occurred: {e}\033[0m") | ||
| exit(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from collections import defaultdict, deque | ||
| from typing import List | ||
|
|
||
| from data_access.models import Book | ||
|
|
||
|
|
||
| class GraphRecommender: | ||
| """ | ||
| Graph-based recommender using BFS. | ||
| Nodes: books; edges: same author or same category. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def get_recommendations( | ||
| start_book_id: str, max_depth: int = 2, max_results: int = 10 | ||
| ) -> List[Book]: | ||
| # Build feature maps: author -> books, category -> books | ||
| author_map = defaultdict(set) | ||
| category_map = defaultdict(set) | ||
|
|
||
| all_books = list(Book.objects.only("id", "authors", "categories")) # O(n) | ||
| for b in all_books: # O(n * f) | ||
| if b.authors: | ||
| for author in b.authors.split(","): | ||
| author_map[author.strip().lower()].add(b.id) | ||
| if b.categories: | ||
| for cat in b.categories.split(","): | ||
| category_map[cat.strip().lower()].add(b.id) | ||
|
|
||
| visited = set([start_book_id]) | ||
| queue = deque([(start_book_id, 0)]) | ||
| recommendations = [] | ||
|
|
||
| while queue and len(recommendations) < max_results: | ||
| current_id, depth = queue.popleft() | ||
| if depth >= max_depth: | ||
| continue | ||
|
|
||
| # neighbors by author | ||
| b = Book.objects.only("id", "authors", "categories").get(pk=current_id) | ||
|
RespectMathias marked this conversation as resolved.
|
||
| neighbors = set() | ||
| if b.authors: | ||
| for author in b.authors.split(","): | ||
| neighbors |= author_map[author.strip().lower()] | ||
| if b.categories: | ||
| for cat in b.categories.split(","): | ||
|
RespectMathias marked this conversation as resolved.
|
||
| neighbors |= category_map[cat.strip().lower()] | ||
|
|
||
| for nb_id in neighbors: | ||
| if nb_id not in visited: | ||
| visited.add(nb_id) | ||
| queue.append((nb_id, depth + 1)) | ||
| if nb_id != start_book_id: | ||
| recommendations.append(nb_id) | ||
| if len(recommendations) >= max_results: | ||
| break | ||
|
|
||
| # Fetch Book instances preserving order | ||
| books = list(Book.objects.filter(id__in=recommendations)) | ||
| # maintain recommendation order | ||
| id_to_book = {book.id: book for book in books} | ||
| ordered = [id_to_book[rid] for rid in recommendations if rid in id_to_book] | ||
| return ordered | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.