vumichien commited on
Commit
9917a58
·
1 Parent(s): 79d472e

output csv

Browse files
Files changed (1) hide show
  1. main.py +11 -7
main.py CHANGED
@@ -131,8 +131,8 @@ async def predict(file: UploadFile = File(...)):
131
  timestamp = int(time.time())
132
  input_file_path = os.path.join(current_dir, "uploads", f"input_{timestamp}.csv")
133
 
134
- # Change output file to Excel format
135
- output_file_path = os.path.join(current_dir, "outputs", f"output_{timestamp}.xlsx")
136
 
137
  try:
138
  with open(input_file_path, "wb") as buffer:
@@ -164,14 +164,18 @@ async def predict(file: UploadFile = File(...)):
164
  output_df.loc[:, COL_STANDARD_SUBJECT] = df_predicted[COL_STANDARD_SUBJECT]
165
  output_df.loc[:, COL_STANDARD_NAME] = df_predicted[COL_STANDARD_NAME]
166
 
167
- # Save as Excel file instead of CSV for better Japanese character support
168
- output_df.to_excel(output_file_path, index=False, engine="openpyxl")
169
 
170
- # Return the Excel file as a download
171
  return FileResponse(
172
  path=output_file_path,
173
- filename=f"output_{Path(file.filename).stem}.xlsx",
174
- media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
 
 
 
 
175
  )
176
 
177
  except Exception as e:
 
131
  timestamp = int(time.time())
132
  input_file_path = os.path.join(current_dir, "uploads", f"input_{timestamp}.csv")
133
 
134
+ # Use CSV format with correct extension
135
+ output_file_path = os.path.join(current_dir, "outputs", f"output_{timestamp}.csv")
136
 
137
  try:
138
  with open(input_file_path, "wb") as buffer:
 
164
  output_df.loc[:, COL_STANDARD_SUBJECT] = df_predicted[COL_STANDARD_SUBJECT]
165
  output_df.loc[:, COL_STANDARD_NAME] = df_predicted[COL_STANDARD_NAME]
166
 
167
+ # Save with Shift-JIS encoding for Japanese Excel compatibility
168
+ output_df.to_csv(output_file_path, index=False, encoding="utf_8_sig")
169
 
170
+ # Return the file as a download with correct content type and headers
171
  return FileResponse(
172
  path=output_file_path,
173
+ filename=f"output_{Path(file.filename).stem}.csv",
174
+ media_type="text/csv",
175
+ headers={
176
+ "Content-Disposition": f'attachment; filename="output_{Path(file.filename).stem}.csv"',
177
+ "Content-Type": "application/x-www-form-urlencoded",
178
+ },
179
  )
180
 
181
  except Exception as e: