Spaces:
Sleeping
Sleeping
Fix: Resolve 0-entity extraction error by adding NVIDIA LLM integration and robust error handling
feca495 | import os | |
| from pathlib import Path | |
| from datetime import timedelta | |
| import dotenv | |
| # Build paths inside the project like this: BASE_DIR / 'subdir'. | |
| BASE_DIR = Path(__file__).resolve().parent.parent | |
| # Load environment variables from parent directory .env | |
| ENV_PATH = BASE_DIR.parent / '.env' | |
| if ENV_PATH.exists(): | |
| dotenv.load_dotenv(ENV_PATH) | |
| # Security settings | |
| SECRET_KEY = os.getenv('SECRET_KEY') | |
| if not SECRET_KEY: | |
| raise ValueError( | |
| "SECRET_KEY environment variable is required. " | |
| "Generate one with: python -c \"from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())\"" | |
| ) | |
| DEBUG = os.getenv('DEBUG', 'False') == 'True' | |
| ALLOWED_HOSTS = [h.strip() for h in os.getenv('ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',') if h.strip()] | |
| # Production security hardening (only when DEBUG=False) | |
| if not DEBUG: | |
| SECURE_BROWSER_XSS_FILTER = True | |
| SECURE_CONTENT_TYPE_NOSNIFF = True | |
| SESSION_COOKIE_SECURE = True | |
| CSRF_COOKIE_SECURE = True | |
| X_FRAME_OPTIONS = 'DENY' | |
| SECURE_SSL_REDIRECT = False # Set True if behind TLS termination proxy | |
| SECURE_HSTS_SECONDS = 0 # Set to 31536000 once HTTPS is confirmed | |
| SECURE_HSTS_INCLUDE_SUBDOMAINS = False | |
| SECURE_HSTS_PRELOAD = False | |
| # Application definition | |
| INSTALLED_APPS = [ | |
| 'django.contrib.admin', | |
| 'django.contrib.auth', | |
| 'django.contrib.contenttypes', | |
| 'django.contrib.sessions', | |
| 'django.contrib.messages', | |
| 'django.contrib.staticfiles', | |
| # Third party apps | |
| 'rest_framework', | |
| 'rest_framework_simplejwt.token_blacklist', | |
| 'corsheaders', | |
| # Custom apps | |
| 'graphrag', | |
| ] | |
| MIDDLEWARE = [ | |
| 'corsheaders.middleware.CorsMiddleware', # Must be placed high up | |
| 'django.middleware.security.SecurityMiddleware', | |
| 'django.contrib.sessions.middleware.SessionMiddleware', | |
| 'django.middleware.common.CommonMiddleware', | |
| 'django.middleware.csrf.CsrfViewMiddleware', | |
| 'django.contrib.auth.middleware.AuthenticationMiddleware', | |
| 'django.contrib.messages.middleware.MessageMiddleware', | |
| 'django.middleware.clickjacking.XFrameOptionsMiddleware', | |
| ] | |
| ROOT_URLCONF = 'config.urls' | |
| TEMPLATES = [ | |
| { | |
| 'BACKEND': 'django.template.backends.django.DjangoTemplates', | |
| 'DIRS': [], | |
| 'APP_DIRS': True, | |
| 'OPTIONS': { | |
| 'context_processors': [ | |
| 'django.template.context_processors.debug', | |
| 'django.template.context_processors.request', | |
| 'django.contrib.auth.context_processors.auth', | |
| 'django.contrib.messages.context_processors.messages', | |
| ], | |
| }, | |
| }, | |
| ] | |
| WSGI_APPLICATION = 'config.wsgi.application' | |
| # Database configuration | |
| # Supports: sqlite (default) or postgres | |
| DB_ENGINE = os.getenv('DB_ENGINE', 'sqlite') | |
| if DB_ENGINE == 'postgres': | |
| DATABASES = { | |
| 'default': { | |
| 'ENGINE': 'django.db.backends.postgresql', | |
| 'NAME': os.getenv('DB_NAME', 'graphrag'), | |
| 'USER': os.getenv('DB_USER', 'postgres'), | |
| 'PASSWORD': os.getenv('DB_PASSWORD', ''), | |
| 'HOST': os.getenv('DB_HOST', 'localhost'), | |
| 'PORT': os.getenv('DB_PORT', '5432'), | |
| } | |
| } | |
| else: | |
| DATABASES = { | |
| 'default': { | |
| 'ENGINE': 'django.db.backends.sqlite3', | |
| 'NAME': BASE_DIR / 'db' / 'db.sqlite3', | |
| } | |
| } | |
| # Custom User Model | |
| AUTH_USER_MODEL = 'graphrag.User' | |
| AUTHENTICATION_BACKENDS = [ | |
| 'graphrag.views.EmailOrUsernameModelBackend', | |
| 'django.contrib.auth.backends.ModelBackend', | |
| ] | |
| # Password validation | |
| AUTH_PASSWORD_VALIDATORS = [ | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | |
| 'OPTIONS': { | |
| 'min_length': 8, | |
| } | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | |
| }, | |
| ] | |
| # Internationalization | |
| LANGUAGE_CODE = 'en-us' | |
| TIME_ZONE = 'UTC' | |
| USE_I18N = True | |
| USE_TZ = True | |
| # Static files (CSS, JavaScript, Images) | |
| STATIC_URL = 'static/' | |
| # Media files (User uploads) | |
| MEDIA_URL = '/media/' | |
| MEDIA_ROOT = BASE_DIR / 'uploaded_documents' | |
| DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' | |
| # CORS configuration | |
| CORS_ALLOW_ALL_ORIGINS = False | |
| CORS_ALLOWED_ORIGINS = [ | |
| origin.strip() | |
| for origin in os.getenv( | |
| 'CORS_ALLOWED_ORIGINS', | |
| 'http://localhost:3000,http://0.0.0.0:3000,http://127.0.0.1:3000' | |
| ).split(',') | |
| if origin.strip() | |
| ] | |
| # Django REST Framework configuration | |
| REST_FRAMEWORK = { | |
| 'DEFAULT_AUTHENTICATION_CLASSES': ( | |
| 'rest_framework_simplejwt.authentication.JWTAuthentication', | |
| ), | |
| 'DEFAULT_PERMISSION_CLASSES': ( | |
| 'rest_framework.permissions.IsAuthenticated', | |
| ), | |
| 'DEFAULT_THROTTLE_CLASSES': ( | |
| 'rest_framework.throttling.AnonRateThrottle', | |
| 'rest_framework.throttling.UserRateThrottle', | |
| ), | |
| 'DEFAULT_THROTTLE_RATES': { | |
| 'anon': '30/minute', | |
| 'user': '100/minute', | |
| }, | |
| } | |
| # Simple JWT settings | |
| SIMPLE_JWT = { | |
| 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=30), | |
| 'REFRESH_TOKEN_LIFETIME': timedelta(days=7), | |
| 'ROTATE_REFRESH_TOKENS': True, | |
| 'BLACKLIST_AFTER_ROTATION': True, | |
| 'ALGORITHM': 'HS256', | |
| 'SIGNING_KEY': SECRET_KEY, | |
| 'VERIFYING_KEY': None, | |
| 'AUDIENCE': None, | |
| 'ISSUER': None, | |
| 'AUTH_HEADER_TYPES': ('Bearer',), | |
| 'USER_ID_FIELD': 'id', | |
| 'USER_ID_CLAIM': 'user_id', | |
| 'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',), | |
| } | |
| # File Upload Settings | |
| ALLOWED_EXTENSIONS = {'.pdf', '.txt', '.md', '.docx', '.doc', '.csv', '.json', '.html', '.xml'} | |
| MAX_UPLOAD_SIZE_MB = 10 | |
| # Ingestion Concurrency | |
| MAX_INGESTION_WORKERS = 3 | |
| # Graph Database (Neo4j) settings | |
| NEO4J_URI = os.getenv('NEO4J_URI', 'bolt://localhost:7687') | |
| NEO4J_USERNAME = os.getenv('NEO4J_USERNAME', 'neo4j') | |
| NEO4J_PASSWORD = os.getenv('NEO4J_PASSWORD', 'password') | |
| # ChromaDB settings | |
| # Supports: local (default) or cloud | |
| CHROMADB_MODE = os.getenv('CHROMADB_MODE', 'local') | |
| CHROMADB_HOST = os.getenv('CHROMADB_HOST', '') | |
| CHROMADB_PORT = int(os.getenv('CHROMADB_PORT', '0')) | |
| CHROMADB_DIR = os.path.join(BASE_DIR, os.getenv('CHROMADB_DIR', 'chroma_db')) | |
| # Logging Configuration | |
| LOG_DIR = BASE_DIR / 'logs' | |
| LOG_DIR.mkdir(exist_ok=True) | |
| LOGGING = { | |
| 'version': 1, | |
| 'disable_existing_loggers': False, | |
| 'formatters': { | |
| 'detailed': { | |
| 'format': '%(asctime)s [%(levelname)s] %(name)s:%(lineno)d - %(message)s' | |
| }, | |
| 'simple': { | |
| 'format': '[%(levelname)s] %(message)s' | |
| }, | |
| }, | |
| 'handlers': { | |
| 'console': { | |
| 'class': 'logging.StreamHandler', | |
| 'formatter': 'detailed', | |
| }, | |
| 'file': { | |
| 'class': 'logging.handlers.RotatingFileHandler', | |
| 'filename': LOG_DIR / 'graphrag.log', | |
| 'maxBytes': 10 * 1024 * 1024, # 10MB | |
| 'backupCount': 5, | |
| 'formatter': 'detailed', | |
| }, | |
| }, | |
| 'loggers': { | |
| '': { | |
| 'handlers': ['console', 'file'], | |
| 'level': 'INFO', | |
| }, | |
| 'django': { | |
| 'handlers': ['console', 'file'], | |
| 'level': 'INFO', | |
| 'propagate': False, | |
| }, | |
| 'graphrag': { | |
| 'handlers': ['console', 'file'], | |
| 'level': 'INFO', | |
| 'propagate': False, | |
| }, | |
| }, | |
| } | |