fix: topic file upload functionality in front and backend

This commit is contained in:
2026-02-09 19:29:54 +00:00
parent 2666670f56
commit 645d2fdfdb
2 changed files with 15 additions and 9 deletions

View File

@@ -7,20 +7,22 @@ import StatsStyling from "../styles/stats_styling";
const styles = StatsStyling; const styles = StatsStyling;
const UploadPage = () => { const UploadPage = () => {
let postFile: File | undefined let postFile: File | undefined;
let commentFile: File | undefined let commentFile: File | undefined;
let topicBucketFile: File | undefined;
const [returnMessage, setReturnMessage] = useState('') const [returnMessage, setReturnMessage] = useState('')
const navigate = useNavigate() const navigate = useNavigate()
const uploadFiles = async () => { const uploadFiles = async () => {
if (!postFile || !commentFile) { if (!postFile || !commentFile || !topicBucketFile) {
alert('Please select both files before uploading.') alert('Please upload all files before uploading.')
return return
} }
const formData = new FormData() const formData = new FormData()
formData.append('posts', postFile) formData.append('posts', postFile)
formData.append('comments', commentFile) formData.append('comments', commentFile)
formData.append('topics', topicBucketFile)
try { try {
const response = await axios.post('http://localhost:5000/upload', formData, { const response = await axios.post('http://localhost:5000/upload', formData, {
@@ -40,11 +42,15 @@ const UploadPage = () => {
<div style={{...styles.container, ...styles.grid, margin: "0"}}> <div style={{...styles.container, ...styles.grid, margin: "0"}}>
<div style={{ ...styles.card }}> <div style={{ ...styles.card }}>
<h2 style={{color: "black" }}>Posts File</h2> <h2 style={{color: "black" }}>Posts File</h2>
<input type="file" onChange={(e) => postFile = e.target.files?.[0]}></input> <input style={{color: "black" }} type="file" onChange={(e) => postFile = e.target.files?.[0]}></input>
</div> </div>
<div style={{ ...styles.card }}> <div style={{ ...styles.card }}>
<h2 style={{color: "black" }}>Comments File</h2> <h2 style={{color: "black" }}>Comments File</h2>
<input type="file" onChange={(e) => commentFile = e.target.files?.[0]}></input> <input style={{color: "black" }} type="file" onChange={(e) => commentFile = e.target.files?.[0]}></input>
</div>
<div style={{ ...styles.card }}>
<h2 style={{color: "black" }}>Topic Buckets File</h2>
<input style={{color: "black" }} type="file" onChange={(e) => topicBucketFile = e.target.files?.[0]}></input>
</div> </div>
<button onClick={uploadFiles}>Upload</button> <button onClick={uploadFiles}>Upload</button>

View File

@@ -20,17 +20,17 @@ stat_obj = StatGen(posts_df, comments_df, domain_topics)
@app.route('/upload', methods=['POST']) @app.route('/upload', methods=['POST'])
def upload_data(): def upload_data():
if "posts" not in request.files or "comments" not in request.files or "topics" not in request.form: if "posts" not in request.files or "comments" not in request.files or "topics" not in request.files:
return jsonify({"error": "Missing required files or form data"}), 400 return jsonify({"error": "Missing required files or form data"}), 400
post_file = request.files["posts"] post_file = request.files["posts"]
comment_file = request.files["comments"] comment_file = request.files["comments"]
topic_file = request.form["topics"] topic_file = request.files["topics"]
if post_file.filename == "" or comment_file.filename == "" or topic_file == "": if post_file.filename == "" or comment_file.filename == "" or topic_file == "":
return jsonify({"error": "Empty filename"}), 400 return jsonify({"error": "Empty filename"}), 400
if not post_file.filename.endswith('.jsonl') or not comment_file.filename.endswith('.jsonl') or not topic_file.endswith('.json'): if not post_file.filename.endswith('.jsonl') or not comment_file.filename.endswith('.jsonl') or not topic_file.filename.endswith('.json'):
return jsonify({"error": "Invalid file type. Only .jsonl and .json files are allowed."}), 400 return jsonify({"error": "Invalid file type. Only .jsonl and .json files are allowed."}), 400
try: try: