Spaces:
Sleeping
Sleeping
Commit
·
396b62b
1
Parent(s):
01da756
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,55 +55,6 @@ def library():
|
|
| 55 |
|
| 56 |
#-------
|
| 57 |
|
| 58 |
-
# Define upload directory inside Hugging Face's writable folder
|
| 59 |
-
UPLOAD_FOLDER = "/home/user/app/uploads"
|
| 60 |
-
BOOKS_JSON = "/home/user/app/books.json"
|
| 61 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 62 |
-
|
| 63 |
-
# Ensure books.json exists
|
| 64 |
-
if not os.path.exists(BOOKS_JSON):
|
| 65 |
-
with open(BOOKS_JSON, "w") as f:
|
| 66 |
-
json.dump([], f)
|
| 67 |
-
|
| 68 |
-
# Upload route (Admin only)
|
| 69 |
-
@app.route('/upload', methods=['POST'])
|
| 70 |
-
def upload_book():
|
| 71 |
-
admin_user = request.form.get('adminUser')
|
| 72 |
-
admin_pass = request.form.get('adminPass')
|
| 73 |
-
|
| 74 |
-
if admin_user != "admin" or admin_pass != "password":
|
| 75 |
-
return jsonify({"error": "Unauthorized"}), 403
|
| 76 |
-
|
| 77 |
-
title = request.form.get('title')
|
| 78 |
-
description = request.form.get('description')
|
| 79 |
-
file = request.files['file']
|
| 80 |
-
|
| 81 |
-
if not file:
|
| 82 |
-
return jsonify({"error": "No file provided"}), 400
|
| 83 |
-
|
| 84 |
-
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
|
| 85 |
-
file.save(filepath)
|
| 86 |
-
|
| 87 |
-
# Save book details in JSON
|
| 88 |
-
with open(BOOKS_JSON, "r+") as f:
|
| 89 |
-
books = json.load(f)
|
| 90 |
-
books.append({"title": title, "description": description, "file": file.filename})
|
| 91 |
-
f.seek(0)
|
| 92 |
-
json.dump(books, f, indent=4)
|
| 93 |
-
|
| 94 |
-
return jsonify({"message": "Book uploaded successfully!"})
|
| 95 |
-
|
| 96 |
-
# Route to get book list
|
| 97 |
-
@app.route('/books', methods=['GET'])
|
| 98 |
-
def get_books():
|
| 99 |
-
with open(BOOKS_JSON, "r") as f:
|
| 100 |
-
books = json.load(f)
|
| 101 |
-
return jsonify(books)
|
| 102 |
-
|
| 103 |
-
# Serve uploaded files
|
| 104 |
-
@app.route('/uploads/<filename>')
|
| 105 |
-
def serve_file(filename):
|
| 106 |
-
return send_from_directory(UPLOAD_FOLDER, filename)
|
| 107 |
#-----
|
| 108 |
|
| 109 |
@app.route("/chat", methods=["POST"])
|
|
|
|
| 55 |
|
| 56 |
#-------
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
#-----
|
| 59 |
|
| 60 |
@app.route("/chat", methods=["POST"])
|