Skip to content

Commit

Permalink
Merge pull request #175 from wizaye/wizaye
Browse files Browse the repository at this point in the history
[FEATURE]: Add Custom Loader/Spinner for Text-to-Speech CLI Input
  • Loading branch information
suryanshsk authored Oct 12, 2024
2 parents fee75cf + aa55dde commit e7735ea
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 102 deletions.
113 changes: 113 additions & 0 deletions cli_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import os
import subprocess
import sys
import platform
from utils.loader import custom_progress_bar, loading_spinner
from colorama import init, Fore, Style
from utils.cli.cli_art import display_art

# Initialize colorama for colored console output
init(autoreset=True)

def get_venv_executable(executable):
"""
Returns the path to the executable within the virtual environment.
"""
venv_path = os.path.join("venv", "Scripts" if platform.system() == "Windows" else "bin", executable)
return venv_path

def install_dependencies():
"""
Installs the required dependencies from the 'requirements.txt' file using pip.
Displays a progress bar and a success/failure message after installation.
"""
task_name = "Installing dependencies"
total_steps = 500
step_duration = 0.02

custom_progress_bar(task_name, total_steps, step_duration, color=Fore.GREEN)

pip_path = get_venv_executable("pip")
try:
subprocess.check_call([pip_path, "install", "-r", "requirements.txt"])
print(f"\n{Fore.GREEN}✓ Successfully installed dependencies!{Style.RESET_ALL}")
except subprocess.CalledProcessError as e:
print(f"\n{Fore.RED}✗ Failed to install dependencies!{Style.RESET_ALL}")
sys.exit(1)

def install_pipreqs():
"""
Installs the 'pipreqs' package, which is used to generate the 'requirements.txt' file.
Displays a progress bar and a success/failure message after installation.
"""
task_name = "Installing pipreqs (tool for generating requirements.txt)"
total_steps = 500
step_duration = 0.02

custom_progress_bar(task_name, total_steps, step_duration, color=Fore.YELLOW)

pip_path = get_venv_executable("pip")
try:
subprocess.check_call([pip_path, "install", "pipreqs"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print(f"\n{Fore.GREEN}✓ Successfully installed pipreqs!{Style.RESET_ALL}")
except subprocess.CalledProcessError as e:
print(f"\n{Fore.RED}✗ Failed to install pipreqs!{Style.RESET_ALL}")
sys.exit(1)

def update_requirements():
"""
Updates the 'requirements.txt' file using pipreqs, ensuring all installed
packages are recorded. Displays a progress bar and a success/failure message.
"""
task_name = "Updating requirements.txt"
total_steps = 500
step_duration = 0.02

custom_progress_bar(task_name, total_steps, step_duration, color=Fore.YELLOW)

pipreqs_path = get_venv_executable("pipreqs")
try:
subprocess.check_call([pipreqs_path, ".", "--force"])
print(f"\n{Fore.GREEN}✓ Successfully updated requirements.txt!{Style.RESET_ALL}")
except subprocess.CalledProcessError as e:
print(f"\n{Fore.RED}✗ Failed to update requirements.txt!{Style.RESET_ALL}")
sys.exit(1)

def run_application():
"""
Runs the main application script 'main_assistant.py'.
Displays a progress bar during the startup process and a success/failure message after.
"""
task_name = "Launching the Assistant"
total_steps = 500
step_duration = 0.05

custom_progress_bar(task_name, total_steps, step_duration, color=Fore.CYAN)

python_path = get_venv_executable("python")
try:
subprocess.call([python_path, "main_assistant.py"])
print(f"{Fore.GREEN}✓ Assistant launched successfully!{Style.RESET_ALL}\n")
except Exception as e:
print(f"\n{Fore.RED}✗ Failed to launch the assistant!{Style.RESET_ALL}")
sys.exit(1)

def main():
"""
Main function that orchestrates the setup process for the Python Voice Assistant.
It installs dependencies, installs pipreqs, updates 'requirements.txt', and runs the application.
"""
print("\nWelcome to the Python Voice Assistant setup! We are setting up everything for you.")
print("No coding knowledge needed. Just follow the progress below!\n")

display_art()
# Use loading spinner for initialization
loading_spinner(2, Fore.CYAN, "Initializing setup environment...")
# update_requirements()
install_dependencies()
install_pipreqs()
# update_requirements()
run_application()

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion setup.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if "%VIRTUAL_ENV%" NEQ "" (
echo Virtual environment activated.

:: Install the required packages
:: pip install -r requirements.txt
pip install progressbar2 colorama
:: Run the main application
python setup.py
) else (
Expand Down
99 changes: 0 additions & 99 deletions setup.py

This file was deleted.

4 changes: 2 additions & 2 deletions setup.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ source venv/bin/activate
# Check if the activation was successful
if [ "$VIRTUAL_ENV" != "" ]; then
echo "Virtual environment activated."

pip install progressbar2 colorama
# Run the main application
python setup.py
python cli_setup.py
else
echo "Failed to activate virtual environment."
fi
11 changes: 11 additions & 0 deletions utils/cli/cli_art.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def display_art():
cli_art = r"""
╔╗ ╔╗ ╔═══╗ ╔╗ ╔╗
║╚╗╔╝║ ║╔═╗║ ╔╝╚╗ ╔╝╚╗
╚╗║║╔╝╔══╗╔╗╔══╗╔══╗ ║║ ║║╔══╗╔══╗╔╗╔══╗╚╗╔╝╔══╗ ╔═╗ ╚╗╔╝
║╚╝║ ║╔╗║╠╣║╔═╝║╔╗║ ║╚═╝║║══╣║══╣╠╣║══╣ ║║ ╚ ╗║ ║╔╗╗ ║║
╚╗╔╝ ║╚╝║║║║╚═╗║║═╣ ║╔═╗║╠══║╠══║║║╠══║ ║╚╗║╚╝╚╗║║║║ ║╚╗
╚╝ ╚══╝╚╝╚══╝╚══╝ ╚╝ ╚╝╚══╝╚══╝╚╝╚══╝ ╚═╝╚═══╝╚╝╚╝ ╚═╝
"""
print(cli_art)
38 changes: 38 additions & 0 deletions utils/loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import time
import sys
import progressbar
from colorama import Style,Fore


def custom_progress_bar(task_name, total_steps, step_duration, color):
"""
Displays a colored progress bar using progressbar with a customizable number of total steps and step duration.
"""
colored_task_name = f"{color}{task_name}{Style.RESET_ALL}"

# Initialize the progress bar with solid blocks
bar = progressbar.ProgressBar(max_value=total_steps, widgets=[
colored_task_name,
' ', progressbar.Percentage(),
' ', progressbar.Bar('█', '░'), # Use solid blocks for the filled part and light blocks for the empty part
' ', progressbar.ETA(),

]).start()

for i in range(total_steps):
time.sleep(step_duration) # Simulate work being done
bar.update(i + 1) # Update the progress bar

def loading_spinner(duration, color, message):
"""
Displays a spinner without a progress bar, using only the spinner symbols and message.
"""
spinner = ['-', '\\', '|', '/']
end_time = time.time() + duration
while time.time() < end_time:
for frame in spinner:
sys.stdout.write(f'\r{color}{message} {frame} {Style.RESET_ALL}')
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write(f'\n')
sys.stdout.flush()

0 comments on commit e7735ea

Please sign in to comment.