Upload 12 files
Browse files- .huggingface.yml +3 -0
- app.py +92 -133
- launch.sh +4 -0
- requirements.txt +1 -1
.huggingface.yml
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
sdk: docker
|
| 2 |
+
app_file: app.py
|
| 3 |
+
entrypoint: ./launch.sh
|
app.py
CHANGED
|
@@ -1,133 +1,92 @@
|
|
| 1 |
-
"""
|
| 2 |
-
app.py β Bodha Vector API
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
import
|
| 12 |
-
from fastapi import
|
| 13 |
-
from
|
| 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 |
-
if
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
"rank": i + 1,
|
| 94 |
-
"score": round(score, 4) if score else None,
|
| 95 |
-
"class": meta.get("class", ""),
|
| 96 |
-
"chapter": meta.get("chapter", ""),
|
| 97 |
-
"pages": meta.get("pages", ""),
|
| 98 |
-
"source": meta.get("source_pdf", ""),
|
| 99 |
-
"text": doc
|
| 100 |
-
})
|
| 101 |
-
return JSONResponse(response_data)
|
| 102 |
-
|
| 103 |
-
except Exception as e:
|
| 104 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
| 105 |
-
|
| 106 |
-
# ---------- Optional Gradio Interface ----------
|
| 107 |
-
def gradio_query(query):
|
| 108 |
-
if not query.strip():
|
| 109 |
-
return "β οΈ Please enter a query."
|
| 110 |
-
results = collection.query(query_texts=[query], n_results=TOP_K)
|
| 111 |
-
docs = results.get("documents", [[]])[0]
|
| 112 |
-
metas = results.get("metadatas", [[]])[0]
|
| 113 |
-
dists = results.get("distances", [[]])[0]
|
| 114 |
-
output = ""
|
| 115 |
-
for i, doc in enumerate(docs):
|
| 116 |
-
meta = metas[i]
|
| 117 |
-
score = 1 - dists[i] if dists else None
|
| 118 |
-
output += f"### Result {i+1} (score: {round(score,3) if score else 'N/A'})\n"
|
| 119 |
-
output += f"**Class:** {meta.get('class','')} \n"
|
| 120 |
-
output += f"**Source:** {meta.get('source_pdf','')} \n"
|
| 121 |
-
output += f"**Pages:** {meta.get('pages','')} \n\n"
|
| 122 |
-
output += f"{doc[:700]}...\n\n---\n\n"
|
| 123 |
-
return output or "No results found."
|
| 124 |
-
|
| 125 |
-
demo = gr.Interface(
|
| 126 |
-
fn=gradio_query,
|
| 127 |
-
inputs=gr.Textbox(label="Ask your Physics/Science question"),
|
| 128 |
-
outputs="markdown",
|
| 129 |
-
title="π Bodha Textbook Search",
|
| 130 |
-
description="Search across NCERT (Class 6β12) & H.C. Verma textbooks"
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
app.py β Bodha Vector API for Hugging Face
|
| 3 |
+
Provides:
|
| 4 |
+
β’ /vector-api/search β JSON API (with Basic Auth)
|
| 5 |
+
β’ / β Gradio interface
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import os, chromadb, gradio as gr
|
| 9 |
+
from fastapi import FastAPI, Request, Depends, HTTPException, status
|
| 10 |
+
from fastapi.responses import JSONResponse
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
| 13 |
+
from contextlib import asynccontextmanager
|
| 14 |
+
|
| 15 |
+
# ---------- CONFIG ----------
|
| 16 |
+
os.environ["CHROMA_TELEMETRY"] = "FALSE"
|
| 17 |
+
CHROMA_PATH = "/app/textbook_db"
|
| 18 |
+
COLLECTION_NAME = "textbooks"
|
| 19 |
+
USERNAME, PASSWORD = "bodha", "securepass123"
|
| 20 |
+
TOP_K = 5
|
| 21 |
+
# ----------------------------
|
| 22 |
+
|
| 23 |
+
# ---------- INIT ----------
|
| 24 |
+
client = chromadb.PersistentClient(path=CHROMA_PATH)
|
| 25 |
+
collection = client.get_or_create_collection(COLLECTION_NAME)
|
| 26 |
+
security = HTTPBasic()
|
| 27 |
+
|
| 28 |
+
@asynccontextmanager
|
| 29 |
+
async def lifespan(app: FastAPI):
|
| 30 |
+
print("β
Starting Bodha Vector API...")
|
| 31 |
+
print(f"π Collection: {COLLECTION_NAME} | Docs: {collection.count()}")
|
| 32 |
+
yield
|
| 33 |
+
print("π Stopping Bodha API...")
|
| 34 |
+
|
| 35 |
+
app = FastAPI(lifespan=lifespan)
|
| 36 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 37 |
+
|
| 38 |
+
# ---------- AUTH ----------
|
| 39 |
+
def verify(credentials: HTTPBasicCredentials = Depends(security)):
|
| 40 |
+
if credentials.username != USERNAME or credentials.password != PASSWORD:
|
| 41 |
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
|
| 42 |
+
detail="Invalid credentials",
|
| 43 |
+
headers={"WWW-Authenticate": "Basic"})
|
| 44 |
+
return credentials.username
|
| 45 |
+
|
| 46 |
+
# ---------- API ----------
|
| 47 |
+
@app.post("/vector-api/search")
|
| 48 |
+
async def search_vector(request: Request, user: str = Depends(verify)):
|
| 49 |
+
body = await request.json()
|
| 50 |
+
query = body.get("query", "").strip()
|
| 51 |
+
if not query:
|
| 52 |
+
return JSONResponse({"error": "Missing query"}, status_code=400)
|
| 53 |
+
|
| 54 |
+
res = collection.query(query_texts=[query], n_results=TOP_K)
|
| 55 |
+
docs, metas, dists = res["documents"][0], res["metadatas"][0], res["distances"][0]
|
| 56 |
+
out = []
|
| 57 |
+
for i, doc in enumerate(docs):
|
| 58 |
+
meta = metas[i]
|
| 59 |
+
score = 1 - dists[i] if dists else None
|
| 60 |
+
out.append({
|
| 61 |
+
"rank": i + 1,
|
| 62 |
+
"score": round(score, 4) if score else None,
|
| 63 |
+
"class": meta.get("class", ""),
|
| 64 |
+
"chapter": meta.get("chapter", ""),
|
| 65 |
+
"pages": meta.get("pages", ""),
|
| 66 |
+
"source": meta.get("source_pdf", ""),
|
| 67 |
+
"text": doc
|
| 68 |
+
})
|
| 69 |
+
return JSONResponse(out)
|
| 70 |
+
|
| 71 |
+
# ---------- GRADIO ----------
|
| 72 |
+
def gradio_query(q):
|
| 73 |
+
if not q.strip():
|
| 74 |
+
return "β οΈ Enter a query."
|
| 75 |
+
res = collection.query(query_texts=[q], n_results=TOP_K)
|
| 76 |
+
docs, metas, dists = res["documents"][0], res["metadatas"][0], res["distances"][0]
|
| 77 |
+
out = ""
|
| 78 |
+
for i, doc in enumerate(docs):
|
| 79 |
+
meta = metas[i]
|
| 80 |
+
score = 1 - dists[i] if dists else None
|
| 81 |
+
out += f"### {i+1}. {meta.get('class','')} ({meta.get('source_pdf','')}) β score {round(score,3)}\n{doc[:700]}...\n\n---\n"
|
| 82 |
+
return out or "No results."
|
| 83 |
+
|
| 84 |
+
demo = gr.Interface(
|
| 85 |
+
fn=gradio_query,
|
| 86 |
+
inputs=gr.Textbox(label="Ask a Physics/Science question"),
|
| 87 |
+
outputs="markdown",
|
| 88 |
+
title="π Bodha Textbook Search",
|
| 89 |
+
description="Search across NCERT (Class 6β12) + H.C.Verma"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
launch.sh
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Hugging Face starts this script; it launches FastAPI via uvicorn.
|
| 3 |
+
echo "π Launching Bodha Vector API on port 7860..."
|
| 4 |
+
exec uvicorn app:app --host 0.0.0.0 --port 7860
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
chromadb==0.5.3
|
| 2 |
gradio==4.29.0
|
| 3 |
fastapi==0.111.0
|
| 4 |
-
uvicorn==0.30.0
|
|
|
|
| 1 |
chromadb==0.5.3
|
| 2 |
gradio==4.29.0
|
| 3 |
fastapi==0.111.0
|
| 4 |
+
uvicorn==0.30.0
|