Skip to content

Commit d4df82a

Browse files
committed
Modularized backend and change in frontend
1 parent 0283d11 commit d4df82a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+4105
-11233
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
__pycache__
3+
models
4+
video_frames
5+
faiss_index.bin
6+
input_video.mp4
7+
metadata.pkl

CVPS.ipynb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
},
207207
{
208208
"cell_type": "code",
209-
"execution_count": 5,
209+
"execution_count": null,
210210
"metadata": {
211211
"id": "bMdCOmne9_vT"
212212
},
@@ -294,6 +294,13 @@
294294
"frames = extract_frames(\"input_video.mp4\",\"video_frames\")"
295295
]
296296
},
297+
{
298+
"cell_type": "code",
299+
"execution_count": null,
300+
"metadata": {},
301+
"outputs": [],
302+
"source": []
303+
},
297304
{
298305
"cell_type": "code",
299306
"execution_count": null,

backend/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
__pycache__
3+
models
4+
video_frames
5+
faiss_index.bin
6+
input_video.mp4
7+
metadata.pkl

backend/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import uvicorn
2+
3+
if __name__ == "__main__":
4+
uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)

backend/app/__init__.py

Whitespace-only changes.

backend/app/api/__init__.py

Whitespace-only changes.

backend/app/api/routes.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from fastapi import APIRouter, UploadFile, Request, File, Form, HTTPException
2+
from app.services.clip_model import clip_model
3+
from app.services.llava_api import query_llava, process_query
4+
from app.core.memory import ChatMemory
5+
from app.services.extract_frames import extract_frames
6+
from app.services.faiss import faiss_process
7+
8+
router = APIRouter()
9+
memory = ChatMemory()
10+
11+
from PIL import Image
12+
13+
router = APIRouter()
14+
15+
16+
@router.post("/clip")
17+
async def clip_embed_video(file: UploadFile = File(...)):
18+
"""
19+
Saves video, extracts frames, embeds them using CLIP, and stores in FAISS.
20+
"""
21+
try:
22+
# Step 1: Save uploaded video to disk
23+
temp_path = "input_video.mp4"
24+
with open(temp_path, "wb") as f:
25+
f.write(await file.read())
26+
27+
# Step 2: Extract frames
28+
frames = extract_frames(temp_path, "video_frames")
29+
(preprocess, model) = clip_model()
30+
31+
faiss_process(preprocess, model, frames)
32+
33+
34+
return {"ready": True}
35+
except Exception as e:
36+
return {"ready": False, "error": str(e)}
37+
38+
39+
@router.post("/llava")
40+
async def llava_query(request: Request):
41+
"""
42+
Accepts a question from JSON, retrieves relevant context from FAISS,
43+
and sends it to Segmind's LLaVA API.
44+
"""
45+
try:
46+
body = await request.json()
47+
question = body.get("question", "").strip()
48+
print(f"Received question: {question}")
49+
50+
if not question:
51+
raise HTTPException(status_code=400, detail="Question is required.")
52+
53+
# Step 1: Retrieve relevant context
54+
(preprocess, model) = clip_model()
55+
context_data = process_query(question, preprocess, model)
56+
print(f"Retrieved context: {context_data}")
57+
58+
# Step 2: Query LLaVA
59+
answer = query_llava(context_data, question)
60+
# memory.add(question, answer)
61+
62+
print(f"LLaVA answer: {answer}")
63+
64+
return {"answer": answer}
65+
66+
except Exception as e:
67+
raise HTTPException(status_code=500, detail=f"Failed to get LLaVA response: {str(e)}")
68+

backend/app/core/__init__.py

Whitespace-only changes.

backend/app/core/memory.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from collections import deque
2+
3+
class ChatMemory:
4+
def __init__(self, max_len=10):
5+
self.history = deque(maxlen=max_len)
6+
7+
def add(self, user_msg: str, bot_msg: str):
8+
self.history.append({"user": user_msg, "bot": bot_msg})
9+
10+
def get(self):
11+
return list(self.history)
12+
13+
def clear(self):
14+
self.history.clear()

backend/app/main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from fastapi import FastAPI
2+
from fastapi.middleware.cors import CORSMiddleware
3+
from app.api.routes import router # Your routes
4+
5+
app = FastAPI()
6+
7+
# Allow requests from frontend (Vite dev server)
8+
app.add_middleware(
9+
CORSMiddleware,
10+
allow_origins=["http://localhost:5173"],
11+
allow_credentials=True,
12+
allow_methods=["*"],
13+
allow_headers=["*"],
14+
)
15+
16+
app.include_router(router)

0 commit comments

Comments
 (0)