-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
165 lines (129 loc) · 5.08 KB
/
app.py
File metadata and controls
165 lines (129 loc) · 5.08 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# OpenCortex
# app.py (image scaling and context constraint)
# Importing Libraries
import os
import warnings
import streamlit as st
import src.core as core
from src.database import MongoManager
from utils.logger import setup_logger
# 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 "model_standard" not in st.session_state:
st.session_state.model_standard = core.PARAMS["llm"]["model_standard"]
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
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"):
for file in uploaded_files:
core.process_uploaded_files([file], st.session_state.username)
st.success("Synced!")
# Clear button
if st.button("Clear Synced Documents"):
core.clear_user_documents(st.session_state.username)
st.success("Cleared all synced documents!")
st.divider()
st.subheader("Model Configuration")
chat_model_options = ["llama3.2:1b", "llama3.2:latest", "llama3.2-vision:latest"]
st.session_state.model_standard = st.selectbox(
"Chat Model",
options=chat_model_options,
index=chat_model_options.index(st.session_state.model_standard) if st.session_state.model_standard in chat_model_options else 0
)
vision_model_options = ["moondream", "llama3.2-vision:latest"]
current_vision = core.PARAMS["llm"]["vision_model"]
selected_vision = st.selectbox(
"Vision Model",
options=vision_model_options,
index=vision_model_options.index(current_vision) if current_vision in vision_model_options else 0
)
core.PARAMS["llm"]["vision_model"] = selected_vision
# 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(
st.session_state.model_standard, 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}
)