NexusInstruments's picture
Update app.py
a00cdf4 verified
# ─────────────────────────────────────────────────────────────
# FIXED APP.PY – PROPERLY RESOLVES UTILS IMPORTS ON HF SPACES
# ─────────────────────────────────────────────────────────────
import os
import sys
from fastapi import FastAPI, UploadFile, Form
from fastapi.responses import JSONResponse
# ─── Ensure /app/utils is visible as top-level modules ─────────
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
UTILS_DIR = os.path.join(BASE_DIR, "utils")
if UTILS_DIR not in sys.path:
sys.path.insert(0, UTILS_DIR)
print("DEBUG: Using UTILS_DIR:", UTILS_DIR)
# ─── Correct imports based on actual filesystem ───────────────
from summarizer import summarize_text
from file_utils import normalize_log_line, keyword_search
from docgen import generate_doc
# ─── FastAPI App Init ─────────────────────────────────────────
app = FastAPI(title="Omniscient Backend API")
# ─── Health Check ─────────────────────────────────────────────
@app.get("/health")
def health():
return JSONResponse({"ok": True, "message": "Omniscient backend alive"})
# ─── File Summarization ───────────────────────────────────────
@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)}
# ─── Log Normalization & Search ───────────────────────────────
@app.post("/analyze-log")
async def analyze_log(file: UploadFile, query: str = Form(None)):
try:
raw = (await file.read()).decode("utf-8", errors="ignore")
normalized = [normalize_log_line(line) for line in raw.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)}
# ─── Script Documentation Generator ───────────────────────────
@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)}