Spaces:
Sleeping
Sleeping
Upload UI.py
Browse files
UI.py
CHANGED
|
@@ -3,12 +3,15 @@ from engine import process_question
|
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
-
# PAGE
|
| 7 |
# =========================
|
| 8 |
-
st.set_page_config(
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
st.title("
|
| 11 |
-
st.caption("Ask questions
|
| 12 |
|
| 13 |
# =========================
|
| 14 |
# SESSION STATE
|
|
@@ -20,19 +23,19 @@ if "transcript" not in st.session_state:
|
|
| 20 |
st.session_state.transcript = []
|
| 21 |
|
| 22 |
# =========================
|
| 23 |
-
#
|
| 24 |
# =========================
|
| 25 |
for msg in st.session_state.messages:
|
| 26 |
with st.chat_message(msg["role"]):
|
| 27 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
| 28 |
|
| 29 |
# =========================
|
| 30 |
-
#
|
| 31 |
# =========================
|
| 32 |
-
user_input = st.chat_input("Ask
|
| 33 |
|
| 34 |
if user_input:
|
| 35 |
-
#
|
| 36 |
st.session_state.messages.append(
|
| 37 |
{"role": "user", "content": user_input}
|
| 38 |
)
|
|
@@ -40,8 +43,8 @@ if user_input:
|
|
| 40 |
with st.chat_message("user"):
|
| 41 |
st.markdown(user_input)
|
| 42 |
|
| 43 |
-
# Call
|
| 44 |
-
with st.spinner("
|
| 45 |
try:
|
| 46 |
result = process_question(user_input)
|
| 47 |
except Exception as e:
|
|
@@ -56,48 +59,26 @@ if user_input:
|
|
| 56 |
reply = ""
|
| 57 |
|
| 58 |
if result.get("status") == "ok":
|
| 59 |
-
|
| 60 |
if result.get("message"):
|
| 61 |
-
reply += f"
|
| 62 |
-
|
| 63 |
-
if result.get("note"):
|
| 64 |
-
reply += f"🕒 {result['note']}\n\n"
|
| 65 |
-
|
| 66 |
-
if result.get("data"):
|
| 67 |
-
columns = result.get("columns", [])
|
| 68 |
-
data = result["data"]
|
| 69 |
-
|
| 70 |
-
reply += "### Result\n"
|
| 71 |
-
reply += "| " + " | ".join(columns) + " |\n"
|
| 72 |
-
reply += "| " + " | ".join(["---"] * len(columns)) + " |\n"
|
| 73 |
-
|
| 74 |
-
for row in data[:10]:
|
| 75 |
-
reply += "| " + " | ".join(str(x) for x in row) + " |\n"
|
| 76 |
|
| 77 |
if result.get("sql"):
|
| 78 |
-
reply += "\n
|
| 79 |
-
reply += "<details><summary><b>Generated SQL</b></summary>\n\n"
|
| 80 |
reply += f"```sql\n{result['sql']}\n```"
|
| 81 |
-
reply += "\n</details>"
|
| 82 |
|
| 83 |
else:
|
| 84 |
-
reply = f"❌ {result.get('message'
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
# SAVE TRANSCRIPT ✅ FIXED
|
| 88 |
-
# =========================
|
| 89 |
st.session_state.transcript.append({
|
| 90 |
"timestamp": datetime.utcnow().isoformat(),
|
| 91 |
"question": user_input,
|
| 92 |
-
"reply": reply,
|
| 93 |
"sql": result.get("sql"),
|
| 94 |
-
"result_preview": result.get("data", [])[:10],
|
| 95 |
"error": result.get("message") if result.get("status") != "ok" else None
|
| 96 |
})
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
# SHOW ASSISTANT MESSAGE
|
| 100 |
-
# =========================
|
| 101 |
st.session_state.messages.append(
|
| 102 |
{"role": "assistant", "content": reply}
|
| 103 |
)
|
|
@@ -106,34 +87,25 @@ if user_input:
|
|
| 106 |
st.markdown(reply, unsafe_allow_html=True)
|
| 107 |
|
| 108 |
# =========================
|
| 109 |
-
# DOWNLOAD
|
| 110 |
# =========================
|
| 111 |
-
def
|
| 112 |
lines = []
|
| 113 |
-
for i,
|
| 114 |
lines.append(f"\n--- Query {i} ---")
|
| 115 |
-
lines.append(f"Time: {
|
| 116 |
-
lines.append(f"Question: {
|
| 117 |
-
|
| 118 |
-
if
|
| 119 |
-
lines.append("
|
| 120 |
-
lines.append(entry["reply"])
|
| 121 |
-
|
| 122 |
-
if entry.get("sql"):
|
| 123 |
-
lines.append(f"SQL:\n{entry['sql']}")
|
| 124 |
-
|
| 125 |
-
if entry.get("error"):
|
| 126 |
-
lines.append(f"Error: {entry['error']}")
|
| 127 |
-
|
| 128 |
return "\n".join(lines)
|
| 129 |
|
| 130 |
-
# Show download button only if data exists
|
| 131 |
if st.session_state.transcript:
|
| 132 |
st.divider()
|
| 133 |
st.download_button(
|
| 134 |
-
|
| 135 |
-
data=
|
| 136 |
-
file_name="
|
| 137 |
mime="text/plain",
|
| 138 |
use_container_width=True
|
| 139 |
)
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
+
# PAGE CONFIG
|
| 7 |
# =========================
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Text-to-SQL AI",
|
| 10 |
+
layout="wide"
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
st.title("🧠 Text-to-SQL Assistant")
|
| 14 |
+
st.caption("Ask questions in natural language. I’ll generate SQL using your database metadata.")
|
| 15 |
|
| 16 |
# =========================
|
| 17 |
# SESSION STATE
|
|
|
|
| 23 |
st.session_state.transcript = []
|
| 24 |
|
| 25 |
# =========================
|
| 26 |
+
# CHAT HISTORY
|
| 27 |
# =========================
|
| 28 |
for msg in st.session_state.messages:
|
| 29 |
with st.chat_message(msg["role"]):
|
| 30 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
| 31 |
|
| 32 |
# =========================
|
| 33 |
+
# USER INPUT
|
| 34 |
# =========================
|
| 35 |
+
user_input = st.chat_input("Ask something like: 'Show employees in IT department'")
|
| 36 |
|
| 37 |
if user_input:
|
| 38 |
+
# Display user message
|
| 39 |
st.session_state.messages.append(
|
| 40 |
{"role": "user", "content": user_input}
|
| 41 |
)
|
|
|
|
| 43 |
with st.chat_message("user"):
|
| 44 |
st.markdown(user_input)
|
| 45 |
|
| 46 |
+
# Call engine
|
| 47 |
+
with st.spinner("Generating SQL..."):
|
| 48 |
try:
|
| 49 |
result = process_question(user_input)
|
| 50 |
except Exception as e:
|
|
|
|
| 59 |
reply = ""
|
| 60 |
|
| 61 |
if result.get("status") == "ok":
|
|
|
|
| 62 |
if result.get("message"):
|
| 63 |
+
reply += f"### ✅ Result\n{result['message']}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
if result.get("sql"):
|
| 66 |
+
reply += "### 🧾 Generated SQL\n"
|
|
|
|
| 67 |
reply += f"```sql\n{result['sql']}\n```"
|
|
|
|
| 68 |
|
| 69 |
else:
|
| 70 |
+
reply = f"❌ **Error:** {result.get('message')}"
|
| 71 |
|
| 72 |
+
# Save transcript
|
|
|
|
|
|
|
| 73 |
st.session_state.transcript.append({
|
| 74 |
"timestamp": datetime.utcnow().isoformat(),
|
| 75 |
"question": user_input,
|
| 76 |
+
"reply": reply,
|
| 77 |
"sql": result.get("sql"),
|
|
|
|
| 78 |
"error": result.get("message") if result.get("status") != "ok" else None
|
| 79 |
})
|
| 80 |
|
| 81 |
+
# Show assistant reply
|
|
|
|
|
|
|
| 82 |
st.session_state.messages.append(
|
| 83 |
{"role": "assistant", "content": reply}
|
| 84 |
)
|
|
|
|
| 87 |
st.markdown(reply, unsafe_allow_html=True)
|
| 88 |
|
| 89 |
# =========================
|
| 90 |
+
# DOWNLOAD LOG
|
| 91 |
# =========================
|
| 92 |
+
def download_transcript():
|
| 93 |
lines = []
|
| 94 |
+
for i, t in enumerate(st.session_state.transcript, 1):
|
| 95 |
lines.append(f"\n--- Query {i} ---")
|
| 96 |
+
lines.append(f"Time: {t['timestamp']}")
|
| 97 |
+
lines.append(f"Question: {t['question']}")
|
| 98 |
+
lines.append(f"Reply:\n{t['reply']}")
|
| 99 |
+
if t.get("sql"):
|
| 100 |
+
lines.append(f"SQL:\n{t['sql']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
return "\n".join(lines)
|
| 102 |
|
|
|
|
| 103 |
if st.session_state.transcript:
|
| 104 |
st.divider()
|
| 105 |
st.download_button(
|
| 106 |
+
"⬇️ Download Query Log",
|
| 107 |
+
data=download_transcript(),
|
| 108 |
+
file_name="text_to_sql_log.txt",
|
| 109 |
mime="text/plain",
|
| 110 |
use_container_width=True
|
| 111 |
)
|