Spaces:
Sleeping
Sleeping
File size: 8,256 Bytes
024013e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | import os
import urllib.parse
import shutil
import glob
import uuid
import base64
import sys
from datetime import date
from functools import lru_cache
from scraper import scrape_latest_sppu_notice
import subprocess
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.responses import FileResponse, JSONResponse, Response
from fastapi.middleware.cors import CORSMiddleware
from urllib.parse import unquote
from pydantic import BaseModel
from huggingface_hub import HfApi, hf_hub_download
from backend import DB_PERSIST_DIRECTORY, retrieve_context_with_sources, generate_llm_response, get_vectorstore
from voice_agent import generate_tutor_audio
from agent import run_agent
# --- SETUP ---
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ---- DAILY REQUEST LIMITER ----
request_count = {"date": date.today(), "count": 0}
MAX_DAILY_REQUESTS = 200
@app.middleware("http")
async def limit_requests(request, call_next):
if request.url.path in ["/chat", "/speak"]:
if request_count["date"] != date.today():
request_count["date"] = date.today()
request_count["count"] = 0
if request_count["count"] >= MAX_DAILY_REQUESTS:
return JSONResponse(
status_code=429,
content={"detail": "Daily limit reached. Try again tomorrow."}
)
request_count["count"] += 1
return await call_next(request)
# ---- END LIMITER ----
# --- VECTOR DB SETUP ---
print("--- LOADING VECTOR DATABASE ---")
vectorstore = get_vectorstore()
print("--- DATABASE LOADED ---")
# --- PDF SOURCE RESOLUTION ---
# The 218 source PDFs live in an HF dataset, not in the container. We resolve
# local data/ first (so Live Sync scraped notices still work), then fall back
# to downloading the single requested PDF from the dataset on demand.
PDF_DATASET = "Yashkute/sppu-pdf-docs"
@lru_cache(maxsize=1)
def _pdf_index():
"""Map basename -> path inside the dataset repo. Built once per container."""
files = HfApi().list_repo_files(PDF_DATASET, repo_type="dataset")
return {os.path.basename(f): f for f in files if f.lower().endswith(".pdf")}
def resolve_pdf(decoded_filename: str):
"""Return a local filesystem path for the PDF, or None if it doesn't exist."""
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
hits = glob.glob(
os.path.join(BASE_DIR, "data", "**", decoded_filename), recursive=True
)
if hits:
path = os.path.abspath(hits[0])
print(f"File found locally at: {path}")
return path
repo_path = _pdf_index().get(os.path.basename(decoded_filename))
if not repo_path:
return None
print(f"Fetching from HF dataset: {repo_path}")
return hf_hub_download(PDF_DATASET, repo_path, repo_type="dataset")
@app.get("/health")
def health():
try:
count = vectorstore._collection.count()
except Exception as e:
return {"chunks_in_db": -1, "error": str(e)}
return {"chunks_in_db": count, "db_path": DB_PERSIST_DIRECTORY}
# --- CHAT ENDPOINT ---
@app.post("/chat")
async def chat_endpoint(
question: str = Form(...),
history: str = Form(""),
use_rag: str = Form("true"),
file: UploadFile = File(None)
):
temp_image_path = None
try:
print(f"Received Question: {question}")
is_rag = use_rag.lower() == "true"
if file:
os.makedirs("temp", exist_ok=True)
temp_image_path = f"temp/{uuid.uuid4()}_{file.filename}"
with open(temp_image_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
print(f"Image saved temporarily at {temp_image_path}")
if is_rag and not temp_image_path:
print("Running agent...")
answer, sources = run_agent(question, history)
mode = "agent"
else:
answer = generate_llm_response(question, "", history, temp_image_path, use_rag=is_rag)
sources = []
mode = "vision" if temp_image_path else "general"
return JSONResponse(content={
"answer": answer,
"sources": sources,
"mode": mode,
"audio_base64": None
})
except Exception as e:
print(f"API Error: {e}")
return JSONResponse(content={"answer": "Sorry, a server error occurred.", "sources": [], "mode": "error", "audio_base64": None})
finally:
if temp_image_path and os.path.exists(temp_image_path):
os.remove(temp_image_path)
# --- VOICE ON-DEMAND ENDPOINT ---
class SpeakRequest(BaseModel):
text: str
@app.post("/speak")
async def speak_endpoint(req: SpeakRequest):
print("Synthesizing Voice Tutor Audio on demand...")
try:
clean_text = req.text.replace("**", "").replace("*", "").replace("#", "")
if len(clean_text) > 3500:
clean_text = clean_text[:3500] + "... The rest of the answer has been truncated for audio playback."
audio_bytes = await generate_tutor_audio(clean_text)
if audio_bytes:
return Response(content=audio_bytes, media_type="audio/mpeg")
raise HTTPException(status_code=500, detail="Audio generation failed")
except Exception as e:
print(f"Speak Error: {e}")
raise HTTPException(status_code=500, detail=str(e))
# --- LIVE SYNC ENDPOINT (kept as manual override) ---
@app.post("/sync-notices")
async def sync_sppu_notices():
try:
success, message = scrape_latest_sppu_notice()
if success:
print("New file detected! Triggering Vector Database Update...")
backend_dir = os.path.dirname(os.path.abspath(__file__))
result = subprocess.run(
[sys.executable, "ingest.py"],
cwd=backend_dir,
check=False,
capture_output=True,
text=True,
encoding="utf-8",
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
)
if result.returncode != 0:
err_tail = (result.stderr or "").strip()[-2000:]
out_tail = (result.stdout or "").strip()[-2000:]
raise RuntimeError(
f"ingest.py failed (exit code {result.returncode}).\nSTDERR tail:\n{err_tail}\nSTDOUT tail:\n{out_tail}"
)
global vectorstore
get_vectorstore.cache_clear()
vectorstore = get_vectorstore()
ingest_tail = ""
combined = ((result.stdout or "") + "\n" + (result.stderr or "")).strip()
if combined:
ingest_tail = combined[-900:]
if ingest_tail:
message += f" and injected into AI Memory (ingest completed). Ingest log tail: {ingest_tail}"
else:
message += " and injected into AI Memory (ingest completed)."
return {"status": "success" if success else "warning", "message": message}
except Exception as e:
print(f"Sync API Error: {e}")
raise HTTPException(status_code=500, detail=str(e))
# --- PDF VIEW ENDPOINT ---
@app.get("/view/{filename:path}")
async def view_pdf(filename: str):
decoded_filename = unquote(filename)
path = resolve_pdf(decoded_filename)
if not path:
print(f"File NOT found: {decoded_filename}")
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(path, media_type="application/pdf")
# --- PDF DOWNLOAD ENDPOINT ---
@app.get("/download/{filename:path}")
async def download_file(filename: str):
decoded_filename = urllib.parse.unquote(filename)
print(f"Download request: {decoded_filename}")
path = resolve_pdf(decoded_filename)
if not path:
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(
path=path,
filename=decoded_filename,
media_type="application/pdf",
headers={"Content-Disposition": f'attachment; filename="{decoded_filename}"'}
)
# --- SERVER STARTUP ---
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) |