File size: 5,356 Bytes
a6e314f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from base.llm_model import get_llm
from rag.rag_module import Offline_RAG
from rag.file_loader import DocumentLoader
from rag.vector_store import VectorDB
from langchain_google_genai import GoogleGenerativeAIEmbeddings


os.environ["TOKENIZERS_PARALLELISM"] = "false"

llm = get_llm(
    model_name="gemini-1.5-flash-002",
    temperature=0.9,
    max_output_tokens=8192,
)

class ChatInterface:
    def __init__(self):
        self.rag_chain = None
        self.chat_chain = None
        self.vector_store = VectorDB(embedding=GoogleGenerativeAIEmbeddings(model="models/text-embedding-004"))
        self.chat_history = []

    def process_files(self, files, url_text):
        documents = []
        if files:
            doc_loader = DocumentLoader(file_type='pdf')
            pdf_paths = [file.name for file in files]
            documents.extend(doc_loader.load(pdf_paths))
        
        if url_text:
            html_loader = DocumentLoader(file_type='html')
            documents.extend(html_loader.load([url_text]))

        if documents:
            self.vector_store.update_db(documents)
            return [{'role': 'assistant', 'content': 'Documents processed successfully!'}]
        return [{'role': 'assistant', 'content': 'No documents provided.'}]

    def get_response(self, user_input, mode):
        if not user_input.strip():
            return self.chat_history

        if mode == "RAG" and self.vector_store.db is None:
            self.chat_history.append({"role": "user", "content": user_input})
            self.chat_history.append({"role": "assistant", "content": "Please upload documents first for RAG mode."})
            return self.chat_history

        try:
            if mode == "RAG":
                chain = self.rag_chain or Offline_RAG(llm, type='rag').get_chain(self.vector_store.get_retriever())
            else:
                chain = self.chat_chain or Offline_RAG(llm, type='chat').get_chain()

            history_text = "\n".join(
                f'User: {m["content"]}' if m["role"]=="user" else f'Assistant: {m["content"]}'
                for m in self.chat_history
            )

            response = chain.invoke({
                "chat_history": history_text,
                "input": user_input
            })
            self.chat_history.append({"role": "user", "content": user_input})
            self.chat_history.append({"role": "assistant", "content": response})
        except Exception as e:
            self.chat_history.append({"role": "user", "content": user_input})
            self.chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
        
        return self.chat_history

    def clear_history(self):
        self.chat_history = []
        return []

    def clear_vector_store(self):
        self.vector_store.clear_vectors()
        return [{'role': 'assistant', 'content': 'Vector store cleared successfully!'}]

def main():
    chat_interface = ChatInterface()

    with gr.Blocks() as demo:
        gr.Markdown(
            """
            # Document Chat Interface
            Upload documents or provide URLs to chat with their content using RAG or standard chatbot mode.
            """
        )

        with gr.Row():
            with gr.Column(scale=1):
                with gr.Tab('PDF Files'):
                    pdf_docs = gr.File(label="Upload PDF Files", file_count="multiple")
                with gr.Tab('URLs'):
                    url_text = gr.Textbox(label="Or Enter URL", placeholder="https://example.com", lines=1)
        
                mode_switch = gr.Radio(["chatbot", "RAG"], label="Select Mode", value="chatbot")
                submit_button = gr.Button("Process Documents")
                clear_vector_button = gr.Button("Clear Vector Store")
            
            with gr.Column(scale=4):
                chat_interface_md = gr.Markdown(
                    """
                    ### Instructions
                    1. Upload PDF files or enter URLs
                    2. Click 'Process Documents' to analyze the content
                    3. Select mode (RAG or chatbot)
                    4. Start chatting!
                    """
                )
                chat_history = gr.Chatbot(label="Chat History", height=400, type="messages")
                with gr.Row():
                    user_input = gr.Textbox(label="Your Question", placeholder="Ask something...", lines=2, scale=5)
                    submit_chat = gr.Button("Send", scale=1)
                clear_button = gr.Button("Clear Chat")

        submit_button.click(
            chat_interface.process_files,
            inputs=[pdf_docs, url_text],
            outputs=chat_history
        )

        user_input.submit(
            chat_interface.get_response,
            inputs=[user_input, mode_switch],
            outputs=chat_history
        )

        submit_chat.click(
            chat_interface.get_response,
            inputs=[user_input, mode_switch],
            outputs=chat_history
        )

        clear_button.click(
            chat_interface.clear_history,
            outputs=chat_history
        )

        clear_vector_button.click(
            chat_interface.clear_vector_store,
            outputs=chat_history
        )

    demo.launch()

if __name__ == "__main__":
    main()