from flask import Flask, render_template, request, jsonify, url_for import pandas as pd import os import json app = Flask(__name__) app.config["UPLOAD_FOLDER"] = "uploads" app.config["IMAGES_FOLDER"] = os.path.join("static", "images") if not os.path.exists(app.config["UPLOAD_FOLDER"]): os.makedirs(app.config["UPLOAD_FOLDER"]) with open("demo_answers.json") as f: answers = json.load(f) analysis_requested = False current_analysis_key = None @app.route("/") def index(): global analysis_requested, current_analysis_key analysis_requested = False current_analysis_key = None return render_template("index.html") @app.route("/upload", methods=["POST"]) def upload_file(): if "file" not in request.files: return jsonify({"error": "No file part"}), 400 file = request.files["file"] if file.filename == "": return jsonify({"error": "No selected file"}), 400 if file and file.filename.endswith(".csv"): file_path = os.path.join(app.config["UPLOAD_FOLDER"], file.filename) file.save(file_path) df = pd.read_csv(file_path) summary = df.describe(include="all").to_html(classes="table table-striped") return jsonify( { "summary": summary, "message": "File uploaded successfully. Let's see what we have here...", } ) else: return jsonify({"error": "Unsupported file type"}), 400 @app.route("/chat", methods=["POST"]) def chat(): global analysis_requested, current_analysis_key user_message = request.json.get("message").lower() if analysis_requested and user_message == "yes": return add_insight() if user_message == "proceed with the analysis of the data": analysis_requested = True current_analysis_key = "01" response_text = ( "Okay, let's start the analysis. " + answers[current_analysis_key]["answer"]["answer"] + " Would you like to add this insight to the dashboard?" ) return jsonify({"response": response_text}) if user_message == "thanks": return jsonify({"response": "You're welcome! It was a pleasure helping you!"}) # Find the answer by matching the user message with questions in the JSON file matched_entry = next( ( entry for key, entry in answers.items() if entry["question"].lower() == user_message ), None, ) if matched_entry: response = matched_entry["answer"]["answer"] justification = matched_entry["answer"]["justification"] image_filename = ( f"{list(answers.keys())[list(answers.values()).index(matched_entry)]}.jpg" ) image_path = os.path.join(app.config["IMAGES_FOLDER"], image_filename) if os.path.exists(image_path): image_url = url_for("static", filename=f"images/{image_filename}") else: image_url = None else: response = "I don't have an answer for that question." justification = None image_url = None return jsonify( {"response": response, "justification": justification, "image_url": image_url} ) @app.route("/explore", methods=["POST"]) def explore(): key = request.json.get("key") matched_entry = answers.get(key) if matched_entry: response = matched_entry["answer"]["answer"] justification = matched_entry["answer"]["justification"] image_filename = f"{key}.jpg" image_path = os.path.join(app.config["IMAGES_FOLDER"], image_filename) if os.path.exists(image_path): image_url = url_for("static", filename=f"images/{image_filename}") else: image_url = None else: response = "No more data to explore." justification = None image_url = None return jsonify( { "response": response, "justification": justification, "image_url": image_url, "key": key, } ) @app.route("/add_insight", methods=["POST"]) def add_insight(): global analysis_requested, current_analysis_key if analysis_requested and current_analysis_key: key = current_analysis_key matched_entry = answers.get(key) if matched_entry: response = matched_entry["answer"]["answer"] justification = matched_entry["answer"]["justification"] image_filename = f"{key}.jpg" image_path = os.path.join(app.config["IMAGES_FOLDER"], image_filename) if os.path.exists(image_path): image_url = url_for("static", filename=f"images/{image_filename}") else: image_url = None next_key = str(int(key) + 1).zfill(2) if next_key in answers: current_analysis_key = next_key next_response = ( answers[next_key]["answer"]["answer"] + " Would you like to add this insight to the dashboard?" ) return jsonify( { "response": response, "justification": justification, "image_url": image_url, "next_response": next_response, "key": key, } ) else: current_analysis_key = None final_summary = "Okay! It seems like there are no more interesting trends to explore in this dataset. Summary: Most of the incidents are hardware-related and they originated in Australia. They mostly are due to a printer not working. Recommended action: Fix the particular printer that's not working." return jsonify( { "response": response, "justification": justification, "image_url": image_url, "key": key, "next_response": final_summary, } ) else: return jsonify({"response": "No more data to explore."}) else: return jsonify( { "response": "Please request to proceed with the analysis of the data first." } ) if __name__ == "__main__": app.run(debug=True, port=7860, host="0.0.0.0")