combine posts and comments endpoint
This commit is contained in:
@@ -7,27 +7,29 @@ app = Flask(__name__)
|
|||||||
# Allow for CORS from localhost:5173
|
# Allow for CORS from localhost:5173
|
||||||
CORS(app, resources={r"/*": {"origins": "http://localhost:5173"}})
|
CORS(app, resources={r"/*": {"origins": "http://localhost:5173"}})
|
||||||
|
|
||||||
@app.route('/upload_posts', methods=['POST'])
|
@app.route('/upload', methods=['POST'])
|
||||||
def upload_data():
|
def upload_data():
|
||||||
if "file" not in request.files:
|
if "posts" not in request.files or "comments" not in request.files:
|
||||||
return jsonify({"error": "No file part in the request"}), 400
|
return jsonify({"error": "Missing posts or comments file"}), 400
|
||||||
|
|
||||||
file = request.files["file"]
|
post_file = request.files["posts"]
|
||||||
|
comment_file = request.files["comments"]
|
||||||
|
|
||||||
if file.filename == "":
|
if post_file.filename == "" or comment_file.filename == "":
|
||||||
return jsonify({"error": "Empty filename"}), 400
|
return jsonify({"error": "Empty filename"}), 400
|
||||||
|
|
||||||
if not file.filename.endswith('.jsonl'):
|
if not post_file.filename.endswith('.jsonl') or not comment_file.filename.endswith('.jsonl'):
|
||||||
return jsonify({"error": "Invalid file type. Only .jsonl files are allowed."}), 400
|
return jsonify({"error": "Invalid file type. Only .jsonl files are allowed."}), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
df = pd.read_json(file, lines=True)
|
posts_df = pd.read_json(post_file, lines=True)
|
||||||
|
comments_df = pd.read_json(comment_file, lines=True)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return jsonify({"error": f"Failed to read JSONL file: {str(e)}"}), 400
|
return jsonify({"error": f"Failed to read JSONL file: {str(e)}"}), 400
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500
|
return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500
|
||||||
|
|
||||||
return jsonify({"message": "File uploaded successfully", "data_preview": df.head().to_dict(orient='records')}), 200
|
return jsonify({"message": "File uploaded successfully", "posts_count": len(posts_df), "comments_count": len(comments_df)}), 200
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
Reference in New Issue
Block a user