-
Notifications
You must be signed in to change notification settings - Fork 0
/
app2.py
884 lines (757 loc) · 40.2 KB
/
app2.py
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
import streamlit as st
from streamlit_option_menu import option_menu
from anthropic import AnthropicVertex
import os
import PyPDF2
from docx import Document
import pyperclip
import json
import time
import sqlite3
from sqlite3 import Connection
from threading import Lock
import queue
from datetime import datetime, timedelta
import hashlib
import atexit
import secrets
from pdf_chat import PDFChatbot
from wikipediaapi import Wikipedia
import numpy as np
import textwrap
import re
# Initialize session state variables
if 'logged_in' not in st.session_state:
st.session_state.logged_in = False
if 'user_id' not in st.session_state:
st.session_state.user_id = None
if 'username' not in st.session_state:
st.session_state.username = None
if 'conversation' not in st.session_state:
st.session_state.conversation = []
if 'document_content' not in st.session_state:
st.session_state.document_content = ""
if 'context' not in st.session_state:
st.session_state.context = "You are a helpful assistant with tool calling capabilities. The user has access to the tool's outputs that you as a model cannot see. This could include text, images and more."
if 'generating' not in st.session_state:
st.session_state.generating = False
# Add the temperature, top_p, and max_tokens session state variables here
if 'temperature' not in st.session_state:
st.session_state.temperature = 0.7 # Default value
if 'top_p' not in st.session_state:
st.session_state.top_p = 0.9 # Default value
if 'max_tokens' not in st.session_state:
st.session_state.max_tokens = 4096 # Default value
# Constants
COMMON_PASSWORD = "claude2023" # Change this to your desired common password
DB_NAME = 'chatbot.db'
POOL_SIZE = 5
st.set_page_config(page_title="VertexClaude Pro", page_icon="blockchain_laboratories_logo.jpg", layout="wide")
# SQLite datetime handling
def adapt_datetime(dt):
return dt.isoformat()
def convert_datetime(s):
return datetime.fromisoformat(s)
sqlite3.register_adapter(datetime, adapt_datetime)
sqlite3.register_converter("datetime", convert_datetime)
# Database connection pool
class ConnectionPool:
def __init__(self, database, max_connections):
self.database = database
self.max_connections = max_connections
self.connections = queue.Queue(maxsize=max_connections)
self.connection_count = 0
self.lock = Lock()
def get_connection(self) -> Connection:
if not self.connections.empty():
return self.connections.get()
with self.lock:
if self.connection_count < self.max_connections:
connection = sqlite3.connect(self.database,
detect_types=sqlite3.PARSE_DECLTYPES,
check_same_thread=False)
connection.execute('PRAGMA journal_mode=WAL')
connection.execute('PRAGMA synchronous=NORMAL')
self.connection_count += 1
return connection
return self.connections.get(block=True, timeout=30)
def return_connection(self, connection: Connection):
self.connections.put(connection)
def close_all(self):
while not self.connections.empty():
connection = self.connections.get()
connection.close()
# Initialize the connection pool
db_pool = ConnectionPool(DB_NAME, POOL_SIZE)
# Database operations
def safe_db_operation(operation):
conn = db_pool.get_connection()
try:
with conn:
operation(conn.cursor())
conn.commit()
except sqlite3.Error as e:
st.error(f"Database error: {e}")
conn.rollback()
finally:
db_pool.return_connection(conn)
def execute_query(query, params=None):
conn = db_pool.get_connection()
try:
with conn:
cursor = conn.cursor()
if params:
cursor.execute(query, params)
else:
cursor.execute(query)
return cursor.fetchall()
finally:
db_pool.return_connection(conn)
def execute_insert(query, params):
conn = db_pool.get_connection()
try:
with conn:
cursor = conn.cursor()
cursor.execute(query, params)
return cursor.lastrowid
finally:
db_pool.return_connection(conn)
# Database initialization
def init_db():
conn = db_pool.get_connection()
try:
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY, username TEXT UNIQUE)''')
c.execute('''CREATE TABLE IF NOT EXISTS conversations
(id INTEGER PRIMARY KEY, user_id INTEGER, conversation TEXT, timestamp TEXT, context TEXT, title TEXT)''')
c.execute('''CREATE TABLE IF NOT EXISTS chat_state
(id INTEGER PRIMARY KEY, user_id INTEGER, conversation TEXT, document_content TEXT, context TEXT)''')
c.execute('''CREATE TABLE IF NOT EXISTS sessions
(id TEXT PRIMARY KEY, user_id INTEGER, expiry TIMESTAMP)''')
conn.commit()
finally:
db_pool.return_connection(conn)
# Call init_db at the start of your application
init_db()
# Load API credentials
def load_credentials():
try:
with open('credentials.json', 'r') as f:
return json.load(f)
except FileNotFoundError:
st.error("Credentials file not found. Please ensure 'credentials.json' exists.")
return None
credentials = load_credentials()
if credentials:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'credentials.json'
LOCATION = credentials.get('location', "europe-west1")
PROJECT_ID = credentials.get('project_id')
MODEL = credentials.get('model', "claude-3-5-sonnet@20240620")
else:
st.error("Failed to load credentials. Chat functionality will be limited.")
# Initialize Anthropic client
@st.cache_resource
def init_client():
try:
return AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)
except Exception as e:
st.sidebar.error(f"Failed to initialize Anthropic client: {e}")
return None
client = init_client()
# User authentication functions
def create_session(user_id):
session_id = secrets.token_urlsafe(32)
expiry = datetime.now() + timedelta(hours=24) # 24-hour session
execute_insert("INSERT INTO sessions (id, user_id, expiry) VALUES (?, ?, ?)",
(session_id, user_id, expiry))
return session_id
def validate_session(session_id):
result = execute_query("SELECT user_id, expiry FROM sessions WHERE id = ?", (session_id,))
if result:
user_id, expiry = result[0]
if datetime.now() < expiry:
return user_id
return None
def login_user(username, password):
if password == COMMON_PASSWORD:
result = execute_query("SELECT id FROM users WHERE username = ?", (username,))
if result:
user_id = result[0][0]
else:
user_id = execute_insert("INSERT INTO users (username) VALUES (?)", (username,))
session_id = create_session(user_id)
st.session_state.session_id = session_id
st.session_state.logged_in = True
st.session_state.user_id = user_id
st.session_state.username = username
# Reset chat state for new session
st.session_state.conversation = []
st.session_state.document_content = ""
st.session_state.context = "You are a helpful assistant with tool calling capabilities. The user has access to the tool's outputs that you as a model cannot see. This could include text, images and more."
save_chat_state()
return True
else:
st.error("Incorrect password")
return False
def logout_user():
if 'session_id' in st.session_state:
safe_db_operation(lambda cur: cur.execute("DELETE FROM sessions WHERE id = ?", (st.session_state.session_id,)))
# Save current chat state before logging out
save_chat_state()
save_conversation()
# Clear session state
for key in list(st.session_state.keys()):
del st.session_state[key]
st.session_state.logged_in = False
# Chat state management functions
def save_chat_state():
safe_db_operation(lambda cur: cur.execute(
"DELETE FROM chat_state WHERE user_id = ?", (st.session_state.user_id,)
))
safe_db_operation(lambda cur: cur.execute(
"INSERT INTO chat_state (user_id, conversation, document_content, context) VALUES (?, ?, ?, ?)",
(st.session_state.user_id, json.dumps(st.session_state.conversation),
st.session_state.document_content, st.session_state.context)
))
def load_chat_state():
result = execute_query("SELECT * FROM chat_state WHERE user_id = ?", (st.session_state.user_id,))
if result:
st.session_state.conversation = json.loads(result[0][2])
st.session_state.document_content = result[0][3]
st.session_state.context = result[0][4]
else:
st.session_state.conversation = []
st.session_state.document_content = ""
st.session_state.context = "You are a helpful assistant with tool calling capabilities. The user has access to the tool's outputs that you as a model cannot see. This could include text, images and more."
def generate_conversation_title(conversation):
sample = json.loads(conversation)[:3]
sample_text = "\n".join([f"{msg['role']}: {msg['content'][:50]}..." for msg in sample])
prompt = f"Generate a short, 1-2 word title for this conversation:\n\n{sample_text}\n\nTitle:"
try:
response = client.messages.create(
model=MODEL,
max_tokens=10,
messages=[{"role": "user", "content": prompt}],
system="You are a helpful assistant that generates short, concise titles."
)
title = response.content[0].text.strip()
return title
except Exception as e:
st.error(f"Error generating title: {e}")
return "Untitled Chat"
def save_conversation():
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
conversation_json = json.dumps(st.session_state.conversation)
title = generate_conversation_title(conversation_json)
execute_insert(
"INSERT INTO conversations (user_id, conversation, timestamp, context, title) VALUES (?, ?, ?, ?, ?)",
(st.session_state.user_id, conversation_json, timestamp, st.session_state.context, title)
)
# Main chat function
def chat(user_input):
if not user_input or not client:
return
st.session_state.generating = True
# Add the user's message to the conversation
st.session_state.conversation.append({"role": "user", "content": user_input})
# Display the user's message
with st.chat_message("user"):
st.markdown(user_input)
display_message_stats(user_input)
with st.chat_message("assistant"):
response_container = st.empty()
full_response = ""
try:
combined_context = create_combined_context()
with st.spinner("Generating response..."):
# Send the entire conversation history
messages_to_send = truncate_conversation(st.session_state.conversation)
for event in client.messages.create(
max_tokens=st.session_state.max_tokens,
temperature=st.session_state.temperature,
top_p=st.session_state.top_p,
system=combined_context,
messages=messages_to_send,
model=MODEL,
stream=True,
):
if st.session_state.generating:
if event.type == "content_block_delta" and hasattr(event.delta, "text"):
full_response += event.delta.text
response_container.markdown(f"{full_response}▌")
time.sleep(0.01)
else:
break
response_container.markdown(full_response)
display_message_stats(full_response)
# Add the assistant's response to the conversation history
st.session_state.conversation.append({"role": "assistant", "content": full_response})
# Save the updated chat state
save_chat_state()
except Exception as e:
response_container.error(f"An error occurred: {e}")
st.error(f"Error during API call: {e}")
finally:
st.session_state.generating = False
# Helper functions
def create_combined_context():
combined_context = f"{st.session_state.context}\n\n"
# Include a summary of the last 10 chats
conversation_summary = ""
chat_count = 0
total_tokens = 0
max_tokens = 50000 # Increased token limit
for msg in reversed(st.session_state.conversation):
if chat_count >= 10:
break
role = msg['role']
content = msg['content']
# Simple compression: truncate long messages and remove extra whitespace
if len(content) > 500:
content = content[:497] + "..."
content = ' '.join(content.split()) # Remove extra whitespace
summary = f"{role.capitalize()}: {content}\n\n"
summary_tokens = len(summary.encode('utf-8'))
if total_tokens + summary_tokens > max_tokens:
break
conversation_summary = summary + conversation_summary
total_tokens += summary_tokens
chat_count += 1
combined_context += f"Recent conversation history:\n{conversation_summary}"
if st.session_state.document_content:
# Compress document content if needed
doc_content = st.session_state.document_content
if len(doc_content) > 1000:
doc_content = doc_content[:997] + "..."
doc_content = ' '.join(doc_content.split()) # Remove extra whitespace
combined_context += f"\n\nDocument Content:\n{doc_content}"
# Ensure we don't exceed the max token limit
if len(combined_context.encode('utf-8')) > max_tokens:
combined_context = combined_context[:max_tokens].rsplit(' ', 1)[0] + "..."
return combined_context
def display_message_stats(text):
if isinstance(text, list):
# If text is a list, join it into a single string
text = ' '.join(map(str, text))
word_count = len(text.split())
token_count = len(text.encode('utf-8'))
st.caption(f"Word count: {word_count} | Token count: {token_count}")
def parse_document(file):
try:
if file.type == "application/pdf":
return parse_pdf(file)
elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
return parse_docx(file)
elif file.type == "text/plain":
return file.getvalue().decode("utf-8")
else:
return "Unsupported file type"
except Exception as e:
return f"Error parsing document: {str(e)}"
def parse_pdf(file):
reader = PyPDF2.PdfReader(file)
return " ".join(page.extract_text() for page in reader.pages)
def parse_docx(file):
doc = Document(file)
return "\n".join(para.text for para in doc.paragraphs)
def truncate_conversation(conversation, max_messages=50):
if len(conversation) > max_messages:
truncated = conversation[-max_messages:]
truncated[0]["content"] = f"[Earlier conversation truncated] ... {truncated[0]['content']}"
return truncated
return conversation
# Function to close all database connections
def close_db_connections():
db_pool.close_all()
# Add this function to perform Wikipedia search and RAG
def wikipedia_search(query, client):
wiki = Wikipedia('RAGBot/0.0', 'en')
page = wiki.page(query)
if not page.exists():
return "No Wikipedia page found for this query."
# Get the full text and split it into paragraphs
full_text = page.text
paragraphs = full_text.split('\n\n')
# Function to score paragraph relevance
def score_paragraph(paragraph, query_terms):
paragraph = paragraph.lower()
return sum(paragraph.count(term.lower()) for term in query_terms)
# Preprocess query
query_terms = re.findall(r'\w+', query.lower())
# Score and sort paragraphs
scored_paragraphs = [(score_paragraph(p, query_terms), p) for p in paragraphs]
scored_paragraphs.sort(reverse=True)
# Get top 3 most relevant paragraphs
top_paragraphs = [p for _, p in scored_paragraphs[:3]]
context = "\n\n".join(top_paragraphs)
return context
# Ensure chat state is loaded when the user logs in
if st.session_state.logged_in:
load_chat_state()
# Initialize PDFChatbot and chat mode
if 'pdf_chatbot' not in st.session_state:
st.session_state.pdf_chatbot = None
if 'chat_mode' not in st.session_state:
st.session_state.chat_mode = "regular"
# Main execution
if __name__ == "__main__":
# Check if this is a new session
if 'db_initialized' not in st.session_state:
st.session_state.db_initialized = True
# Register the function to be called when the Streamlit script stops
atexit.register(close_db_connections)
if not st.session_state.logged_in:
st.title("🤖 VertexClade Pro - Login")
with st.form("login_form"):
username = st.text_input("Username")
password = st.text_input("Password", type="password")
submit_button = st.form_submit_button("Login")
if submit_button:
if login_user(username, password):
st.success("Logged in successfully!")
st.rerun()
else:
# Header
st.title("🤖 VertexClade Pro")
# Sidebar
with st.sidebar:
st.sidebar.title("Chatbot Configuration")
# Create two columns for the login info and logout button
col1, col2 = st.sidebar.columns([2, 1])
#Display the logged-in username in the first column
col1.write(f"Logged in as: {st.session_state.username}")
# Place the logout button in the second column
if col2.button("Log out", help="Click here to log out"):
logout_user()
st.rerun()
# Navigation dropdown
selected = st.selectbox(
"Select a tool:",
options=[
"Chat", "Text Summarization", "Content Generation",
"Data Extraction", "Q&A", "Translation",
"Text Analysis", "Code Assistant"
],
format_func=lambda x: {
"Chat": "💬 Chat",
"Text Summarization": "📝 Text Summarization",
"Content Generation": "✍️ Content Generation",
"Data Extraction": "🔍 Data Extraction",
"Q&A": "❓ Q&A",
"Translation": "🌐 Translation",
"Text Analysis": "📊 Text Analysis",
"Code Assistant": "💻 Code Assistant"
}[x]
)
# Conversation Management section
st.sidebar.subheader("💬 Conversation Management")
col1, col2 = st.sidebar.columns(2)
if col1.button("Clear Chat", key="clear_chat", help="Click here to clear the current chat history"):
st.session_state.conversation = []
st.session_state.document_content = ""
save_chat_state()
st.success("Chat cleared!")
st.rerun()
if col2.button("New Chat", key="new_chat", help="Click here to start a new chat session"):
if st.session_state.conversation:
save_conversation()
st.session_state.conversation = []
st.session_state.document_content = ""
st.session_state.context = "You are a helpful assistant with tool calling capabilities. The user has access to the tool's outputs that you as a model cannot see. This could include text, images and more."
save_chat_state()
st.success("New chat started!")
st.rerun()
# Load chat state for the logged-in user
if "conversation" not in st.session_state:
load_chat_state()
with st.expander("⚙️ Model Settings", expanded=False):
st.session_state.temperature = st.slider("Temperature", 0.0, 1.0, st.session_state.temperature, 0.01,
help="Controls the randomness of the output. Lower values make the output more deterministic.")
st.session_state.top_p = st.slider("Top-p (Nucleus Sampling)", 0.0, 1.0, st.session_state.top_p, 0.01,
help="Controls the diversity of the output. Lower values make the output more focused.")
st.session_state.max_tokens = st.slider("Max Tokens", 256, 4096, st.session_state.max_tokens, 64,
help="Sets the maximum number of tokens in the output. Higher values allow longer responses.")
with st.expander("🎭 Define Chatbot Role", expanded=False):
new_context = st.text_area("Enter the role and purpose:", st.session_state.context, height=80)
if new_context != st.session_state.context:
st.session_state.context = new_context
save_chat_state()
st.success("Context updated successfully!")
with st.expander("🧠 Current Context", expanded=False):
current_context = st.text_area("Current context:", st.session_state.context, height=60)
if current_context != st.session_state.context:
st.session_state.context = current_context
save_chat_state()
st.success("Context updated successfully!")
if st.button("View Full Context"):
full_context = create_combined_context()
st.text_area("Full Context (including conversation history):", full_context, height=200)
with st.expander("📜 Conversation History", expanded=False):
conversation_history = execute_query(
"SELECT id, conversation, timestamp, context, title FROM conversations WHERE user_id = ? ORDER BY timestamp DESC",
(st.session_state.user_id,)
)
for idx, (conv_id, conv, timestamp, context, title) in enumerate(conversation_history):
col1, col2, col3 = st.columns([0.6, 0.25, 0.15])
col1.write(f"<small>{title} - {timestamp}</small>", unsafe_allow_html=True)
if col2.button("↻", key=f"load_conv_{idx}", use_container_width=True):
st.session_state.conversation = json.loads(conv)
st.session_state.context = context
save_chat_state()
st.rerun()
if col3.button("x", key=f"delete_conv_{idx}", use_container_width=True):
safe_db_operation(lambda cur: cur.execute("DELETE FROM conversations WHERE id = ?", (conv_id,)))
st.success(f"{title} deleted!")
st.rerun()
if conversation_history:
if st.button("Delete All Saved Conversations"):
safe_db_operation(lambda cur: cur.execute("DELETE FROM conversations WHERE user_id = ?", (st.session_state.user_id,)))
st.success("All saved conversations deleted!")
st.rerun()
with st.expander("📄 Upload Document", expanded=False):
uploaded_file = st.file_uploader("Choose a file", type=["pdf", "docx", "txt"])
if uploaded_file:
with st.spinner("Processing document..."):
if uploaded_file.type == "application/pdf":
st.session_state.pdf_chatbot = PDFChatbot(client, MODEL)
success = st.session_state.pdf_chatbot.process_pdf(uploaded_file)
if success:
st.success("✅ PDF processed successfully!")
st.session_state.document_content = st.session_state.pdf_chatbot.get_pdf_content()
save_chat_state()
else:
st.error("Failed to process PDF.")
else:
st.session_state.document_content = parse_document(uploaded_file)
save_chat_state()
st.success("✅ Document processed successfully!")
if st.session_state.pdf_chatbot or st.session_state.document_content:
st.write("Chat mode:")
chat_mode = st.radio(
"Select chat mode",
("Regular", "PDF"),
key="chat_mode_radio",
index=0 if st.session_state.chat_mode == "regular" else 1,
horizontal=True,
)
st.session_state.chat_mode = chat_mode.lower()
if st.button("❌ Delete Document Content"):
st.session_state.pdf_chatbot = None
st.session_state.document_content = ""
st.session_state.chat_mode = "regular"
save_chat_state()
st.success("Document content deleted!")
st.rerun()
st.sidebar.title("Chat Analytics")
with st.expander("📊 Chat Statistics", expanded=False):
total_messages = len(st.session_state.conversation)
user_messages = sum(1 for msg in st.session_state.conversation if msg['role'] == 'user')
assistant_messages = total_messages - user_messages
st.write(f"Total messages: {total_messages}")
st.write(f"User messages: {user_messages}")
st.write(f"Assistant messages: {assistant_messages}")
with st.expander("🔍 Search Conversation", expanded=False):
search_term = st.text_input("Enter search term:")
if search_term:
search_results = [msg for msg in st.session_state.conversation if search_term.lower() in msg['content'].lower()]
st.write(f"Found {len(search_results)} results:")
for idx, msg in enumerate(search_results):
st.text_area(f"Result {idx + 1}", msg['content'], height=100)
with st.expander("📈 Conversation Analysis", expanded=False):
if st.session_state.conversation:
avg_user_msg_length = sum(len(msg['content']) for msg in st.session_state.conversation if msg['role'] == 'user') / user_messages if user_messages > 0 else 0
avg_assistant_msg_length = sum(len(msg['content']) for msg in st.session_state.conversation if msg['role'] == 'assistant') / assistant_messages if assistant_messages > 0 else 0
st.write(f"Avg. user message length: {avg_user_msg_length:.2f} characters")
st.write(f"Avg. assistant message length: {avg_assistant_msg_length:.2f} characters")
with st.expander("🏷️ Topic Modeling", expanded=False):
st.write("Coming soon: Topic modeling for conversation analysis")
with st.sidebar.expander("🌐 Wikipedia Search", expanded=False):
wiki_query = st.text_input("Enter a Wikipedia search term:")
if st.button("Search Wikipedia"):
if wiki_query:
with st.spinner("Searching Wikipedia..."):
context = wikipedia_search(wiki_query, client)
st.session_state.wiki_context = context
st.success("Wikipedia search completed!")
else:
st.warning("Please enter a search term.")
if 'wiki_context' in st.session_state:
st.text_area("Wikipedia Context:", st.session_state.wiki_context, height=200)
st.subheader("Popular Questions")
questions = [
"How is blockchain technology used in AI?",
"What are the benefits of Web3 SaaS?",
"How does AI impact blockchain development?",
"What are the key features of decentralized applications?",
"How can blockchain improve AI data security?"
]
selected_question = st.selectbox("Select a question:", questions)
if st.button("Ask Selected Question"):
with st.spinner("Generating response..."):
response = chat(f"Based on the Wikipedia context: {st.session_state.wiki_context}\n\nQuestion: {selected_question}")
st.write("Answer:")
st.write(response)
# Main content area
if selected == "Chat":
st.subheader("💬 Chat")
st.markdown("<small>Welcome to your AI assistant! How can I help you today?</small>", unsafe_allow_html=True)
if st.session_state.chat_mode == "pdf":
st.info("You are in PDF chat mode. Your questions will be answered based on the uploaded PDF content.")
for idx, message in enumerate(st.session_state.conversation):
with st.chat_message(message["role"]):
st.markdown(f"<small>{message['content']}</small>", unsafe_allow_html=True)
display_message_stats(message["content"])
col1, col2 = st.columns([0.13, 0.9])
if col1.button("📋 Copy", key=f"copy_{message['role']}_{idx}", help="Copy this message"):
pyperclip.copy(message["content"])
st.success("Copied to clipboard!", icon="✅")
if idx == len(st.session_state.conversation) - 1:
if col2.button("📋 Copy entire conversation", key="copy_entire_conv", help="Copy the entire conversation to clipboard"):
conversation_text = "\n\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in st.session_state.conversation])
pyperclip.copy(conversation_text)
st.success("Entire conversation copied to clipboard!", icon="✅")
# User input
user_input = st.chat_input("Ask me anything or share your thoughts...", key="user_input")
if user_input:
if st.session_state.chat_mode == "pdf" and st.session_state.pdf_chatbot:
with st.spinner("Processing your query..."):
response = st.session_state.pdf_chatbot.chat(user_input)
st.session_state.conversation.append({"role": "user", "content": user_input})
st.session_state.conversation.append({"role": "assistant", "content": response})
save_chat_state()
st.rerun()
else:
chat(user_input)
else:
with st.expander("🛠️ AI Tools", expanded=True):
if selected == "Text Summarization":
st.subheader("📝 Text Summarization")
text_to_summarize = st.text_area("Enter the text you want to summarize:", height=200)
if st.button("Summarize"):
if text_to_summarize:
with st.spinner("Generating summary..."):
summary = chat(f"Please summarize the following text:\n\n{text_to_summarize}")
st.subheader("Summary:")
st.write(summary)
else:
st.warning("Please enter some text to summarize.")
elif selected == "Content Generation":
st.subheader("✍️ Content Generation")
content_type = st.selectbox("Select content type:", ["Blog Post", "Email", "Marketing Slogan", "Product Description"])
topic = st.text_input("Enter the topic or product:")
if st.button("Generate Content"):
if topic:
with st.spinner("Generating content..."):
generated_content = chat(f"Generate a {content_type} about {topic}")
st.subheader("Generated Content:")
st.write(generated_content)
else:
st.warning("Please enter a topic or product.")
elif selected == "Data Extraction":
st.subheader("🔍 Data / Entity Extraction")
text_for_extraction = st.text_area("Enter the text for entity extraction:", height=200)
if st.button("Extract Entities"):
if text_for_extraction:
with st.spinner("Extracting entities..."):
entities = chat(f"Extract key entities (like names, organizations, locations, dates) from this text:\n\n{text_for_extraction}")
st.subheader("Extracted Entities:")
st.write(entities)
else:
st.warning("Please enter some text for entity extraction.")
elif selected == "Q&A":
st.subheader("❓ Question Answering")
context = st.text_area("Enter the context or background information:", height=150)
question = st.text_input("Enter your question:")
if st.button("Get Answer"):
if context and question:
with st.spinner("Finding the answer..."):
answer = chat(f"Context: {context}\n\nQuestion: {question}\n\nAnswer:")
st.subheader("Answer:")
st.write(answer)
else:
st.warning("Please provide both context and a question.")
elif selected == "Translation":
st.subheader("🌐 Text Translation")
source_lang = st.selectbox("Source Language:", ["English", "Spanish", "French", "German", "Chinese"])
target_lang = st.selectbox("Target Language:", ["Spanish", "English", "French", "German", "Chinese"])
text_to_translate = st.text_area("Enter text to translate:", height=150)
if st.button("Translate"):
if text_to_translate:
with st.spinner("Translating..."):
translated_text = chat(f"Translate the following {source_lang} text to {target_lang}:\n\n{text_to_translate}")
st.subheader("Translated Text:")
st.write(translated_text)
else:
st.warning("Please enter some text to translate.")
elif selected == "Text Analysis":
st.subheader("📊 Text Analysis & Recommendations")
analysis_text = st.text_area("Enter the text for analysis:", height=200)
analysis_type = st.multiselect("Select analysis types:", ["Sentiment Analysis", "Keyword Extraction", "Topic Classification"])
if st.button("Analyze"):
if analysis_text and analysis_type:
with st.spinner("Analyzing text..."):
analysis_results = chat(f"Perform the following analyses on this text: {', '.join(analysis_type)}.\n\nText: {analysis_text}")
st.subheader("Analysis Results:")
st.write(analysis_results)
else:
st.warning("Please enter text and select at least one analysis type.")
elif selected == "Code Assistant":
st.subheader("💻 Code Explanation & Generation")
code_action = st.radio("Select action:", ["Explain Code", "Generate Code", "Review Code"])
if code_action == "Explain Code":
code_to_explain = st.text_area("Enter the code you want explained:", height=200)
if st.button("Explain"):
if code_to_explain:
with st.spinner("Generating explanation..."):
explanation = chat(f"Explain this code:\n\n```\n{code_to_explain}\n```")
st.subheader("Explanation:")
st.write(explanation)
else:
st.warning("Please enter some code to explain.")
elif code_action == "Generate Code":
code_description = st.text_area("Describe the code you want generated:", height=150)
programming_language = st.selectbox("Select programming language:", ["Python", "JavaScript", "Java", "C++", "Ruby"])
if st.button("Generate"):
if code_description:
with st.spinner("Generating code..."):
generated_code = chat(f"Generate {programming_language} code for the following description:\n\n{code_description}")
st.subheader("Generated Code:")
st.code(generated_code, language=programming_language.lower())
else:
st.warning("Please describe the code you want generated.")
elif code_action == "Review Code":
code_to_review = st.text_area("Enter the code you want reviewed:", height=200)
if st.button("Review"):
if code_to_review:
with st.spinner("Reviewing code..."):
review = chat(f"Review this code and provide suggestions for improvement:\n\n```\n{code_to_review}\n```")
st.subheader("Code Review:")
st.write(review)
else:
st.warning("Please enter some code to review.")
# Display conversation history for non-Chat tools
if selected != "Chat":
st.subheader("Conversation History")
for idx, message in enumerate(st.session_state.conversation):
with st.chat_message(message["role"]):
st.markdown(f"<small>{message['content']}</small>", unsafe_allow_html=True)
display_message_stats(message["content"])
col1, col2 = st.columns([0.13, 0.9])
if col1.button("📋 Copy", key=f"copy_{message['role']}_{idx}", help="Copy this message"):
pyperclip.copy(message["content"])
st.success("Copied to clipboard!", icon="✅")
if idx == len(st.session_state.conversation) - 1:
if col2.button("📋 Copy entire conversation", key="copy_entire_conv", help="Copy the entire conversation to clipboard"):
conversation_text = "\n\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in st.session_state.conversation])
pyperclip.copy(conversation_text)
st.success("Entire conversation copied to clipboard!", icon="✅")
# User input for non-Chat tools
user_input = st.text_input("Ask a follow-up question or provide additional input:", key="user_input_non_chat")
if st.button("Submit", key="submit_non_chat"):
if user_input:
chat(user_input)
else:
st.warning("Please enter some input before submitting.")
# Register the function to be called when the Streamlit script stops
atexit.register(close_db_connections)