-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (42 loc) · 1.56 KB
/
Copy pathmain.py
File metadata and controls
53 lines (42 loc) · 1.56 KB
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import os
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pydantic import BaseModel
from utils.youtube_utils import extract_video_id, fetch_transcript
from utils.qa_pipeline import get_answer
app = FastAPI(title="🎥 YouTube Q&A with LLM")
# ✅ Allow frontend access (React, Streamlit, etc.)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Query
class YouTubeQuery(BaseModel):
url: str
question: str
# Post Endpoint
@app.post("/youtube_qa")
async def youtube_qa(query: YouTubeQuery):
"""Main API endpoint."""
video_id = extract_video_id(query.url)
if not video_id:
raise HTTPException(status_code=400, detail="Invalid YouTube URL.")
try:
transcript = fetch_transcript(video_id)
if not transcript:
raise HTTPException(status_code=404, detail="No transcript found.")
answer = get_answer(transcript, query.question, video_id)
return {"video_id": video_id, "question": query.question, "answer": answer}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
FRONTEND_DIR = os.path.join(BASE_DIR, "frontend")
app.mount("/static", StaticFiles(directory=FRONTEND_DIR), name="static")
@app.get("/")
def home():
return FileResponse(os.path.join(FRONTEND_DIR, "index.html"))