Spaces:
Sleeping
Sleeping
Upload engine.py
Browse files
engine.py
CHANGED
|
@@ -737,6 +737,40 @@ def process_question(question):
|
|
| 737 |
"message": humanize(describe_schema()),
|
| 738 |
"data": []
|
| 739 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 740 |
|
| 741 |
# ----------------------------------
|
| 742 |
# Unsupported question check
|
|
@@ -746,7 +780,7 @@ def process_question(question):
|
|
| 746 |
user_q=question,
|
| 747 |
error="Unsupported question"
|
| 748 |
)
|
| 749 |
-
|
| 750 |
"status": "ok",
|
| 751 |
"message": (
|
| 752 |
"That information isn’t available in the system.\n\n"
|
|
@@ -760,8 +794,6 @@ def process_question(question):
|
|
| 760 |
"data": []
|
| 761 |
}
|
| 762 |
|
| 763 |
-
|
| 764 |
-
|
| 765 |
# ----------------------------------
|
| 766 |
# Generate SQL
|
| 767 |
# ----------------------------------
|
|
@@ -855,25 +887,4 @@ def process_question(question):
|
|
| 855 |
"sql": sql,
|
| 856 |
"columns": cols,
|
| 857 |
"data": rows
|
| 858 |
-
}
|
| 859 |
-
|
| 860 |
-
def download_transcript_txt():
|
| 861 |
-
lines = []
|
| 862 |
-
for i, entry in enumerate(st.session_state.transcript, 1):
|
| 863 |
-
lines.append(f"\n--- Query {i} ---")
|
| 864 |
-
lines.append(f"Time: {entry['timestamp']}")
|
| 865 |
-
lines.append(f"Question: {entry['question']}")
|
| 866 |
-
|
| 867 |
-
if entry.get("sql"):
|
| 868 |
-
lines.append(f"SQL: {entry['sql']}")
|
| 869 |
-
|
| 870 |
-
if entry.get("result_preview"):
|
| 871 |
-
lines.append(f"Result Preview: {entry['result_preview']}")
|
| 872 |
-
|
| 873 |
-
if entry.get("error"):
|
| 874 |
-
lines.append(f"Error: {entry['error']}")
|
| 875 |
-
|
| 876 |
-
return "\n".join(lines)
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
|
|
|
| 737 |
"message": humanize(describe_schema()),
|
| 738 |
"data": []
|
| 739 |
}
|
| 740 |
+
# ----------------------------------
|
| 741 |
+
# # LAST DATA / RECENT DATA HANDLING
|
| 742 |
+
# # ----------------------------------
|
| 743 |
+
if any(x in q for x in ["last data", "latest data"]):
|
| 744 |
+
return {
|
| 745 |
+
"status": "ok",
|
| 746 |
+
"message": f"Latest data available is from {get_latest_data_date()}",
|
| 747 |
+
"data": []
|
| 748 |
+
}
|
| 749 |
+
|
| 750 |
+
if "last" in q and "day" in q and ("visit" in q or "admission" in q):
|
| 751 |
+
sql = """
|
| 752 |
+
SELECT subject_id, admittime
|
| 753 |
+
FROM admissions
|
| 754 |
+
WHERE admittime >= date(
|
| 755 |
+
(SELECT MAX(admittime) FROM admissions),
|
| 756 |
+
'-30 days'
|
| 757 |
+
)
|
| 758 |
+
ORDER BY admittime DESC
|
| 759 |
+
"""
|
| 760 |
+
cols, rows = run_query(sql)
|
| 761 |
+
|
| 762 |
+
log_interaction(
|
| 763 |
+
user_q=question,
|
| 764 |
+
sql=sql,
|
| 765 |
+
result=rows
|
| 766 |
+
)
|
| 767 |
+
|
| 768 |
+
return {
|
| 769 |
+
"status": "ok",
|
| 770 |
+
"sql": sql,
|
| 771 |
+
"columns": cols,
|
| 772 |
+
"data": rows
|
| 773 |
+
}
|
| 774 |
|
| 775 |
# ----------------------------------
|
| 776 |
# Unsupported question check
|
|
|
|
| 780 |
user_q=question,
|
| 781 |
error="Unsupported question"
|
| 782 |
)
|
| 783 |
+
return {
|
| 784 |
"status": "ok",
|
| 785 |
"message": (
|
| 786 |
"That information isn’t available in the system.\n\n"
|
|
|
|
| 794 |
"data": []
|
| 795 |
}
|
| 796 |
|
|
|
|
|
|
|
| 797 |
# ----------------------------------
|
| 798 |
# Generate SQL
|
| 799 |
# ----------------------------------
|
|
|
|
| 887 |
"sql": sql,
|
| 888 |
"columns": cols,
|
| 889 |
"data": rows
|
| 890 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|