ssboost commited on
Commit
5ad395a
·
verified ·
1 Parent(s): c73cf42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -4
app.py CHANGED
@@ -75,18 +75,21 @@ def extract_keywords(product_names):
75
 
76
  @app.post("/extract_keywords_from_file/")
77
  async def extract_keywords_from_file(file: UploadFile = File(...)):
 
78
  try:
79
  contents = await file.read()
80
  df = pd.read_excel(io.BytesIO(contents), usecols="D", skiprows=2, nrows=1997, engine='openpyxl')
81
  if df.empty:
82
- raise HTTPException(status_code=400, detail="No data found in the specified range.")
83
  unique_product_names = df.iloc[:, 0].dropna().astype(str).unique().tolist()
 
84
  file_path = extract_keywords(unique_product_names)
85
- return FileResponse(file_path, media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', filename=file_path.split("/")[-1])
86
  except Exception as e:
87
- raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
88
  finally:
89
- shutil.rmtree(temp_dir)
 
90
 
91
  @app.post("/extract_keywords_from_text/")
92
  async def extract_keywords_from_text(text: str):
 
75
 
76
  @app.post("/extract_keywords_from_file/")
77
  async def extract_keywords_from_file(file: UploadFile = File(...)):
78
+ temp_dir = None # temp_dir 변수를 None으로 초기화
79
  try:
80
  contents = await file.read()
81
  df = pd.read_excel(io.BytesIO(contents), usecols="D", skiprows=2, nrows=1997, engine='openpyxl')
82
  if df.empty:
83
+ raise HTTPException(status_code=400, detail="지정된 범위 내에 데이터가 없습니다.")
84
  unique_product_names = df.iloc[:, 0].dropna().astype(str).unique().tolist()
85
+ temp_dir = tempfile.mkdtemp() # 임시 디렉토리 생성
86
  file_path = extract_keywords(unique_product_names)
87
+ return FileResponse(file_path, media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', filename=os.path.basename(file_path))
88
  except Exception as e:
89
+ raise HTTPException(status_code=500, detail=f"오류가 발생했습니다: {str(e)}")
90
  finally:
91
+ if temp_dir and os.path.exists(temp_dir): # temp_dir이 None이 아니고 존재할 때만 삭제
92
+ shutil.rmtree(temp_dir)
93
 
94
  @app.post("/extract_keywords_from_text/")
95
  async def extract_keywords_from_text(text: str):