Spaces:
Sleeping
Sleeping
File size: 6,485 Bytes
0363714 95f2c92 0363714 95f2c92 0363714 95f2c92 0363714 4ac5566 0363714 95f2c92 a1e737c 95f2c92 a1e737c 95f2c92 0363714 95f2c92 0363714 4ac5566 2552550 0363714 a1e737c 0363714 a1e737c c8d39b7 0363714 95f2c92 2552550 4ac5566 2552550 a1e737c c8d39b7 a1e737c 0edc94d a1e737c 95f2c92 a1e737c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
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")
|