mike23415 commited on
Commit
98e82be
·
verified ·
1 Parent(s): 304423c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import io
3
- from flask import Flask, request, jsonify
4
  from werkzeug.utils import secure_filename
5
  from PyPDF2 import PdfReader
6
  from docx import Document
@@ -14,36 +14,43 @@ ALLOWED_EXTENSIONS = {"pdf", "docx", "pptx", "txt"}
14
  def allowed_file(filename):
15
  return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
16
 
 
 
 
 
17
  @app.route("/summarize", methods=["POST"])
18
  def summarize():
19
  if "file" not in request.files:
20
  return jsonify({"error": "No file uploaded"}), 400
21
-
22
  file = request.files["file"]
23
 
24
  if file.filename == "":
25
  return jsonify({"error": "No selected file"}), 400
26
-
27
  if not allowed_file(file.filename):
28
  return jsonify({"error": "Unsupported file format"}), 400
29
-
30
  filename = secure_filename(file.filename)
31
  file_content = file.read()
32
-
33
  # Process file based on type
34
  summary = None
35
  file_ext = filename.rsplit(".", 1)[1].lower()
36
-
37
- if file_ext == "pdf":
38
- summary = summarize_pdf(file_content)
39
- elif file_ext == "docx":
40
- summary = summarize_docx(file_content)
41
- elif file_ext == "pptx":
42
- summary = summarize_pptx(file_content)
43
- elif file_ext == "txt":
44
- summary = summarize_txt(file_content)
45
-
46
- return jsonify({"filename": filename, "summary": summary})
 
 
 
47
 
48
  # Summarization functions
49
  def summarize_pdf(file_content):
@@ -58,12 +65,16 @@ def summarize_docx(file_content):
58
 
59
  def summarize_pptx(file_content):
60
  ppt = Presentation(io.BytesIO(file_content))
61
- text = "\n".join([slide.shapes.title.text for slide in ppt.slides if slide.shapes.title])
62
- return text[:500]
 
 
 
 
63
 
64
  def summarize_txt(file_content):
65
  text = file_content.decode("utf-8")
66
  return text[:500]
67
 
68
  if __name__ == "__main__":
69
- app.run(host="0.0.0.0", port=7860)
 
1
  import os
2
  import io
3
+ from flask import Flask, request, jsonify, render_template
4
  from werkzeug.utils import secure_filename
5
  from PyPDF2 import PdfReader
6
  from docx import Document
 
14
  def allowed_file(filename):
15
  return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
16
 
17
+ @app.route("/", methods=["GET"])
18
+ def index():
19
+ return "Document Summarizer API is running! Use /summarize endpoint for POST requests."
20
+
21
  @app.route("/summarize", methods=["POST"])
22
  def summarize():
23
  if "file" not in request.files:
24
  return jsonify({"error": "No file uploaded"}), 400
25
+
26
  file = request.files["file"]
27
 
28
  if file.filename == "":
29
  return jsonify({"error": "No selected file"}), 400
30
+
31
  if not allowed_file(file.filename):
32
  return jsonify({"error": "Unsupported file format"}), 400
33
+
34
  filename = secure_filename(file.filename)
35
  file_content = file.read()
36
+
37
  # Process file based on type
38
  summary = None
39
  file_ext = filename.rsplit(".", 1)[1].lower()
40
+
41
+ try:
42
+ if file_ext == "pdf":
43
+ summary = summarize_pdf(file_content)
44
+ elif file_ext == "docx":
45
+ summary = summarize_docx(file_content)
46
+ elif file_ext == "pptx":
47
+ summary = summarize_pptx(file_content)
48
+ elif file_ext == "txt":
49
+ summary = summarize_txt(file_content)
50
+
51
+ return jsonify({"filename": filename, "summary": summary})
52
+ except Exception as e:
53
+ return jsonify({"error": f"Error processing file: {str(e)}"}), 500
54
 
55
  # Summarization functions
56
  def summarize_pdf(file_content):
 
65
 
66
  def summarize_pptx(file_content):
67
  ppt = Presentation(io.BytesIO(file_content))
68
+ text = []
69
+ for slide in ppt.slides:
70
+ for shape in slide.shapes:
71
+ if hasattr(shape, "text"):
72
+ text.append(shape.text)
73
+ return "\n".join(text)[:500]
74
 
75
  def summarize_txt(file_content):
76
  text = file_content.decode("utf-8")
77
  return text[:500]
78
 
79
  if __name__ == "__main__":
80
+ app.run(host="0.0.0.0", port=7860, debug=True)