import sys, os from fastapi import FastAPI, UploadFile, Form from fastapi.responses import JSONResponse # Ensure omniscientframework/ is importable ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) if ROOT not in sys.path: sys.path.insert(0, ROOT) from omniscientframework.utils.summarizer import summarize_text from omniscientframework.utils.file_utils import normalize_log_line, keyword_search from omniscientframework.utils.docgen import generate_doc app = FastAPI(title="Omniscient Backend API") @app.get("/health") def health(): return JSONResponse({"ok": True, "message": "Omniscient backend alive"}) @app.post("/summarize") async def summarize(file: UploadFile): try: content = (await file.read()).decode("utf-8", errors="ignore") summary = summarize_text(content) return {"file": file.filename, "summary": summary} except Exception as e: return {"error": str(e)} @app.post("/analyze-log") async def analyze_log(file: UploadFile, query: str = Form(None)): try: content = (await file.read()).decode("utf-8", errors="ignore") normalized = [normalize_log_line(line) for line in content.splitlines()] result = { "total_lines": len(normalized), "unique_entries": len(set(normalized)), "preview": normalized[:50], } if query: matches = keyword_search("\n".join(normalized), query) result["matches"] = matches[:50] return {"file": file.filename, "analysis": result} except Exception as e: return {"error": str(e)} @app.post("/generate-doc") async def gen_doc(file: UploadFile): try: content = (await file.read()).decode("utf-8", errors="ignore") doc = generate_doc(file.filename, "uploaded", content) return {"file": file.filename, "doc": doc} except Exception as e: return {"error": str(e)}