File size: 6,964 Bytes
360a82f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
"""
Advanced Sentiment Analysis System - Setup Script
==================================================

This script helps you set up the Advanced Sentiment Analysis System
with proper environment configuration and security measures.

Usage:
    python setup.py
"""

import os
import sys
import subprocess
import getpass
from pathlib import Path

def print_banner():
    """Print setup banner"""
    print("=" * 70)
    print("πŸš€ Advanced Sentiment Analysis System - Setup")
    print("=" * 70)
    print("Setting up your production-ready sentiment analysis platform...")
    print()

def check_python_version():
    """Check Python version compatibility"""
    if sys.version_info < (3, 8):
        print("❌ Python 3.8 or higher is required")
        print(f"   Current version: {sys.version}")
        sys.exit(1)
    print(f"βœ… Python version: {sys.version.split()[0]}")

def install_dependencies():
    """Install required dependencies"""
    print("\nπŸ“¦ Installing dependencies...")
    
    requirements = [
        "dspy-ai>=2.4.0",
        "openai>=1.0.0",
        "pandas>=1.5.0",
        "numpy>=1.21.0",
        "scikit-learn>=1.1.0",
        "matplotlib>=3.5.0",
        "seaborn>=0.11.0",
        "plotly>=5.0.0",
        "openpyxl>=3.0.0",
        "jupyter>=1.0.0",
        "ipykernel>=6.0.0",
        "ipywidgets>=7.0.0",
        "python-dotenv>=0.19.0",
        "requests>=2.28.0"
    ]
    
    for package in requirements:
        try:
            print(f"   Installing {package}...")
            subprocess.check_call([sys.executable, "-m", "pip", "install", package, "-q"])
        except subprocess.CalledProcessError:
            print(f"   ⚠️  Warning: Failed to install {package}")
    
    print("βœ… Dependencies installation completed")

def setup_environment():
    """Setup environment configuration"""
    print("\nπŸ”§ Setting up environment configuration...")
    
    env_file = Path(".env")
    
    if env_file.exists():
        print("   .env file already exists")
        overwrite = input("   Do you want to overwrite it? (y/N): ").lower()
        if overwrite != 'y':
            print("   Keeping existing .env file")
            return
    
    # Get OpenAI API key
    print("\nπŸ”‘ OpenAI API Key Configuration")
    print("   You need an OpenAI API key to use this system.")
    print("   Get one at: https://platform.openai.com/api-keys")
    
    api_key = getpass.getpass("   Enter your OpenAI API key (input hidden): ").strip()
    
    if not api_key:
        print("   ⚠️  No API key provided. You can set it later.")
        api_key = "your_openai_api_key_here"
    elif not api_key.startswith("sk-"):
        print("   ⚠️  Warning: API key should start with 'sk-'")
    
    # Create .env file
    env_content = f"""# Advanced Sentiment Analysis System - Environment Configuration
# Created by setup.py

# OpenAI API Configuration (REQUIRED)
OPENAI_API_KEY={api_key}

# Sentiment Analysis Thresholds (Optional - defaults provided)
SENTIMENT_CONFIDENCE_THRESHOLD=0.7
ESCALATION_RATE_THRESHOLD=0.15
PROCESSING_TIME_THRESHOLD=5.0
ERROR_RATE_THRESHOLD=0.05

# Production Configuration (Optional)
ENVIRONMENT=development
MAX_CONCURRENT_REQUESTS=100
RATE_LIMIT_REQUESTS_PER_MINUTE=1000
RATE_LIMIT_BURST_CAPACITY=50

# Monitoring Configuration (Optional)
METRICS_COLLECTION_ENABLED=true

# Logging Configuration (Optional)
LOG_LEVEL=INFO
LOG_FORMAT=json

# Cache Configuration (Optional)
CACHE_ENABLED=true
CACHE_TTL_SECONDS=300

# Performance Configuration (Optional)
BATCH_SIZE_DEFAULT=100
MAX_WORKERS_DEFAULT=10
PROCESSING_TIMEOUT_SECONDS=30
"""
    
    with open(".env", "w") as f:
        f.write(env_content)
    
    print("βœ… Environment configuration created (.env)")

def setup_jupyter():
    """Setup Jupyter environment"""
    print("\nπŸ“š Setting up Jupyter environment...")
    
    try:
        # Install Jupyter extensions
        subprocess.check_call([sys.executable, "-m", "pip", "install", "jupyter_contrib_nbextensions", "-q"])
        print("βœ… Jupyter extensions installed")
        
        # Enable widgets
        subprocess.check_call([sys.executable, "-m", "jupyter", "nbextension", "enable", "--py", "widgetsnbextension", "--sys-prefix"])
        print("βœ… Jupyter widgets enabled")
        
    except subprocess.CalledProcessError:
        print("   ⚠️  Warning: Some Jupyter setup steps failed")

def verify_installation():
    """Verify the installation"""
    print("\nπŸ” Verifying installation...")
    
    try:
        # Test imports
        import pandas
        import numpy
        import matplotlib
        import seaborn
        import plotly
        import sklearn
        import dspy
        import openai
        
        print("βœ… All core packages imported successfully")
        
        # Check environment
        if os.path.exists(".env"):
            print("βœ… Environment configuration found")
        else:
            print("⚠️  Environment configuration not found")
        
        # Check notebook
        if os.path.exists("advanced_sentiment_analysis.ipynb"):
            print("βœ… Main notebook found")
        else:
            print("⚠️  Main notebook not found")
        
    except ImportError as e:
        print(f"❌ Import error: {e}")
        return False
    
    return True

def print_next_steps():
    """Print next steps for the user"""
    print("\n" + "=" * 70)
    print("πŸŽ‰ Setup completed successfully!")
    print("=" * 70)
    print("\nπŸ“‹ Next Steps:")
    print("   1. Start Jupyter Notebook:")
    print("      jupyter notebook advanced_sentiment_analysis.ipynb")
    print("\n   2. Run all cells in the notebook to initialize the system")
    print("\n   3. The system will automatically use your configured API key")
    print("\n   4. Check the README.md for detailed usage examples")
    print("\nπŸ”— Useful Links:")
    print("   β€’ Documentation: README.md")
    print("   β€’ Contributing: CONTRIBUTING.md")
    print("   β€’ License: LICENSE")
    print("\nπŸ’‘ Tips:")
    print("   β€’ Keep your .env file secure and never commit it to git")
    print("   β€’ Monitor your OpenAI API usage at platform.openai.com")
    print("   β€’ Check CHANGELOG.md for updates and new features")
    print("\n" + "=" * 70)

def main():
    """Main setup function"""
    try:
        print_banner()
        check_python_version()
        install_dependencies()
        setup_environment()
        setup_jupyter()
        
        if verify_installation():
            print_next_steps()
        else:
            print("\n❌ Setup completed with errors. Please check the output above.")
            sys.exit(1)
            
    except KeyboardInterrupt:
        print("\n\n⚠️  Setup interrupted by user")
        sys.exit(1)
    except Exception as e:
        print(f"\n❌ Setup failed with error: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()