Update main.py
Browse files
main.py
CHANGED
|
@@ -4,18 +4,23 @@ import subprocess
|
|
| 4 |
import time
|
| 5 |
import requests
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def run_scraper():
|
| 8 |
"""Run the web scraper to collect data."""
|
| 9 |
from buffalo_rag.scraper.scraper import BuffaloScraper
|
| 10 |
|
| 11 |
if os.path.exists("data/raw"):
|
| 12 |
num_pages = len([name for name in os.listdir("data/raw") if os.path.isfile(os.path.join("data/raw", name))])
|
| 13 |
-
if num_pages >
|
| 14 |
print(f"{num_pages} scraped data files found under data/raw. Skipping data scraper stage.")
|
| 15 |
else:
|
| 16 |
print("Starting web scraper...")
|
| 17 |
scraper = BuffaloScraper()
|
| 18 |
-
scraper.scrape(max_pages=
|
| 19 |
print("Scraping completed!")
|
| 20 |
|
| 21 |
def build_embeddings():
|
|
@@ -28,65 +33,28 @@ def build_embeddings():
|
|
| 28 |
chunker.create_embeddings(chunks)
|
| 29 |
print("Embeddings created!")
|
| 30 |
|
| 31 |
-
def
|
| 32 |
-
"""
|
| 33 |
print("Starting API server...")
|
| 34 |
-
subprocess.run(["uvicorn", "buffalo_rag.api.main:app", "--host", "0.0.0.0", "--port",
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
"""
|
| 38 |
print("Starting Flask frontend...")
|
| 39 |
-
subprocess.run(["flask", "run", "--host=0.0.0.0", "--port=
|
| 40 |
env={**os.environ, "FLASK_APP": "buffalo_rag/frontend/flask_app.py"})
|
| 41 |
|
| 42 |
-
def build_react_frontend():
|
| 43 |
-
"""Build the React frontend."""
|
| 44 |
-
print("Building React frontend...")
|
| 45 |
-
subprocess.run(["npm", "run", "build"], cwd="frontend")
|
| 46 |
-
|
| 47 |
-
# Copy build files to static directory
|
| 48 |
-
subprocess.run(["cp", "-r", "frontend/build/*", "buffalo_rag/api/static/"])
|
| 49 |
-
print("React frontend built successfully!")
|
| 50 |
-
|
| 51 |
-
def setup_react_frontend():
|
| 52 |
-
"""Install dependencies and build the React frontend."""
|
| 53 |
-
print("Setting up React frontend...")
|
| 54 |
-
if not os.path.exists("frontend"):
|
| 55 |
-
# Run the setup script
|
| 56 |
-
subprocess.run(["bash", "setup_frontend.sh"])
|
| 57 |
-
|
| 58 |
-
# Install dependencies
|
| 59 |
-
subprocess.run(["npm", "install"], cwd="frontend")
|
| 60 |
-
|
| 61 |
-
# Build the frontend
|
| 62 |
-
subprocess.run(["npm", "run", "build"], cwd="frontend")
|
| 63 |
-
|
| 64 |
-
# Create static directory if it doesn't exist
|
| 65 |
-
static_dir = os.path.join("buffalo_rag", "api", "static")
|
| 66 |
-
os.makedirs(static_dir, exist_ok=True)
|
| 67 |
-
|
| 68 |
-
# Copy build files to static directory
|
| 69 |
-
if os.path.exists("frontend/build"):
|
| 70 |
-
# On macOS/Linux
|
| 71 |
-
subprocess.run(["cp", "-r", "frontend/build/.", static_dir])
|
| 72 |
-
else:
|
| 73 |
-
print("Frontend build directory not found. Please build the frontend manually.")
|
| 74 |
-
|
| 75 |
-
print("React frontend setup completed!")
|
| 76 |
-
|
| 77 |
def wait_for_server(url, timeout=30, interval=1):
|
| 78 |
"""Waits for a server at the given URL to be reachable."""
|
| 79 |
start_time = time.time()
|
| 80 |
print(f"Waiting for server at {url} to be ready...")
|
| 81 |
while time.time() - start_time < timeout:
|
| 82 |
try:
|
| 83 |
-
# Make a simple request (e.g., to the root or a health check endpoint)
|
| 84 |
response = requests.get(url, timeout=interval)
|
| 85 |
-
if response.status_code < 500:
|
| 86 |
print(f"Server at {url} is ready.")
|
| 87 |
return True
|
| 88 |
except requests.exceptions.RequestException:
|
| 89 |
-
# Server is not ready yet, continue waiting
|
| 90 |
pass
|
| 91 |
time.sleep(interval)
|
| 92 |
print(f"Timeout waiting for server at {url}.")
|
|
@@ -94,7 +62,6 @@ def wait_for_server(url, timeout=30, interval=1):
|
|
| 94 |
|
| 95 |
def main():
|
| 96 |
parser = argparse.ArgumentParser(description="BuffaloRAG - AI Assistant for UB International Students")
|
| 97 |
-
parser.add_argument("--flask-setup", action="store_true", help="Setup Flask frontend")
|
| 98 |
parser.add_argument("--scrape", action="store_true", help="Run web scraper")
|
| 99 |
parser.add_argument("--build", action="store_true", help="Build embeddings")
|
| 100 |
parser.add_argument("--api", action="store_true", help="Run API server")
|
|
@@ -104,11 +71,6 @@ def main():
|
|
| 104 |
|
| 105 |
args = parser.parse_args()
|
| 106 |
|
| 107 |
-
if args.flask_setup or args.all or args.run:
|
| 108 |
-
# Run the Flask setup script
|
| 109 |
-
from setup_flask_templates import setup_flask_templates
|
| 110 |
-
setup_flask_templates()
|
| 111 |
-
|
| 112 |
if args.scrape or args.all:
|
| 113 |
run_scraper()
|
| 114 |
|
|
@@ -116,24 +78,18 @@ def main():
|
|
| 116 |
build_embeddings()
|
| 117 |
|
| 118 |
if args.api or args.all or args.run:
|
| 119 |
-
# Start API in a separate process
|
| 120 |
if args.all or args.run:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# Wait for API to start
|
| 124 |
-
api_ready = wait_for_server('http://localhost:8000/', timeout=60)
|
| 125 |
|
| 126 |
if api_ready:
|
| 127 |
time.sleep(3)
|
| 128 |
-
|
| 129 |
-
flask_process = subprocess.Popen(["flask", "run", "--host=0.0.0.0", "--port=7860"],
|
| 130 |
env={**os.environ, "FLASK_APP": "buffalo_rag/frontend/flask_app.py"})
|
| 131 |
|
| 132 |
-
# Open the browser
|
| 133 |
import webbrowser
|
| 134 |
-
webbrowser.open('http://localhost:
|
| 135 |
|
| 136 |
-
# Wait for user to quit
|
| 137 |
input("Press Enter to stop the server and exit...\n")
|
| 138 |
if api_process.poll() is None:
|
| 139 |
api_process.terminate()
|
|
@@ -143,10 +99,10 @@ def main():
|
|
| 143 |
if api_process.poll() is None:
|
| 144 |
api_process.terminate()
|
| 145 |
else:
|
| 146 |
-
|
| 147 |
|
| 148 |
if args.frontend:
|
| 149 |
-
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
main()
|
|
|
|
| 4 |
import time
|
| 5 |
import requests
|
| 6 |
|
| 7 |
+
# Port configurations
|
| 8 |
+
API_PORT = 8000
|
| 9 |
+
FRONTEND_PORT = 7860
|
| 10 |
+
MAX_PAGES = 1000 # Max pages to be Scrapped
|
| 11 |
+
|
| 12 |
def run_scraper():
|
| 13 |
"""Run the web scraper to collect data."""
|
| 14 |
from buffalo_rag.scraper.scraper import BuffaloScraper
|
| 15 |
|
| 16 |
if os.path.exists("data/raw"):
|
| 17 |
num_pages = len([name for name in os.listdir("data/raw") if os.path.isfile(os.path.join("data/raw", name))])
|
| 18 |
+
if num_pages > MAX_PAGES:
|
| 19 |
print(f"{num_pages} scraped data files found under data/raw. Skipping data scraper stage.")
|
| 20 |
else:
|
| 21 |
print("Starting web scraper...")
|
| 22 |
scraper = BuffaloScraper()
|
| 23 |
+
scraper.scrape(max_pages=MAX_PAGES)
|
| 24 |
print("Scraping completed!")
|
| 25 |
|
| 26 |
def build_embeddings():
|
|
|
|
| 33 |
chunker.create_embeddings(chunks)
|
| 34 |
print("Embeddings created!")
|
| 35 |
|
| 36 |
+
def start_api_server():
|
| 37 |
+
"""Start the FastAPI backend server."""
|
| 38 |
print("Starting API server...")
|
| 39 |
+
subprocess.run(["uvicorn", "buffalo_rag.api.main:app", "--host", "0.0.0.0", "--port", str(API_PORT), "--reload"])
|
| 40 |
|
| 41 |
+
def start_flask_frontend():
|
| 42 |
+
"""Start the Flask frontend."""
|
| 43 |
print("Starting Flask frontend...")
|
| 44 |
+
subprocess.run(["flask", "run", "--host=0.0.0.0", f"--port={FRONTEND_PORT}"],
|
| 45 |
env={**os.environ, "FLASK_APP": "buffalo_rag/frontend/flask_app.py"})
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def wait_for_server(url, timeout=30, interval=1):
|
| 48 |
"""Waits for a server at the given URL to be reachable."""
|
| 49 |
start_time = time.time()
|
| 50 |
print(f"Waiting for server at {url} to be ready...")
|
| 51 |
while time.time() - start_time < timeout:
|
| 52 |
try:
|
|
|
|
| 53 |
response = requests.get(url, timeout=interval)
|
| 54 |
+
if response.status_code < 500:
|
| 55 |
print(f"Server at {url} is ready.")
|
| 56 |
return True
|
| 57 |
except requests.exceptions.RequestException:
|
|
|
|
| 58 |
pass
|
| 59 |
time.sleep(interval)
|
| 60 |
print(f"Timeout waiting for server at {url}.")
|
|
|
|
| 62 |
|
| 63 |
def main():
|
| 64 |
parser = argparse.ArgumentParser(description="BuffaloRAG - AI Assistant for UB International Students")
|
|
|
|
| 65 |
parser.add_argument("--scrape", action="store_true", help="Run web scraper")
|
| 66 |
parser.add_argument("--build", action="store_true", help="Build embeddings")
|
| 67 |
parser.add_argument("--api", action="store_true", help="Run API server")
|
|
|
|
| 71 |
|
| 72 |
args = parser.parse_args()
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
if args.scrape or args.all:
|
| 75 |
run_scraper()
|
| 76 |
|
|
|
|
| 78 |
build_embeddings()
|
| 79 |
|
| 80 |
if args.api or args.all or args.run:
|
|
|
|
| 81 |
if args.all or args.run:
|
| 82 |
+
api_process = subprocess.Popen(["uvicorn", "buffalo_rag.api.main:app", "--host", "0.0.0.0", "--port", str(API_PORT), "--reload"])
|
| 83 |
+
api_ready = wait_for_server(f'http://localhost:{API_PORT}/', timeout=60)
|
|
|
|
|
|
|
| 84 |
|
| 85 |
if api_ready:
|
| 86 |
time.sleep(3)
|
| 87 |
+
flask_process = subprocess.Popen(["flask", "run", "--host=0.0.0.0", f"--port={FRONTEND_PORT}"],
|
|
|
|
| 88 |
env={**os.environ, "FLASK_APP": "buffalo_rag/frontend/flask_app.py"})
|
| 89 |
|
|
|
|
| 90 |
import webbrowser
|
| 91 |
+
webbrowser.open(f'http://localhost:{FRONTEND_PORT}')
|
| 92 |
|
|
|
|
| 93 |
input("Press Enter to stop the server and exit...\n")
|
| 94 |
if api_process.poll() is None:
|
| 95 |
api_process.terminate()
|
|
|
|
| 99 |
if api_process.poll() is None:
|
| 100 |
api_process.terminate()
|
| 101 |
else:
|
| 102 |
+
start_api_server()
|
| 103 |
|
| 104 |
if args.frontend:
|
| 105 |
+
start_flask_frontend()
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
main()
|