Spaces:
Build error
Build error
| # backend/app/main.py | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles # StaticFiles 임포트 | |
| import os | |
| from pydantic import BaseModel | |
| from youtube_parser import process_youtube_video_data | |
| from rag_core import perform_rag_query | |
| app = FastAPI() | |
| # CORS 설정: 프론트엔드와 백엔드가 다른 포트에서 실행될 때 필요 | |
| origins = [ | |
| "http://localhost:8080", # Vue 개발 서버 기본 포트 | |
| "http://localhost:5173", # Vue Vite 개발 서버 기본 포트 | |
| "https://sodagraph-po.hf.space", # 여러분의 Hugging Face Space URL | |
| ] | |
| 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") | |
| # ✅ API 라우터 먼저 정의 | |
| async def api_health_check(): | |
| return {"status": "ok", "message": "Backend API is running"} | |
| class VideoProcessRequest(BaseModel): | |
| video_url: str | |
| query: str | |
| async def process_youtube_video(request: VideoProcessRequest): | |
| try: | |
| processed_chunks_with_timestamps = await process_youtube_video_data(request.video_url) | |
| if not processed_chunks_with_timestamps: | |
| return {"message": "자막 또는 내용을 추출할 수 없습니다.", "results": []} | |
| rag_results = await perform_rag_query( | |
| chunks_with_timestamps=processed_chunks_with_timestamps, | |
| query=request.query, | |
| top_k=5 | |
| ) | |
| return { | |
| "status": "success", | |
| "message": "성공적으로 영상을 처리하고 RAG 검색을 수행했습니다.", | |
| "video_url": request.video_url, | |
| "query": request.query, | |
| "results": rag_results | |
| } | |
| except Exception as e: | |
| print(f"ERROR: 서버 처리 중 오류 발생: {str(e)}") | |
| raise HTTPException(status_code=500, detail="서버 처리 중 오류가 발생했습니다. 잠시 후 다시 시도해주세요.") | |
| # ✅ 정적 파일은 마지막에 mount | |
| app.mount("/", StaticFiles(directory=static_files_dir, html=True), name="static") | |
| # 서버 실행을 위한 메인 진입점 (Docker에서는 Uvicorn이 직접 호출하므로 필수는 아님) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| import os | |
| port = int(os.environ.get("PORT", 7860)) # Hugging Face가 전달하는 포트를 우선 사용 | |
| uvicorn.run(app, host="0.0.0.0", port=port) |