-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
129 lines (99 loc) · 3.89 KB
/
app.py
File metadata and controls
129 lines (99 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# app.py
import streamlit as st
import src.core as core
from src.database import MongoManager
from utils.logger import setup_logger
import warnings
import os
# Ignore Transformer Warnings
os.environ["TRANSFORMERS_VERBOSITY"] = "error"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# Catch any leftover Python-level warnings
warnings.filterwarnings("ignore")
logger = setup_logger("app_ui")
st.set_page_config(page_title="OpenCortex", layout="wide")
@st.cache_resource
def init_db():
return MongoManager()
db = init_db()
# Initialize session state for user
if "logged_in" not in st.session_state:
st.session_state.logged_in = False
st.session_state.username = ""
if not st.session_state.logged_in:
# Login / Signup UI
st.title("Welcome to OpenCortex")
tab1, tab2 = st.tabs(["Login", "Sign Up"])
# Login Tab
with tab1:
with st.form("login"):
u = st.text_input("Username")
p = st.text_input("Password", type="password")
# Login button
if st.form_submit_button("Login"):
success, msg = db.verify_user(u, p)
# Login success
if success:
st.session_state.logged_in = True
st.session_state.username = u
st.rerun()
# Login failed
else:
st.error(msg)
# Signup Tab
with tab2:
with st.form("signup"):
new_u = st.text_input("New Username")
new_p = st.text_input("New Password", type="password")
# Signup button
if st.form_submit_button("Register"):
success, msg = db.create_user(new_u, new_p)
st.success(msg) if success else st.error(msg)
# User is logged in
else:
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = db.get_history(st.session_state.username)
# Main UI
st.title("OpenCortex")
st.caption(f"Authenticated as: {st.session_state.username}")
# Sidebar
with st.sidebar:
# Logout button
if st.button("Logout"):
st.session_state.logged_in = False
st.session_state.messages = []
st.rerun()
# File uploader
st.divider()
uploaded_files = st.file_uploader(
"Upload Documents & Images",
type=["pdf", "txt", "png", "jpg", "jpeg"],
accept_multiple_files=True
)
# Sync button
if uploaded_files and st.button("Sync"):
core.process_uploaded_files(uploaded_files, st.session_state.username)
st.success("Synced!")
# Chat Interface
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# User input
if prompt := st.chat_input("Ask OpenCortex..."):
st.session_state.messages.append({"role": "user", "content": prompt})
# Save user message
db.save_message(st.session_state.username, "user", prompt)
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Display assistant message
with st.chat_message("assistant"):
# Retrieve context
context = core.retrieve_context(prompt, st.session_state.username)
logger.info(f"RETRIEVED CONTEXT: {context}")
# Stream response
full_response = st.write_stream(core.opencortex_response_stream("llama3.2", prompt, context))
# Save assistant message
db.save_message(st.session_state.username, "assistant", full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})