# ./backend/app/main.py from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles import os from pydantic import BaseModel from youtube_parser import process_youtube_video_data from rag_core import perform_rag_and_generate # 수정된 함수를 임포트 app = FastAPI() # CORS 설정 origins = [ "http://localhost:8080", "http://localhost:5173", "https://sodagraph-po.hf.space", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 경로 설정 current_file_dir = os.path.dirname(os.path.abspath(__file__)) project_root_dir = os.path.join(current_file_dir, "..", "..") static_files_dir = os.path.join(project_root_dir, "static") class VideoProcessRequest(BaseModel): video_url: str query: str ollama_model_name: str = "hf.co/DevQuasar/naver-hyperclovax.HyperCLOVAX-SEED-Text-Instruct-0.5B-GGUF:F16" # ✅ 유튜브 영상 처리 API @app.post("/api/process_youtube_video") async def process_youtube_video(request: VideoProcessRequest): try: # 1. 유튜브 영상에서 자막/콘텐츠 추출 processed_chunks_with_timestamps = await process_youtube_video_data(request.video_url) if not processed_chunks_with_timestamps: return {"message": "자막 또는 내용을 추출할 수 없습니다.", "results": []} # 2. RAG 프로세스 실행 (검색 + 생성) rag_result = await perform_rag_and_generate( query=request.query, chunks_with_timestamps=processed_chunks_with_timestamps, ollama_model_name=request.ollama_model_name, top_k=50 ) # 3. 최종 결과 반환 return { **rag_result, # rag_core에서 반환된 결과 딕셔너리를 그대로 사용 "video_url": request.video_url, "query": request.query, "ollama_model_used": request.ollama_model_name, } except Exception as e: print(f"ERROR: 서버 처리 중 오류 발생: {str(e)}") # 실제 Exception의 세부 정보를 로깅하는 것이 좋음 raise HTTPException(status_code=500, detail=f"서버 처리 중 오류가 발생했습니다: {e}") # ✅ 정적 파일은 마지막에 mount app.mount("/", StaticFiles(directory=static_files_dir, html=True), name="static") # 서버 실행을 위한 메인 진입점 if __name__ == "__main__": import uvicorn port = int(os.environ.get("PORT", 7860)) uvicorn.run(app, host="0.0.0.0", port=port)