Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,9 +13,15 @@ if not os.path.exists(app.config["UPLOAD_FOLDER"]):
|
|
| 13 |
with open("demo_answers.json") as f:
|
| 14 |
answers = json.load(f)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@app.route("/")
|
| 18 |
def index():
|
|
|
|
|
|
|
|
|
|
| 19 |
return render_template("index.html")
|
| 20 |
|
| 21 |
|
|
@@ -44,7 +50,22 @@ def upload_file():
|
|
| 44 |
|
| 45 |
@app.route("/chat", methods=["POST"])
|
| 46 |
def chat():
|
|
|
|
| 47 |
user_message = request.json.get("message").lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Find the answer by matching the user message with questions in the JSON file
|
| 49 |
matched_entry = next(
|
| 50 |
(
|
|
@@ -107,5 +128,60 @@ def explore():
|
|
| 107 |
)
|
| 108 |
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
if __name__ == "__main__":
|
| 111 |
-
app.run(host="0.0.0.0"
|
|
|
|
| 13 |
with open("demo_answers.json") as f:
|
| 14 |
answers = json.load(f)
|
| 15 |
|
| 16 |
+
analysis_requested = False
|
| 17 |
+
current_analysis_key = None
|
| 18 |
+
|
| 19 |
|
| 20 |
@app.route("/")
|
| 21 |
def index():
|
| 22 |
+
global analysis_requested, current_analysis_key
|
| 23 |
+
analysis_requested = False
|
| 24 |
+
current_analysis_key = None
|
| 25 |
return render_template("index.html")
|
| 26 |
|
| 27 |
|
|
|
|
| 50 |
|
| 51 |
@app.route("/chat", methods=["POST"])
|
| 52 |
def chat():
|
| 53 |
+
global analysis_requested, current_analysis_key
|
| 54 |
user_message = request.json.get("message").lower()
|
| 55 |
+
|
| 56 |
+
if analysis_requested and user_message == "yes":
|
| 57 |
+
return add_insight()
|
| 58 |
+
|
| 59 |
+
if user_message == "proceed with the analysis of the data":
|
| 60 |
+
analysis_requested = True
|
| 61 |
+
current_analysis_key = "01"
|
| 62 |
+
response_text = (
|
| 63 |
+
"Okay, let's start the analysis. "
|
| 64 |
+
+ answers[current_analysis_key]["answer"]["answer"]
|
| 65 |
+
+ " Would you like to add this insight to the dashboard?"
|
| 66 |
+
)
|
| 67 |
+
return jsonify({"response": response_text})
|
| 68 |
+
|
| 69 |
# Find the answer by matching the user message with questions in the JSON file
|
| 70 |
matched_entry = next(
|
| 71 |
(
|
|
|
|
| 128 |
)
|
| 129 |
|
| 130 |
|
| 131 |
+
@app.route("/add_insight", methods=["POST"])
|
| 132 |
+
def add_insight():
|
| 133 |
+
global analysis_requested, current_analysis_key
|
| 134 |
+
if analysis_requested and current_analysis_key:
|
| 135 |
+
key = current_analysis_key
|
| 136 |
+
matched_entry = answers.get(key)
|
| 137 |
+
|
| 138 |
+
if matched_entry:
|
| 139 |
+
response = matched_entry["answer"]["answer"]
|
| 140 |
+
justification = matched_entry["answer"]["justification"]
|
| 141 |
+
image_filename = f"{key}.jpg"
|
| 142 |
+
image_path = os.path.join(app.config["IMAGES_FOLDER"], image_filename)
|
| 143 |
+
|
| 144 |
+
if os.path.exists(image_path):
|
| 145 |
+
image_url = url_for("static", filename=f"images/{image_filename}")
|
| 146 |
+
else:
|
| 147 |
+
image_url = None
|
| 148 |
+
|
| 149 |
+
next_key = str(int(key) + 1).zfill(2)
|
| 150 |
+
if next_key in answers:
|
| 151 |
+
current_analysis_key = next_key
|
| 152 |
+
next_response = (
|
| 153 |
+
answers[next_key]["answer"]["answer"]
|
| 154 |
+
+ " Would you like to add this insight to the dashboard?"
|
| 155 |
+
)
|
| 156 |
+
return jsonify(
|
| 157 |
+
{
|
| 158 |
+
"response": response,
|
| 159 |
+
"justification": justification,
|
| 160 |
+
"image_url": image_url,
|
| 161 |
+
"next_response": next_response,
|
| 162 |
+
"key": key,
|
| 163 |
+
}
|
| 164 |
+
)
|
| 165 |
+
else:
|
| 166 |
+
current_analysis_key = None
|
| 167 |
+
return jsonify(
|
| 168 |
+
{
|
| 169 |
+
"response": response,
|
| 170 |
+
"justification": justification,
|
| 171 |
+
"image_url": image_url,
|
| 172 |
+
"key": key,
|
| 173 |
+
"next_response": None,
|
| 174 |
+
}
|
| 175 |
+
)
|
| 176 |
+
else:
|
| 177 |
+
return jsonify({"response": "No more data to explore."})
|
| 178 |
+
else:
|
| 179 |
+
return jsonify(
|
| 180 |
+
{
|
| 181 |
+
"response": "Please request to proceed with the analysis of the data first."
|
| 182 |
+
}
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
if __name__ == "__main__":
|
| 187 |
+
app.run(debug=True, port=7860, host="0.0.0.0")
|