#!/usr/bin/env python | |
""" | |
Main entry point for Hugging Face Spaces deployment. | |
This file starts the Streamlit UI when deployed to Hugging Face Spaces. | |
""" | |
import subprocess | |
import os | |
import sys | |
import time | |
import random | |
import logging | |
# Configure logging | |
logging.basicConfig( | |
level=logging.INFO, | |
format="%(asctime)s [%(levelname)s] %(message)s", | |
handlers=[ | |
logging.StreamHandler() | |
] | |
) | |
logger = logging.getLogger(__name__) | |
# Make sure the app directory is in the path | |
# Add the current directory to the path so that 'app' is recognized as a package | |
sys.path.append(os.path.dirname(os.path.abspath(__file__))) | |
# Also add the parent directory to path to ensure imports work properly | |
sys.path.append(os.path.abspath('.')) | |
# Create necessary directories | |
logger.info("Creating necessary directories...") | |
os.makedirs('data/documents', exist_ok=True) | |
os.makedirs('data/vector_db', exist_ok=True) | |
# Create multiple vector database paths to help with concurrent access | |
for i in range(1, 4): | |
path = f'data/vector_db_{i}' | |
os.makedirs(path, exist_ok=True) | |
# Ensure directories have proper permissions | |
try: | |
os.chmod(path, 0o777) | |
except Exception as e: | |
logger.warning(f"Could not set permissions for {path}: {e}") | |
# Set environment variable for Python path | |
os.environ['PYTHONPATH'] = os.path.abspath('.') | |
# Add a small delay to ensure directory creation is complete | |
logger.info("Starting application...") | |
time.sleep(1) | |
# Run the Streamlit app with specific port to match huggingface-space.yml | |
# Add server.maxMessageSize to handle larger files and messages | |
cmd = [ | |
"streamlit", "run", "app/ui/streamlit_app.py", | |
"--server.port=7860", | |
"--server.address=0.0.0.0", | |
"--server.maxUploadSize=10", | |
"--server.maxMessageSize=200" | |
] | |
logger.info(f"Running command: {' '.join(cmd)}") | |
subprocess.run(cmd) |