Files
ds_fire_fighter/main.py
T

59 lines
1.7 KiB
Python
Raw Normal View History

2024-08-07 18:55:56 +01:00
import sys, os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
2024-08-07 18:27:42 +01:00
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
2024-08-16 17:50:51 +01:00
from utils import load_embedded_data, load_documents_from_directory, create_vector_store, save_embedded_data, process_directory
2024-08-16 17:37:28 +01:00
from search import search_and_summarize
2024-08-14 23:09:10 +01:00
from data_ingest import load_data
2024-08-07 18:27:42 +01:00
app = FastAPI()
# Define allowed origins for CORS
origins = [
"http://localhost",
"http://localhost:8000",
"http://localhost:3000",
# Add other allowed origins here
]
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # Allows requests from listed origins
allow_credentials=True,
allow_methods=["*"], # Allows all HTTP methods
allow_headers=["*"], # Allows all headers
)
class SearchRequest(BaseModel):
query: str
2024-08-16 21:39:28 +01:00
@app.post("/load_documents")
2024-08-07 18:27:42 +01:00
def load_documents(directory: str):
2024-08-16 17:37:28 +01:00
# loading the documents from the directory
documents, docs_id, num_pages = load_documents_from_directory(directory)
# embedding the documents
embed_db = create_vector_store(documents, docs_id, num_pages)
# saving the embedded data
status = save_embedded_data(embed_db)
2024-08-16 17:50:51 +01:00
# creating the thumbnails
status = process_directory(directory)
2024-08-07 18:27:42 +01:00
return {"status": "Documents loaded successfully"}
2024-08-16 17:37:28 +01:00
@app.post("/search")
2024-08-07 18:27:42 +01:00
def search(request: SearchRequest):
# Perform search using the utility function
2024-08-16 17:37:28 +01:00
results = search_and_summarize(request.query)
2024-08-07 18:27:42 +01:00
return {"results": results}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)