Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| Startup Script for Iran Legal Dashboard | |
| ====================================== | |
| Ensures proper initialization and debugging | |
| """ | |
| import os | |
| import sys | |
| import sqlite3 | |
| import logging | |
| from pathlib import Path | |
| # Setup logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(levelname)s - %(message)s' | |
| ) | |
| logger = logging.getLogger(__name__) | |
| def check_environment(): | |
| """Check if running environment is properly configured""" | |
| logger.info("🔍 Checking environment...") | |
| # Check current directory | |
| cwd = os.getcwd() | |
| logger.info(f"📁 Current working directory: {cwd}") | |
| # Check for required files | |
| required_files = ['app.py', 'requirements.txt'] | |
| src_required_files = ['src/streamlit_app.py'] if os.path.exists('src') else [] | |
| all_required = required_files + src_required_files | |
| missing_files = [] | |
| for file in all_required: | |
| if not os.path.exists(file): | |
| missing_files.append(file) | |
| if missing_files: | |
| logger.error(f"❌ Missing required files: {missing_files}") | |
| return False | |
| logger.info("✅ All required files found") | |
| return True | |
| def setup_database(): | |
| """Setup database with proper permissions""" | |
| logger.info("🗄️ Setting up database...") | |
| # Try different database paths | |
| db_paths = [ | |
| "/tmp/iran_legal.db", | |
| "./iran_legal.db", | |
| "data/iran_legal.db" | |
| ] | |
| for db_path in db_paths: | |
| try: | |
| # Ensure directory exists | |
| db_dir = os.path.dirname(db_path) | |
| if db_dir and not os.path.exists(db_dir): | |
| os.makedirs(db_dir, exist_ok=True) | |
| # Test database connection | |
| with sqlite3.connect(db_path, timeout=10) as conn: | |
| conn.execute("SELECT 1") | |
| logger.info(f"✅ Database accessible at: {db_path}") | |
| return db_path | |
| except Exception as e: | |
| logger.warning(f"⚠️ Cannot access {db_path}: {e}") | |
| continue | |
| logger.error("❌ No accessible database path found") | |
| return None | |
| def check_permissions(): | |
| """Check write permissions for critical directories""" | |
| logger.info("🔐 Checking permissions...") | |
| test_dirs = ["/tmp", ".", "./data", "./src"] | |
| results = {} | |
| for test_dir in test_dirs: | |
| if os.path.exists(test_dir): | |
| try: | |
| test_file = os.path.join(test_dir, "test_write.tmp") | |
| with open(test_file, 'w') as f: | |
| f.write("test") | |
| os.remove(test_file) | |
| results[test_dir] = True | |
| logger.info(f"✅ {test_dir} - Writable") | |
| except Exception as e: | |
| results[test_dir] = False | |
| logger.warning(f"❌ {test_dir} - Not writable: {e}") | |
| else: | |
| results[test_dir] = None | |
| logger.info(f"ℹ️ {test_dir} - Does not exist") | |
| return results | |
| def verify_imports(): | |
| """Verify all required imports work""" | |
| logger.info("📦 Verifying imports...") | |
| required_modules = [ | |
| 'streamlit', | |
| 'pandas', | |
| 'plotly', | |
| 'requests', | |
| 'bs4', | |
| 'sqlite3', | |
| 'json', | |
| 'hashlib', | |
| 'datetime' | |
| ] | |
| failed_imports = [] | |
| for module in required_modules: | |
| try: | |
| __import__(module) | |
| logger.info(f"✅ {module} - OK") | |
| except ImportError as e: | |
| failed_imports.append(module) | |
| logger.error(f"❌ {module} - Failed: {e}") | |
| if failed_imports: | |
| logger.error(f"❌ Failed imports: {failed_imports}") | |
| logger.info("💡 Run: pip install -r requirements.txt") | |
| return False | |
| return True | |
| def create_sample_data(db_path): | |
| """Create sample data for testing""" | |
| logger.info("📝 Creating sample data...") | |
| try: | |
| with sqlite3.connect(db_path) as conn: | |
| # Check if data already exists | |
| cursor = conn.execute("SELECT COUNT(*) FROM documents") | |
| doc_count = cursor.fetchone()[0] | |
| if doc_count > 0: | |
| logger.info(f"ℹ️ Database already has {doc_count} documents") | |
| return True | |
| # Create sample document | |
| conn.execute(""" | |
| INSERT INTO documents (title, content, source, category, ai_score, keywords, file_size, language) | |
| VALUES (?, ?, ?, ?, ?, ?, ?, ?) | |
| """, ( | |
| "نمونه سند حقوقی", | |
| "این یک نمونه سند حقوقی برای تست سیستم است که شامل محتوای آزمایشی میباشد.", | |
| "سیستم تست", | |
| "قانون", | |
| 0.75, | |
| '["نمونه", "حقوق", "تست"]', | |
| 150, | |
| "fa" | |
| )) | |
| # Create sample scraped item | |
| conn.execute(""" | |
| INSERT INTO scraped_items (id, url, title, content, domain, rating_score, word_count, status) | |
| VALUES (?, ?, ?, ?, ?, ?, ?, ?) | |
| """, ( | |
| "sample_001", | |
| "https://dastour.ir/sample", | |
| "نمونه محتوای حقوقی", | |
| "این یک نمونه محتوای حقوقی جمعآوری شده از وب است.", | |
| "dastour.ir", | |
| 0.80, | |
| 25, | |
| "completed" | |
| )) | |
| conn.commit() | |
| logger.info("✅ Sample data created successfully") | |
| return True | |
| except Exception as e: | |
| logger.error(f"❌ Failed to create sample data: {e}") | |
| return False | |
| def main(): | |
| """Main startup function""" | |
| logger.info("🚀 Starting Iran Legal Dashboard initialization...") | |
| # Check environment | |
| if not check_environment(): | |
| logger.error("❌ Environment check failed") | |
| return False | |
| # Check permissions | |
| check_permissions() | |
| # Verify imports | |
| if not verify_imports(): | |
| logger.error("❌ Import verification failed") | |
| return False | |
| # Setup database | |
| db_path = setup_database() | |
| if not db_path: | |
| logger.error("❌ Database setup failed") | |
| return False | |
| # Create sample data | |
| create_sample_data(db_path) | |
| logger.info("✅ Initialization completed successfully!") | |
| logger.info("🎉 Ready to run: streamlit run app.py") | |
| return True | |
| if __name__ == "__main__": | |
| success = main() | |
| if not success: | |
| sys.exit(1) |