Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
188 lines (156 loc) Β· 6.21 KB
/
Copy pathsetup.py
File metadata and controls
188 lines (156 loc) Β· 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
"""
Setup script for Swedish Voice Chatbot
Automatically installs dependencies and sets up the environment
"""
import subprocess
import sys
import os
import platform
def run_command(command, description):
"""Run a command and handle errors"""
print(f"π {description}...")
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
print(f"β
{description} completed successfully!")
return True
except subprocess.CalledProcessError as e:
print(f"β Error during {description}:")
print(f"Command: {command}")
print(f"Error: {e.stderr}")
return False
def check_python_version():
"""Check if Python version is compatible"""
version = sys.version_info
if version.major < 3 or (version.major == 3 and version.minor < 8):
print("β Python 3.8 or higher is required!")
print(f"Current version: {version.major}.{version.minor}")
return False
print(f"β
Python version {version.major}.{version.minor} is compatible")
return True
def install_system_dependencies():
"""Install system-level dependencies based on OS"""
system = platform.system().lower()
if system == "linux":
print("π§ Detected Linux system")
# Install audio dependencies for Linux
commands = [
"sudo apt-get update",
"sudo apt-get install -y portaudio19-dev python3-pyaudio ffmpeg mpg123 alsa-utils",
]
for cmd in commands:
if not run_command(cmd, f"Installing Linux dependencies: {cmd.split()[-1]}"):
print("β οΈ Some system dependencies might not be installed. Audio might not work properly.")
elif system == "darwin": # macOS
print("π Detected macOS system")
# Check if Homebrew is installed
if subprocess.run("which brew", shell=True, capture_output=True).returncode == 0:
commands = [
"brew install portaudio ffmpeg mpg123",
]
for cmd in commands:
run_command(cmd, f"Installing macOS dependencies")
else:
print("β οΈ Homebrew not found. Please install Homebrew first: https://brew.sh/")
elif system == "windows":
print("πͺ Detected Windows system")
print("β οΈ On Windows, you might need to install additional audio codecs manually")
print("Consider installing: https://www.codecguide.com/download_kl.htm")
def create_virtual_environment():
"""Create and activate virtual environment"""
venv_name = "chatbot_env"
if os.path.exists(venv_name):
print(f"β
Virtual environment '{venv_name}' already exists")
return True
if run_command(f"python3 -m venv {venv_name}", "Creating virtual environment"):
print(f"β
Virtual environment '{venv_name}' created successfully!")
print(f"π To activate it manually:")
if platform.system().lower() == "windows":
print(f" {venv_name}\\Scripts\\activate")
else:
print(f" source {venv_name}/bin/activate")
return True
return False
def install_python_dependencies():
"""Install Python dependencies from requirements.txt"""
# Check if we're in a virtual environment
in_venv = hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)
if not in_venv:
print("β οΈ Not running in a virtual environment!")
print("It's recommended to use a virtual environment to avoid conflicts.")
response = input("Continue anyway? (y/N): ").lower()
if response != 'y':
return False
# Install pip packages
commands = [
"pip install --upgrade pip",
"pip install -r requirements.txt",
]
for cmd in commands:
if not run_command(cmd, f"Running: {cmd}"):
return False
return True
def test_imports():
"""Test if all required packages can be imported"""
print("π§ͺ Testing package imports...")
required_packages = [
"speech_recognition",
"vosk",
"gtts",
"gradio",
"transformers",
"torch",
"sounddevice"
]
failed_imports = []
for package in required_packages:
try:
__import__(package)
print(f" β
{package}")
except ImportError as e:
print(f" β {package}: {e}")
failed_imports.append(package)
if failed_imports:
print(f"\nβ Failed to import: {', '.join(failed_imports)}")
print("Try running: pip install -r requirements.txt")
return False
print("β
All packages imported successfully!")
return True
def main():
"""Main setup function"""
print("πΈπͺ Swedish Voice Chatbot Setup")
print("=" * 40)
# Check Python version
if not check_python_version():
return False
# Install system dependencies
print("\nπ¦ Installing system dependencies...")
install_system_dependencies()
# Create virtual environment
print("\nπ Setting up virtual environment...")
create_virtual_environment()
# Install Python dependencies
print("\nπ Installing Python dependencies...")
if not install_python_dependencies():
print("β Failed to install Python dependencies!")
return False
# Test imports
print("\nπ§ͺ Testing installation...")
if not test_imports():
print("β Some packages failed to import!")
return False
print("\nπ Setup completed successfully!")
print("\nπ Next steps:")
print("1. Run the chatbot:")
print(" python swedish_chatbot.py")
print("\n2. Open your browser to the displayed URL")
print("3. Click the microphone button and start talking in Swedish!")
print("\nπ‘ Tips:")
print("- Make sure your microphone is working")
print("- The first run will download AI models (may take time)")
print("- Speak clearly in Swedish")
print("- Internet connection needed for text-to-speech")
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)