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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Here is one of the many custom scripts i build. 
# Costs to use it is exactly 0
# Even runs with llama3.1 70B or 405B..and few more...

import streamlit as st
from llm_chatbot import LLMChatBot
from streamlit_option_menu import option_menu
import speech_recognition as sr
import pyttsx3
import os
import getpass
from uuid import uuid4
import faiss
import numpy as np
import requests
import io
import warnings
import torch
import pickle
import asyncio
import json
from git import Repo
from rich import print as rp
from typing import Union, List, Generator, Any, Mapping, Optional, Dict
from requests.sessions import RequestsCookieJar
from dotenv import load_dotenv, find_dotenv
from langchain import hub
from langchain_core.documents import Document
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains import create_retrieval_chain
from langchain_community.document_loaders import DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter, Language
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma, FAISS
from langchain.vectorstores.base import VectorStore
from langchain.retrievers import MultiQueryRetriever
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain.llms import BaseLLM
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor
from langchain.retrievers.document_compressors import DocumentCompressorPipeline
from langchain_community.document_transformers import EmbeddingsRedundantFilter
from langchain_text_splitters import CharacterTextSplitter
from langchain.retrievers.document_compressors import EmbeddingsFilter
from langchain.memory.buffer import ConversationBufferMemory
from langchain.chains import StuffDocumentsChain, LLMChain, ConversationalRetrievalChain
from uber_toolkit_class import UberToolkit
from glob import glob
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.io as pio
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler
from langchain_core.documents import Document
from scipy.stats import gaussian_kde
from huggingface_hub import InferenceClient
from hugchat import hugchat
from hugchat.login import Login
from hugchat.message import Message
from hugchat.types.assistant import Assistant
from hugchat.types.model import Model
from hugchat.types.message import MessageNode, Conversation
from langchain_community.document_loaders import TextLoader
from TTS.api import TTS
import time
from playsound import playsound
from system_prompts import __all__ as prompts
from profiler import VoiceProfileManager, VoiceProfile

# Load environment variables
load_dotenv(find_dotenv())

