|
|
|
|
|
""" |
|
|
Startup script for Book Summarizer AI |
|
|
This script helps you start both the FastAPI backend and Streamlit frontend. |
|
|
""" |
|
|
|
|
|
import subprocess |
|
|
import sys |
|
|
import time |
|
|
import requests |
|
|
import os |
|
|
from pathlib import Path |
|
|
|
|
|
def check_dependencies(): |
|
|
"""Check if required packages are installed.""" |
|
|
required_packages = [ |
|
|
'streamlit', 'fastapi', 'uvicorn', 'transformers', |
|
|
'torch', 'PyPDF2', 'pdfplumber', 'nltk' |
|
|
] |
|
|
|
|
|
missing_packages = [] |
|
|
for package in required_packages: |
|
|
try: |
|
|
__import__(package) |
|
|
except ImportError: |
|
|
missing_packages.append(package) |
|
|
|
|
|
if missing_packages: |
|
|
print("β Missing required packages:") |
|
|
for package in missing_packages: |
|
|
print(f" - {package}") |
|
|
print("\nπ¦ Install them with: pip install -r requirements.txt") |
|
|
return False |
|
|
|
|
|
print("β
All dependencies are installed") |
|
|
return True |
|
|
|
|
|
def download_nltk_data(): |
|
|
"""Download required NLTK data.""" |
|
|
try: |
|
|
import nltk |
|
|
nltk.download('punkt', quiet=True) |
|
|
nltk.download('stopwords', quiet=True) |
|
|
print("β
NLTK data downloaded") |
|
|
except Exception as e: |
|
|
print(f"β οΈ Warning: Could not download NLTK data: {e}") |
|
|
|
|
|
def check_api_health(): |
|
|
"""Check if the API is running and healthy.""" |
|
|
try: |
|
|
response = requests.get("http://localhost:8000/health", timeout=5) |
|
|
return response.status_code == 200 |
|
|
except: |
|
|
return False |
|
|
|
|
|
def start_api(): |
|
|
"""Start the FastAPI backend.""" |
|
|
print("π Starting FastAPI backend...") |
|
|
|
|
|
|
|
|
if check_api_health(): |
|
|
print("β
API is already running") |
|
|
return True |
|
|
|
|
|
try: |
|
|
|
|
|
api_process = subprocess.Popen([ |
|
|
sys.executable, "-m", "uvicorn", |
|
|
"api.main:app", |
|
|
"--reload", |
|
|
"--port", "8000", |
|
|
"--host", "0.0.0.0" |
|
|
], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
|
|
|
|
|
|
|
print("β³ Waiting for API to start...") |
|
|
for i in range(30): |
|
|
time.sleep(1) |
|
|
if check_api_health(): |
|
|
print("β
API started successfully") |
|
|
return True |
|
|
|
|
|
print("β API failed to start within 30 seconds") |
|
|
return False |
|
|
|
|
|
except Exception as e: |
|
|
print(f"β Error starting API: {e}") |
|
|
return False |
|
|
|
|
|
def start_frontend(): |
|
|
"""Start the Streamlit frontend.""" |
|
|
print("π Starting Streamlit frontend...") |
|
|
|
|
|
try: |
|
|
|
|
|
subprocess.run([ |
|
|
sys.executable, "-m", "streamlit", "run", "app.py", |
|
|
"--server.port", "8501", |
|
|
"--server.address", "0.0.0.0" |
|
|
]) |
|
|
except KeyboardInterrupt: |
|
|
print("\nπ Shutting down...") |
|
|
except Exception as e: |
|
|
print(f"β Error starting frontend: {e}") |
|
|
|
|
|
def main(): |
|
|
"""Main startup function.""" |
|
|
print("π Book Summarizer AI - Startup") |
|
|
print("=" * 40) |
|
|
|
|
|
|
|
|
if not check_dependencies(): |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
download_nltk_data() |
|
|
|
|
|
print("\nπ§ Starting services...") |
|
|
|
|
|
|
|
|
if not start_api(): |
|
|
print("β Failed to start API. Please check the logs.") |
|
|
sys.exit(1) |
|
|
|
|
|
print("\nπ Ready! Opening the application...") |
|
|
print("π Frontend: http://localhost:8501") |
|
|
print("π API: http://localhost:8000") |
|
|
print("π API Docs: http://localhost:8000/docs") |
|
|
print("\nπ‘ Press Ctrl+C to stop the application") |
|
|
|
|
|
|
|
|
start_frontend() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |