mike23415 commited on
Commit
3696c1f
·
verified ·
1 Parent(s): 9f6aa3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -64,26 +64,30 @@ def index():
64
 
65
  @app.route("/summarize", methods=["POST"])
66
  def summarize():
67
- """Handle file uploads and summarization."""
68
  logger.info("Summarize endpoint called.")
69
-
 
70
  if "file" not in request.files:
71
  logger.error("No file uploaded.")
72
  return jsonify({"error": "No file uploaded"}), 400
73
-
74
  file = request.files["file"]
 
 
75
  if file.filename == "":
76
  logger.error("No file selected.")
77
  return jsonify({"error": "No selected file"}), 400
78
-
 
79
  if not allowed_file(file.filename):
80
  logger.error(f"Unsupported file format: {file.filename}")
81
  return jsonify({"error": "Unsupported file format"}), 400
82
 
 
83
  filename = secure_filename(file.filename)
84
  file_content = file.read()
85
  file_ext = filename.rsplit(".", 1)[1].lower()
86
-
87
  try:
88
  if file_ext == "pdf":
89
  text = summarize_pdf(file_content)
@@ -94,15 +98,15 @@ def summarize():
94
  elif file_ext == "txt":
95
  text = summarize_txt(file_content)
96
  else:
 
97
  return jsonify({"error": "Unsupported file format"}), 400
98
 
99
- if not text.strip():
100
- return jsonify({"error": "No extractable text found in the document"}), 400
101
-
102
  summary = summarize_text(text)
 
103
  logger.info(f"File {filename} summarized successfully.")
104
  return jsonify({"filename": filename, "summary": summary})
105
-
106
  except Exception as e:
107
  logger.error(f"Error processing file {filename}: {str(e)}")
108
  return jsonify({"error": f"Error processing file: {str(e)}"}), 500
 
64
 
65
  @app.route("/summarize", methods=["POST"])
66
  def summarize():
 
67
  logger.info("Summarize endpoint called.")
68
+
69
+ # Check if a file is in the request
70
  if "file" not in request.files:
71
  logger.error("No file uploaded.")
72
  return jsonify({"error": "No file uploaded"}), 400
73
+
74
  file = request.files["file"]
75
+
76
+ # Check if file is empty
77
  if file.filename == "":
78
  logger.error("No file selected.")
79
  return jsonify({"error": "No selected file"}), 400
80
+
81
+ # Check if file has an allowed extension
82
  if not allowed_file(file.filename):
83
  logger.error(f"Unsupported file format: {file.filename}")
84
  return jsonify({"error": "Unsupported file format"}), 400
85
 
86
+ # Process the file
87
  filename = secure_filename(file.filename)
88
  file_content = file.read()
89
  file_ext = filename.rsplit(".", 1)[1].lower()
90
+
91
  try:
92
  if file_ext == "pdf":
93
  text = summarize_pdf(file_content)
 
98
  elif file_ext == "txt":
99
  text = summarize_txt(file_content)
100
  else:
101
+ logger.error("Unsupported file format received.")
102
  return jsonify({"error": "Unsupported file format"}), 400
103
 
104
+ # Generate summary
 
 
105
  summary = summarize_text(text)
106
+
107
  logger.info(f"File {filename} summarized successfully.")
108
  return jsonify({"filename": filename, "summary": summary})
109
+
110
  except Exception as e:
111
  logger.error(f"Error processing file {filename}: {str(e)}")
112
  return jsonify({"error": f"Error processing file: {str(e)}"}), 500