Spaces:
Sleeping
Sleeping
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) | |
def greet_json(): | |
return {"v": 0.1} | |
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)}) | |