from nlp import analyze_kakao_csv, get_json_result from fastapi import FastAPI, UploadFile, File from fastapi.responses import JSONResponse from transformers import AutoTokenizer, AutoModelForSequenceClassification import os # 업로드 디렉토리 생성 UPLOAD_DIR = "uploads" os.makedirs(UPLOAD_DIR, exist_ok=True) app = FastAPI() model_name = "nlp04/korean_sentiment_analysis_kcelectra" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) @app.get("/") def greet_json(): return {"v": 0.1} @app.post("/upload/") async def upload_file(file: UploadFile = File(...)): file_location = os.path.join(UPLOAD_DIR, file.filename) try: contents = await file.read() with open(file_location, "wb") as f: f.write(contents) # analyze_kakao_upload_file이 비동기 함수라면 await 필요 results_df = await analyze_kakao_csv(contents, model, tokenizer) if results_df is None: return {"error": "CSV 파일을 읽을 수 없습니다."} else: result_json = get_json_result(results_df, "KCElectra") print(f"\nAnalysis complete! Results saved to the 'results' folder.") return { "filename": file.filename, "saved_to": file_location, "result": result_json } except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)})