Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,171 +1,102 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
# β
|
| 6 |
BACKEND_URL = ""
|
| 7 |
|
| 8 |
st.set_page_config(page_title="PDF Assistant", page_icon="π", layout="wide")
|
| 9 |
|
| 10 |
-
# ---------------- CSS
|
| 11 |
st.markdown("""
|
| 12 |
<style>
|
| 13 |
-
:root {
|
| 14 |
-
--primary-color: #1e3a8a;
|
| 15 |
-
--background-color: #0e1117;
|
| 16 |
-
--secondary-background-color: #1a1d29;
|
| 17 |
-
--text-color: #f0f2f6;
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
.chat-user {
|
| 21 |
background: #2d3748;
|
| 22 |
padding: 12px;
|
| 23 |
-
border-radius: 10px
|
| 24 |
-
margin: 6px 0 6px auto;
|
| 25 |
-
max-width: 85%;
|
| 26 |
text-align: right;
|
| 27 |
-
color: var(--text-color);
|
| 28 |
}
|
| 29 |
.chat-bot {
|
| 30 |
-
background:
|
| 31 |
padding: 12px;
|
| 32 |
-
border-radius: 10px
|
| 33 |
-
|
| 34 |
-
max-width: 85%;
|
| 35 |
-
text-align: left;
|
| 36 |
-
color: #ffffff;
|
| 37 |
}
|
| 38 |
-
|
| 39 |
.sources {
|
| 40 |
font-size: 0.8em;
|
| 41 |
opacity: 0.7;
|
| 42 |
-
margin-top: 10px;
|
| 43 |
-
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
| 44 |
-
padding-top: 5px;
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
.footer {
|
| 48 |
-
position: fixed;
|
| 49 |
-
left: 0;
|
| 50 |
-
bottom: 0;
|
| 51 |
-
width: 100%;
|
| 52 |
-
background-color: var(--secondary-background-color);
|
| 53 |
-
color: var(--text-color);
|
| 54 |
-
text-align: center;
|
| 55 |
-
padding: 10px;
|
| 56 |
-
font-size: 0.85em;
|
| 57 |
-
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
| 58 |
-
}
|
| 59 |
-
.footer a {
|
| 60 |
-
color: var(--primary-color);
|
| 61 |
-
text-decoration: none;
|
| 62 |
-
font-weight: bold;
|
| 63 |
-
}
|
| 64 |
-
.footer a:hover {
|
| 65 |
-
text-decoration: underline;
|
| 66 |
}
|
| 67 |
</style>
|
| 68 |
""", unsafe_allow_html=True)
|
| 69 |
|
| 70 |
-
# ----------------
|
| 71 |
if "chat" not in st.session_state:
|
| 72 |
st.session_state.chat = []
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
st.session_state.uploaded_file_name = None
|
| 76 |
-
|
| 77 |
if "uploader_key" not in st.session_state:
|
| 78 |
st.session_state.uploader_key = 0
|
| 79 |
|
| 80 |
st.title("π PDF Assistant")
|
| 81 |
|
| 82 |
# ---------------- FUNCTIONS ----------------
|
| 83 |
-
def clear_chat_history():
|
| 84 |
-
st.session_state.chat = []
|
| 85 |
-
|
| 86 |
-
|
| 87 |
def clear_memory():
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
else:
|
| 94 |
-
st.error(res.json().get("detail", "Failed to clear memory"))
|
| 95 |
|
| 96 |
|
| 97 |
# ---------------- SIDEBAR ----------------
|
| 98 |
with st.sidebar:
|
| 99 |
-
st.
|
| 100 |
-
st.button("ποΈ Clear Chat History", on_click=clear_chat_history, use_container_width=True)
|
| 101 |
-
st.button("π₯ Clear PDF Memory", on_click=clear_memory, use_container_width=True)
|
| 102 |
-
|
| 103 |
-
st.markdown("---")
|
| 104 |
-
if st.session_state.uploaded_file_name:
|
| 105 |
-
st.success(f"β
Active PDF:\n`{st.session_state.uploaded_file_name}`")
|
| 106 |
-
else:
|
| 107 |
-
st.warning("β¬οΈ Upload a PDF to start chatting!")
|
| 108 |
|
| 109 |
-
# ---------------- UPLOAD
|
| 110 |
uploaded = st.file_uploader(
|
| 111 |
-
"Upload
|
| 112 |
type=["pdf"],
|
| 113 |
key=st.session_state.uploader_key
|
| 114 |
)
|
| 115 |
|
| 116 |
-
if uploaded and uploaded.name != st.session_state.
|
| 117 |
-
st.
|
| 118 |
-
st.session_state.chat = []
|
| 119 |
-
|
| 120 |
-
with st.spinner(f"Processing '{uploaded.name}'..."):
|
| 121 |
files = {"file": (uploaded.name, uploaded.getvalue(), "application/pdf")}
|
| 122 |
-
res = requests.post(f"{BACKEND_URL}/upload", files=files)
|
| 123 |
|
| 124 |
if res.status_code == 200:
|
| 125 |
-
|
| 126 |
-
st.success(
|
| 127 |
-
st.session_state.uploaded_file_name = uploaded.name
|
| 128 |
else:
|
| 129 |
-
st.error(res.
|
| 130 |
|
| 131 |
st.rerun()
|
| 132 |
|
| 133 |
-
# ---------------- CHAT
|
| 134 |
-
disabled_input = st.session_state.uploaded_file_name is None
|
| 135 |
question = st.text_input(
|
| 136 |
-
"Ask a question
|
| 137 |
-
disabled=
|
| 138 |
)
|
| 139 |
|
| 140 |
-
if st.button("Send"
|
| 141 |
st.session_state.chat.append(("user", question))
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
else:
|
| 153 |
-
st.session_state.chat.append(("bot", "β Error generating answer"))
|
| 154 |
|
| 155 |
st.rerun()
|
| 156 |
|
| 157 |
# ---------------- CHAT HISTORY ----------------
|
| 158 |
-
st.markdown("## Chat History")
|
| 159 |
for role, msg in st.session_state.chat:
|
| 160 |
if role == "user":
|
| 161 |
st.markdown(f"<div class='chat-user'>{msg}</div>", unsafe_allow_html=True)
|
| 162 |
else:
|
| 163 |
st.markdown(f"<div class='chat-bot'>{msg}</div>", unsafe_allow_html=True)
|
| 164 |
-
|
| 165 |
-
# ---------------- FOOTER ----------------
|
| 166 |
-
st.markdown("""
|
| 167 |
-
<div class="footer">
|
| 168 |
-
Created by <a href="https://www.linkedin.com/in/abhishek-iitr/" target="_blank">
|
| 169 |
-
Abhishek Saxena</a>
|
| 170 |
-
</div>
|
| 171 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
+
# β
HF-safe same-origin routing
|
| 5 |
BACKEND_URL = ""
|
| 6 |
|
| 7 |
st.set_page_config(page_title="PDF Assistant", page_icon="π", layout="wide")
|
| 8 |
|
| 9 |
+
# ---------------- CSS ----------------
|
| 10 |
st.markdown("""
|
| 11 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
.chat-user {
|
| 13 |
background: #2d3748;
|
| 14 |
padding: 12px;
|
| 15 |
+
border-radius: 10px;
|
|
|
|
|
|
|
| 16 |
text-align: right;
|
|
|
|
| 17 |
}
|
| 18 |
.chat-bot {
|
| 19 |
+
background: #1e3a8a;
|
| 20 |
padding: 12px;
|
| 21 |
+
border-radius: 10px;
|
| 22 |
+
color: white;
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
|
|
|
| 24 |
.sources {
|
| 25 |
font-size: 0.8em;
|
| 26 |
opacity: 0.7;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
</style>
|
| 29 |
""", unsafe_allow_html=True)
|
| 30 |
|
| 31 |
+
# ---------------- STATE ----------------
|
| 32 |
if "chat" not in st.session_state:
|
| 33 |
st.session_state.chat = []
|
| 34 |
+
if "pdf" not in st.session_state:
|
| 35 |
+
st.session_state.pdf = None
|
|
|
|
|
|
|
| 36 |
if "uploader_key" not in st.session_state:
|
| 37 |
st.session_state.uploader_key = 0
|
| 38 |
|
| 39 |
st.title("π PDF Assistant")
|
| 40 |
|
| 41 |
# ---------------- FUNCTIONS ----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def clear_memory():
|
| 43 |
+
requests.post(f"{BACKEND_URL}/api/clear")
|
| 44 |
+
st.session_state.chat = []
|
| 45 |
+
st.session_state.pdf = None
|
| 46 |
+
st.session_state.uploader_key += 1
|
| 47 |
+
st.success("Memory cleared")
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
# ---------------- SIDEBAR ----------------
|
| 51 |
with st.sidebar:
|
| 52 |
+
st.button("π₯ Clear PDF Memory", on_click=clear_memory)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# ---------------- UPLOAD ----------------
|
| 55 |
uploaded = st.file_uploader(
|
| 56 |
+
"Upload PDF",
|
| 57 |
type=["pdf"],
|
| 58 |
key=st.session_state.uploader_key
|
| 59 |
)
|
| 60 |
|
| 61 |
+
if uploaded and uploaded.name != st.session_state.pdf:
|
| 62 |
+
with st.spinner("Processing PDF..."):
|
|
|
|
|
|
|
|
|
|
| 63 |
files = {"file": (uploaded.name, uploaded.getvalue(), "application/pdf")}
|
| 64 |
+
res = requests.post(f"{BACKEND_URL}/api/upload", files=files)
|
| 65 |
|
| 66 |
if res.status_code == 200:
|
| 67 |
+
st.session_state.pdf = uploaded.name
|
| 68 |
+
st.success("PDF processed")
|
|
|
|
| 69 |
else:
|
| 70 |
+
st.error(res.text)
|
| 71 |
|
| 72 |
st.rerun()
|
| 73 |
|
| 74 |
+
# ---------------- CHAT ----------------
|
|
|
|
| 75 |
question = st.text_input(
|
| 76 |
+
"Ask a question",
|
| 77 |
+
disabled=st.session_state.pdf is None
|
| 78 |
)
|
| 79 |
|
| 80 |
+
if st.button("Send") and question:
|
| 81 |
st.session_state.chat.append(("user", question))
|
| 82 |
|
| 83 |
+
res = requests.post(
|
| 84 |
+
f"{BACKEND_URL}/api/ask",
|
| 85 |
+
json={"question": question}
|
| 86 |
+
)
|
| 87 |
|
| 88 |
+
if res.status_code == 200:
|
| 89 |
+
data = res.json()
|
| 90 |
+
msg = f"{data['answer']}<div class='sources'>Chunks used: {data['sources']}</div>"
|
| 91 |
+
st.session_state.chat.append(("bot", msg))
|
| 92 |
+
else:
|
| 93 |
+
st.session_state.chat.append(("bot", "Error"))
|
|
|
|
|
|
|
| 94 |
|
| 95 |
st.rerun()
|
| 96 |
|
| 97 |
# ---------------- CHAT HISTORY ----------------
|
|
|
|
| 98 |
for role, msg in st.session_state.chat:
|
| 99 |
if role == "user":
|
| 100 |
st.markdown(f"<div class='chat-user'>{msg}</div>", unsafe_allow_html=True)
|
| 101 |
else:
|
| 102 |
st.markdown(f"<div class='chat-bot'>{msg}</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|