Spaces:
Sleeping
Sleeping
Changes in file structures.
Browse files
app.py
DELETED
|
@@ -1,109 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
-
import uuid
|
| 4 |
-
|
| 5 |
-
st.set_page_config(page_title="Text2SQL Agent", layout="wide")
|
| 6 |
-
|
| 7 |
-
BACKEND_URL = "http://localhost:8000"
|
| 8 |
-
|
| 9 |
-
if "user_id" not in st.session_state:
|
| 10 |
-
st.session_state.user_id = str(uuid.uuid4())[:8]
|
| 11 |
-
|
| 12 |
-
if "thread_id" not in st.session_state:
|
| 13 |
-
st.session_state.thread_id = str(uuid.uuid4())
|
| 14 |
-
|
| 15 |
-
if "db_connected" not in st.session_state:
|
| 16 |
-
st.session_state.db_connected = False
|
| 17 |
-
|
| 18 |
-
if "connection_url" not in st.session_state:
|
| 19 |
-
st.session_state.connection_url = ""
|
| 20 |
-
|
| 21 |
-
if "messages" not in st.session_state:
|
| 22 |
-
st.session_state.messages = []
|
| 23 |
-
|
| 24 |
-
st.sidebar.markdown("---")
|
| 25 |
-
st.sidebar.subheader("Database")
|
| 26 |
-
|
| 27 |
-
connection_url_input = st.sidebar.text_input(
|
| 28 |
-
"Database connection URL",
|
| 29 |
-
value=st.session_state.connection_url,
|
| 30 |
-
type="password",
|
| 31 |
-
disabled=st.session_state.db_connected,
|
| 32 |
-
placeholder="postgresql://user:pass@host:5432/dbname",
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
if not st.session_state.db_connected:
|
| 36 |
-
if st.sidebar.button("Connect database", use_container_width=True):
|
| 37 |
-
if not connection_url_input.strip():
|
| 38 |
-
st.sidebar.error("Enter a connection URL first.")
|
| 39 |
-
else:
|
| 40 |
-
with st.spinner("Ingesting schema..."):
|
| 41 |
-
try:
|
| 42 |
-
resp = requests.post(
|
| 43 |
-
f"{BACKEND_URL}/upload",
|
| 44 |
-
json={
|
| 45 |
-
"connection_url": connection_url_input,
|
| 46 |
-
"user_id": st.session_state.user_id,
|
| 47 |
-
},
|
| 48 |
-
timeout=60,
|
| 49 |
-
)
|
| 50 |
-
resp.raise_for_status()
|
| 51 |
-
st.session_state.connection_url = connection_url_input
|
| 52 |
-
st.session_state.db_connected = True
|
| 53 |
-
st.sidebar.success("Database connected.")
|
| 54 |
-
st.rerun()
|
| 55 |
-
except requests.exceptions.RequestException as e:
|
| 56 |
-
st.sidebar.error(f"Upload failed: {e}")
|
| 57 |
-
else:
|
| 58 |
-
st.sidebar.success("Database connected.")
|
| 59 |
-
if st.sidebar.button("Disconnect / change database", use_container_width=True):
|
| 60 |
-
st.session_state.db_connected = False
|
| 61 |
-
st.session_state.connection_url = ""
|
| 62 |
-
st.session_state.messages = []
|
| 63 |
-
st.session_state.thread_id = str(uuid.uuid4())
|
| 64 |
-
st.rerun()
|
| 65 |
-
|
| 66 |
-
st.sidebar.markdown("---")
|
| 67 |
-
if st.sidebar.button("Clear conversation", use_container_width=True):
|
| 68 |
-
st.session_state.messages = []
|
| 69 |
-
st.session_state.thread_id = str(uuid.uuid4())
|
| 70 |
-
st.rerun()
|
| 71 |
-
|
| 72 |
-
st.title("Text2SQL Agent")
|
| 73 |
-
|
| 74 |
-
if not st.session_state.db_connected:
|
| 75 |
-
st.info("Connect a database from the sidebar to start chatting.")
|
| 76 |
-
st.stop()
|
| 77 |
-
|
| 78 |
-
for msg in st.session_state.messages:
|
| 79 |
-
with st.chat_message(msg["role"]):
|
| 80 |
-
st.markdown(msg["content"])
|
| 81 |
-
|
| 82 |
-
user_input = st.chat_input("Ask a question about your data...")
|
| 83 |
-
|
| 84 |
-
if user_input:
|
| 85 |
-
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 86 |
-
with st.chat_message("user"):
|
| 87 |
-
st.markdown(user_input)
|
| 88 |
-
|
| 89 |
-
with st.chat_message("assistant"):
|
| 90 |
-
with st.spinner("Thinking..."):
|
| 91 |
-
try:
|
| 92 |
-
resp = requests.post(
|
| 93 |
-
f"{BACKEND_URL}/chat",
|
| 94 |
-
json={
|
| 95 |
-
"connection_url": st.session_state.connection_url,
|
| 96 |
-
"message": user_input,
|
| 97 |
-
"user_id": st.session_state.user_id,
|
| 98 |
-
"thread_id": st.session_state.thread_id,
|
| 99 |
-
},
|
| 100 |
-
timeout=120,
|
| 101 |
-
)
|
| 102 |
-
resp.raise_for_status()
|
| 103 |
-
answer = resp.json().get("response", "No response received.")
|
| 104 |
-
except requests.exceptions.RequestException as e:
|
| 105 |
-
answer = f"Error reaching the agent: {e}"
|
| 106 |
-
|
| 107 |
-
st.markdown(answer)
|
| 108 |
-
|
| 109 |
-
st.session_state.messages.append({"role": "assistant", "content": answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|