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)}) | |