class ChatbotApp:

  def __init__(self, email, password, default_llm=1):

    self.email = email

    self.password = password

    self.default_llm = default_llm

    self.embeddings = HuggingFaceEmbeddings(

      model_name="all-MiniLM-L6-v2",

      model_kwargs={'device': 'cpu'},

      encode_kwargs={'normalize_embeddings': True}

    )

    self.vectorstore = None


  def create_vectorstore_from_github(self):

    repo_url = "YOUR_REPO_URL"

    local_repo_path = self.clone_github_repo(repo_url)

    loader = DirectoryLoader(path=local_repo_path, glob=f"**/*", show_progress=True, recursive=True)

    loaded_files = loader.load()

    documents = [Document(page_content=file_content) for file_content in loaded_files]

    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)

    split_documents = text_splitter.split_documents(documents)

    texts = [doc.page_content for doc in split_documents]

    print(f"Texts for embedding: {texts}") # Debug print

    self.vectorstore = FAISS.from_texts(texts, self.embeddings)


  def create_vectorstore(self, docs):

     

    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)

    # Wrap text content in Document objects

    documents = [Document(page_content=doc) for doc in docs]

    # Split documents using the text splitter

    split_documents = text_splitter.split_documents(documents)

    # Convert split documents back to plain text

    texts = [doc.page_content for doc in split_documents]

    vectorstore = FAISS.from_texts(texts, self.setup_embeddings())

    return vectorstore

   

  def setup_session_state(self):

    if 'chat_history' not in st.session_state:

      st.session_state.chat_history = []

    if 'voice_mode' not in st.session_state:

      st.session_state.voice_mode = False

    if 'vectorstore' not in st.session_state:

      st.session_state.vectorstore = None

    if 'retriever' not in st.session_state:

      st.session_state.retriever = None

    if 'compression_retriever' not in st.session_state:

      st.session_state.compression_retriever = None



  def text_to_speech(self, text):

    self.engine.say(text)

    self.engine.runAndWait()



  def speech_to_text(self):

    r = sr.Recognizer()

    with sr.Microphone() as source:

      st.write("Listening...")

      audio = r.listen(source)

      try:

        text = r.recognize_google(audio)

        return text

      except:

        return "Sorry, I didn't catch that."


  def setup_embeddings(self):

    return HuggingFaceEmbeddings(

      model_name="all-MiniLM-L6-v2",

      model_kwargs={'device': 'cpu'},

      encode_kwargs={'normalize_embeddings': True}

    )

 
  def create_vector_store(self, docs):

    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)

    # Wrap text content in Document objects

    documents = [Document(page_content=doc) for doc in docs]

    # Split documents using the text splitter

    split_documents = text_splitter.split_documents(documents)

    print(f"Split documents: {split_documents}") # Debug print

    # Convert split documents back to plain text

    texts = [doc.page_content for doc in split_documents]

    print(f"Texts: {texts}") # Debug print

    if not texts:

      print("No valid texts found for embedding. Check your repository content.")

      return


    try:

      self.vectorstore = FAISS.from_texts(texts, self.embeddings)

      print("Vector store created successfully")

    except Exception as e:

      print(f"Error creating vector store: {str(e)}")


  def setup_retriever(self, k=5, similarity_threshold=0.76):

    self.retriever = st.session_state.vectorstore.as_retriever(k=k)

    splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=0, separator=". ")

    redundant_filter = EmbeddingsRedundantFilter(embeddings=self.setup_embeddings())

    relevant_filter = EmbeddingsFilter(embeddings=self.setup_embeddings(), similarity_threshold=similarity_threshold)

    pipeline_compressor = DocumentCompressorPipeline(

      transformers=[splitter, redundant_filter, relevant_filter]

    )

    st.session_state.compression_retriever = ContextualCompressionRetriever(base_compressor=pipeline_compressor, base_retriever=self.retriever)


  def create_retrieval_chain(self):

    rag_prompt = hub.pull("langchain-ai/retrieval-qa-chat")

    combine_docs_chain = create_stuff_documents_chain(self.llm, rag_prompt)

    self.high_retrieval_chain = create_retrieval_chain(st.session_state.compression_retriever, combine_docs_chain)

    self.low_retrieval_chain = create_retrieval_chain(self.retriever, combine_docs_chain)



  def setup_tts(self, model_name="tts_models/en/ljspeech/fast_pitch"):

    self.tts = TTS(model_name=model_name, progress_bar=False, vocoder_path='vocoder_models/en/ljspeech/univnet')


  def setup_speech_recognition(self):

    self.recognizer = sr.Recognizer()


  def setup_folders(self):

    self.dirs = ["test_input", "vectorstore", "test"]

    for d in self.dirs:

      os.makedirs(d, exist_ok=True)


  def send_message(self, message, web=False):

    message_result = self.llm.chat(message, web_search=web)

    return message_result.wait_until_done()


  def stream_response(self, message, web=False, stream=False):

    responses = []

    for resp in self.llm.query(message, stream=stream, web_search=web):

      responses.append(resp['token'])

    return ' '.join(responses)


  def web_search(self, text):

    result = self.send_message(text, web=True)

    return result


  def retrieve_context(self, query: str):

    context = []

    lowres = self.retriever._get_relevant_documents(query)

    highres = st.session_state.compression_retriever.get_relevant_documents(query)

    context = "\n".join([doc.page_content for doc in lowres + highres])

    return context


  def get_conversation_chain(self):

    EMAIL = os.getenv("EMAIL")

    PASSWD = os.getenv("PASSWD")

    model = 1

    self.llm = LLMChatBot(EMAIL, PASSWD, default_llm=model)
    self.llm.create_new_conversation(system_prompt=self.llm.default_system_prompt, switch_to=True)

    memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)

    conversation_chain = ConversationalRetrievalChain.from_llm(

      llm=self.llm,

      retriever=st.session_state.vectorstore.as_retriever(),

      memory=memory

    )

    return conversation_chain

  async def handle_user_input(self, user_input):

    response = st.session_state.conversation({'question': user_input})

    st.session_state.chat_history = response['chat_history']



    for i, message in enumerate(st.session_state.chat_history):

      if i % 2 == 0:

        st.write(f"Human: {message.content}")

      else:

        st.write(f"AI: {message.content}")

        if st.session_state.voice_mode:

          self.text_to_speech(message.content)

  def clone_github_repo(self, repo_url, local_path='./repo'):

    if os.path.exists(local_path):

      st.write("Repository already cloned.")

      return local_path

    Repo.clone_from(repo_url, local_path)

    return local_path


  def glob_recursive_multiple_extensions(base_dir, extensions):

    all_files = []

    for ext in extensions:

      pattern = os.path.join(base_dir, '**', f'*.{ext}')

      files = glob(pattern, recursive=True)

      all_files.extend(files)

    return all_files


  def load_documents_from_github(self, repo_url, file_types=['*.py', '*.md', '*.txt', '*.html']):

    local_repo_path = self.clone_github_repo(repo_url)

    globber=f"**/*/{{{','.join(file_types)}}}"

    rp(globber)

    loader = DirectoryLoader(path=local_repo_path, glob=globber, show_progress=True, recursive=True,loader_cls=TextLoader)

    loaded_files = loader.load()

    st.write(f"Nr. files loaded: {len(loaded_files)}")

    print(f"Loaded files: {len(loaded_files)}") # Debug print

    # Convert the loaded files to Document objects

    documents = [Document(page_content=file_content) for file_content in loaded_files]

    print(f"Documents: {documents}") # Debug print

    return documents


  def split_documents(self, documents, chunk_s=512, chunk_o=0):

    split_docs = []

    splitter=None

    for doc in documents:

      ext = os.path.splitext(getattr(doc, 'source', '') or getattr(doc, 'filename', ''))[1].lower()

      if ext == '.py':

        splitter = RecursiveCharacterTextSplitter.from_language(language=Language.PYTHON, chunk_size=chunk_s, chunk_overlap=chunk_o)

      elif ext in ['.md', '.markdown']:

        splitter = RecursiveCharacterTextSplitter.from_language(language=Language.MARKDOWN, chunk_size=chunk_s, chunk_overlap=chunk_o)

      elif ext in ['.html', '.htm']:

        splitter = RecursiveCharacterTextSplitter.from_language(language=Language.HTML, chunk_size=chunk_s, chunk_overlap=chunk_o)

      else:

        splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_s, chunk_overlap=chunk_o)

      split_docs.extend(splitter.split_documents([doc]))

    return split_docs, splitter


  def visualize_vectorstore(self):

    if st.session_state.vectorstore is None:

      st.write("Vectorstore is not initialized.")

      return

    documents = st.session_state.vectorstore.get_all_documents()

    embeddings = [doc.embedding for doc in documents]

    pca = PCA(n_components=3)

    embeddings_3d = pca.fit_transform(embeddings)

    scaler = MinMaxScaler()

    embeddings_3d_normalized = scaler.fit_transform(embeddings_3d)

    colors = embeddings_3d_normalized[:, 0]

    hover_text = [f"Document {i}:<br>{doc.page_content[:100]}..." for i, doc in enumerate(documents)]

    fig = go.Figure(data=[go.Scatter3d(

      x=embeddings_3d_normalized[:, 0],

      y=embeddings_3d_normalized[:, 1],

      z=embeddings_3d_normalized[:, 2],

      mode='markers',

      marker=dict(

        size=5,

        color=colors,

        colorscale='Viridis',

        opacity=0.8

      ),

      text=hover_text,

      hoverinfo='text'

    )])


    fig.update_layout(

      title="Interactive 3D Vectorstore Document Distribution",

      scene=dict(

        xaxis_title="PCA Component 1",

        yaxis_title="PCA Component 2",

        zaxis_title="PCA Component 3"

      ),

      width=800,

      height=600,

    )

    st.plotly_chart(fig)


  def chatbot_page(self):

    st.title("Chatbot")

    # Toggle for voice mode

    st.session_state.voice_mode = st.toggle("Voice Mode")

    # File uploader for context injection

    uploaded_file = st.file_uploader("Choose a file for context injection")

    if uploaded_file is not None:

      documents = [uploaded_file.read().decode()]

      st.session_state.vectorstore = self.create_vector_store(documents)

      st.session_state.conversation = self.get_conversation_chain()

    # GitHub repository URL input

    repo_url = st.text_input("Enter GitHub repository URL")

    if repo_url:

      documents = self.load_documents_from_github(repo_url)

      split_docs, _ = self.split_documents(documents)

      st.session_state.vectorstore = self.create_vector_store(split_docs)

      st.session_state.conversation = self.get_conversation_chain()

    # Chat interface

    user_input = st.text_input("You: ", key="user_input")

    if user_input:

      asyncio.run(self.handle_user_input(user_input))

    if st.session_state.voice_mode:

      if st.button("Speak"):

        user_speech = self.speech_to_text()

        st.text_input("You: ", value=user_speech, key="user_speech_input")

        if user_speech != "Sorry, I didn't catch that.":

          asyncio.run(self.handle_user_input(user_speech))


  def dashboard_page(self):

    st.title("Dashboard")


    if st.session_state.vectorstore is not None:

      st.write("Vectorstore Visualization")

      self.visualize_vectorstore()

    else:

      st.write("Vectorstore is not initialized. Please add documents in the Chatbot page.")


  def main(self):

    st.set_page_config(page_title="Enhanced Multi-page Chatbot App", layout="wide")

    # Sidebar navigation

    with st.sidebar:

      selected = option_menu(

        menu_title="Navigation",

        options=["Chatbot", "Dashboard"],

        icons=["chat", "bar-chart"],

        menu_icon="cast",

        default_index=0,

      )

    if selected == "Chatbot":

      self.chatbot_page()

    elif selected == "Dashboard":

      self.dashboard_page()


if __name__ == "__main__":

  app = ChatbotApp(os.getenv("EMAIL"),os.getenv("PASSWD"))

  app.main()
#https://www.linkedin.com/pulse/multi-type-ragollama31-405b-chatbot-boudewijn-kooy-t5lue/?trackingId=Q5pqCmYoQYGWkbViMWtqLQ%3D%3D