-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
287 lines (243 loc) · 10.9 KB
/
main.py
File metadata and controls
287 lines (243 loc) · 10.9 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import os
import sqlite3
import redis
from pathlib import Path
from PIL import Image
import streamlit as st
from streamlit_mic_recorder import mic_recorder
from chat_api_handler import ChatAPIHandler
from utils import (
get_timestamp, load_config, get_avatar,
list_openai_models, list_ollama_models, command
)
from utils.audio_handler import transcribe_audio
from utils.pdf_handler import add_documents_to_db
from utils.html_templates import css
from database_operations import (
save_text_message, save_image_message, save_audio_message,
load_messages, get_all_chat_history_ids,
delete_chat_history, load_last_k_text_messages_ollama
)
# ==================================================================
# Project : Neura-Nix - Multimodal AI Assistant {Ollama MultiRag}
# Author : UjjwalS (https://www.ujjwalsaini.dev)
# License : Apache-2.0
# Copyright : © 2025 UjjwalS. All rights reserved.
# ==================================================================
config = load_config()
# For Redis LocalHost
# redis_client = redis.Redis(
# host=os.getenv("REDIS_HOST", "localhost"),
# port=int(os.getenv("REDIS_PORT", 6379)),
# db=0,
# decode_responses=True
# )
# For Redis Cloud
redis_client = redis.Redis(
host=os.getenv("REDIS_HOST", "redis-12345.c15.us-east-1-2.ec2.cloud.redislabs.com"),
port=int(os.getenv("REDIS_PORT", 12345)),
password=os.getenv("REDIS_PASSWORD"),
ssl=True,
decode_responses=True
)
logo_long_image = Image.open("assets/logo_long_image.png")
logo_short_image = Image.open("assets/logo_short_image.png")
# ---------------------------
# Sidebar Footer
# ---------------------------
def sidebar_footer():
with st.sidebar:
st.markdown(
"""
<div style="padding: 5px; text-align: center; font-size: 0.9em; color: #999;">
Powered by Ollama & OpenAI
</div>
<div style="padding: 2px; text-align: center; font-size: 1em; color: #fff;">
Built with dedication by <strong>UjjwalS</strong>
</div>
""",
unsafe_allow_html=True
)
# ---------------------------
# Session Helpers
# ---------------------------
def toggle_pdf_chat():
st.session_state.pdf_chat = True
clear_cache()
def detoggle_pdf_chat():
st.session_state.pdf_chat = False
def get_session_key():
if st.session_state.session_key == "new_session":
st.session_state.new_session_key = get_timestamp()
return st.session_state.new_session_key
return st.session_state.session_key
def delete_chat_session_history():
delete_chat_history(st.session_state.session_key)
st.session_state.session_index_tracker = "new_session"
# Author: UjjwalS (https://www.ujjwalsaini.dev)
def clear_cache():
"""Clear Redis cache."""
redis_client.flushdb()
def list_model_options():
endpoint = st.session_state.endpoint_to_use
cache_key = f"models:{endpoint}"
if redis_client.exists(cache_key):
return redis_client.lrange(cache_key, 0, -1)
if endpoint == "ollama":
ollama_options = list_ollama_models()
if not ollama_options:
st.warning("No Ollama models found. Visit https://ollama.com/library and pull one with /pull <model_name>")
if ollama_options:
redis_client.rpush(cache_key, *ollama_options)
return ollama_options
elif endpoint == "openai":
openai_options = list_openai_models()
if openai_options:
redis_client.rpush(cache_key, *openai_options)
return openai_options
def update_model_options():
st.session_state.model_options = list_model_options()
# ---------------------------
# Main Application
# ---------------------------
def main():
st.set_page_config(
page_title="Neura-Nix: Multimodal Assistant",
page_icon="🤖",
layout="wide",
initial_sidebar_state="expanded"
)
# Author: UjjwalS (https://www.ujjwalsaini.dev)
st.write(css, unsafe_allow_html=True)
st.title("Neura-Nix: Multimodal Assistant")
st.markdown(
"""
<div style="font-size: 1.05em; line-height: 1.6; padding: 8px;
background-color: #2b313e; border-radius: 8px; color: #e6e6e6;">
Neura-Nix is a **context-aware multimodal assistant** that enables seamless interaction
across text, images, PDFs, and audio.
Designed for clarity, efficiency, and adaptability — it transforms diverse inputs
into coherent, actionable insights.
</div>
""",
unsafe_allow_html=True
)
# Feature highlights
st.markdown("### Capabilities")
text_col, image_col, audio_col = st.columns(3)
text_col.success("📑 Query and summarize PDFs")
image_col.success("🖼️ Analyze and interpret images")
audio_col.success("🎙️ Converse through audio inputs")
# Session initialization
if "db_conn" not in st.session_state:
st.session_state.session_key = "new_session"
st.session_state.new_session_key = None
st.session_state.session_index_tracker = "new_session"
st.session_state.db_conn = sqlite3.connect(config["chat_sessions_database_path"], check_same_thread=False)
st.session_state.audio_uploader_key = 0
st.session_state.pdf_uploader_key = 1
st.session_state.endpoint_to_use = "ollama"
st.session_state.model_options = list_model_options()
st.session_state.model_tracker = None
if st.session_state.session_key == "new_session" and st.session_state.new_session_key is not None:
st.session_state.session_index_tracker = st.session_state.new_session_key
st.session_state.new_session_key = None
# Sidebar: Chat sessions
st.sidebar.title("Chat History")
chat_sessions = ["new_session"] + get_all_chat_history_ids()
try:
index = chat_sessions.index(st.session_state.session_index_tracker)
except ValueError:
st.session_state.session_index_tracker = "new_session"
index = 0
clear_cache()
st.sidebar.selectbox("Select a chat session", chat_sessions, key="session_key", index=index)
st.sidebar.button("Delete Chat Session", on_click=delete_chat_session_history)
# Sidebar: API settings
api_col, model_col = st.sidebar.columns(2)
api_col.selectbox("Provider", ["ollama", "openai"], key="endpoint_to_use", on_change=update_model_options)
model_col.selectbox("Model", st.session_state.model_options, key="model_to_use")
pdf_toggle_col, voice_rec_col = st.sidebar.columns(2)
pdf_toggle_col.toggle("Enable PDF Chat", key="pdf_chat", value=False, on_change=clear_cache)
with voice_rec_col:
voice_recording = mic_recorder(start_prompt="🎤 Start", stop_prompt="⏹ Stop", just_once=True)
# File uploaders
st.sidebar.title("Input Options")
with st.sidebar.expander("Upload Files", expanded=False):
uploaded_pdf = st.file_uploader("PDF", accept_multiple_files=True, key=st.session_state.pdf_uploader_key, type=["pdf"], on_change=toggle_pdf_chat)
uploaded_image = st.file_uploader("Image", type=["jpg", "jpeg", "png"], on_change=detoggle_pdf_chat)
uploaded_audio = st.file_uploader("Audio", type=["wav", "mp3", "ogg"], key=st.session_state.audio_uploader_key)
sidebar_footer()
# Chat container
chat_container = st.container()
user_input = st.chat_input("Enter your query here...")
# ---------------------------
# Process Uploaded Files
# ---------------------------
if uploaded_pdf:
with st.spinner("Processing PDF..."):
add_documents_to_db(uploaded_pdf)
st.session_state.pdf_uploader_key += 2
if voice_recording:
transcribed_audio = transcribe_audio(voice_recording["bytes"])
llm_answer = ChatAPIHandler.chat(
user_input=transcribed_audio,
chat_history=load_last_k_text_messages_ollama(get_session_key(), config["chat_config"]["chat_memory_length"])
)
save_audio_message(get_session_key(), "user", voice_recording["bytes"])
save_text_message(get_session_key(), "assistant", llm_answer)
if user_input:
if user_input.startswith("/"):
response = command(user_input)
save_text_message(get_session_key(), "user", user_input)
save_text_message(get_session_key(), "assistant", response)
user_input = None
elif uploaded_image:
with st.spinner("Processing image..."):
llm_answer = ChatAPIHandler.chat(
user_input=user_input,
chat_history=[],
image=uploaded_image.getvalue()
)
save_text_message(get_session_key(), "user", user_input)
save_image_message(get_session_key(), "user", uploaded_image.getvalue())
save_text_message(get_session_key(), "assistant", llm_answer)
user_input = None
elif uploaded_audio:
transcribed_audio = transcribe_audio(uploaded_audio.getvalue())
llm_answer = ChatAPIHandler.chat(
user_input=user_input + "\n" + transcribed_audio,
chat_history=[]
)
save_text_message(get_session_key(), "user", user_input)
save_audio_message(get_session_key(), "user", uploaded_audio.getvalue())
save_text_message(get_session_key(), "assistant", llm_answer)
st.session_state.audio_uploader_key += 2
user_input = None
elif user_input:
llm_answer = ChatAPIHandler.chat(
user_input=user_input,
chat_history=load_last_k_text_messages_ollama(get_session_key(), config["chat_config"]["chat_memory_length"])
)
save_text_message(get_session_key(), "user", user_input)
save_text_message(get_session_key(), "assistant", llm_answer)
user_input = None
# ---------------------------
# Display Chat History
# ---------------------------
if (st.session_state.session_key != "new_session") != (st.session_state.new_session_key is not None):
with chat_container:
chat_history_messages = load_messages(get_session_key())
for message in chat_history_messages:
with st.chat_message(name=message["sender_type"], avatar=get_avatar(message["sender_type"])):
if message["message_type"] == "text":
st.write(message["content"])
elif message["message_type"] == "image":
st.image(message["content"])
elif message["message_type"] == "audio":
st.audio(message["content"], format="audio/wav")
if st.session_state.session_key == "new_session" and st.session_state.new_session_key is not None:
st.rerun()
# Author: UjjwalS (https://www.ujjwalsaini.dev)
if __name__ == "__main__":
main()