Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +32 -0
src/streamlit_app.py
CHANGED
|
@@ -3,6 +3,12 @@ from chat_langraph import system, workflow, HumanMessage, AIMessage, get_all_cha
|
|
| 3 |
import uuid
|
| 4 |
import re
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
st.title("College chatbot")
|
| 7 |
st.set_page_config(layout='wide')
|
| 8 |
|
|
@@ -68,6 +74,17 @@ def loadchats():
|
|
| 68 |
return messages
|
| 69 |
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
load_session_state()
|
| 72 |
render_sidebar()
|
| 73 |
|
|
@@ -88,7 +105,22 @@ if "current_chat_id" in st.session_state:
|
|
| 88 |
):
|
| 89 |
if isinstance(messages, AIMessage) and messages.content.strip():
|
| 90 |
full_response += messages.content
|
|
|
|
| 91 |
if isinstance(messages, ToolMessage) and messages.content.strip():
|
| 92 |
st.info(f"Using appropriate tool")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
response_placeholder.markdown(full_response)
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import uuid
|
| 4 |
import re
|
| 5 |
import os
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
# Ensure temporary directory for files in Hugging Face Spaces
|
| 9 |
+
TEMP_DIR = "/tmp"
|
| 10 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
st.title("College chatbot")
|
| 13 |
st.set_page_config(layout='wide')
|
| 14 |
|
|
|
|
| 74 |
return messages
|
| 75 |
|
| 76 |
|
| 77 |
+
def create_download_link(file_path: str, label: str = "Download file") -> str:
|
| 78 |
+
try:
|
| 79 |
+
with open(file_path, "rb") as f:
|
| 80 |
+
data = f.read()
|
| 81 |
+
b64 = base64.b64encode(data).decode()
|
| 82 |
+
href = f'<a href="data:file/octet-stream;base64,{b64}" download="{os.path.basename(file_path)}">{label}</a>'
|
| 83 |
+
return href
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return f"Error creating download link: {e}"
|
| 86 |
+
|
| 87 |
+
|
| 88 |
load_session_state()
|
| 89 |
render_sidebar()
|
| 90 |
|
|
|
|
| 105 |
):
|
| 106 |
if isinstance(messages, AIMessage) and messages.content.strip():
|
| 107 |
full_response += messages.content
|
| 108 |
+
|
| 109 |
if isinstance(messages, ToolMessage) and messages.content.strip():
|
| 110 |
st.info(f"Using appropriate tool")
|
| 111 |
+
|
| 112 |
+
# If tool output contains file creation info → show download link
|
| 113 |
+
if "Content saved to" in messages.content:
|
| 114 |
+
file_name = messages.content.replace("Content saved to ", "").strip()
|
| 115 |
+
file_path = os.path.join(TEMP_DIR, file_name)
|
| 116 |
+
if os.path.exists(file_path):
|
| 117 |
+
st.markdown(
|
| 118 |
+
create_download_link(file_path, "📥 Download File"),
|
| 119 |
+
unsafe_allow_html=True
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
response_placeholder.markdown(full_response)
|
| 123 |
|
| 124 |
+
st.experimental_rerun()
|
| 125 |
+
|
| 126 |
+
|