HeartInsight / app.py
AmiKim's picture
Update app.py
10324f8 verified
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)})